pass url information in segue - xcode

I have 2 button set each with a segue to a view controller with a web view. I'm try to load a url in the web view based on which button started the segue.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: #"toWeb"]){
NSString *abc = #"http://www.abc.com/";
NSURL *url = [NSURL URLWithString:kmb];
NetView *netview =[segue destinationViewController];
[netview.internet loadRequest:[NSURLRequest requestWithURL:url]];
}else if ([segue.identifier isEqualToString: #"toFacebook"]){
NSString *fb = #"http://www.facebook.com/ABC?ref=hl";
NSURL *url2 = [NSURL URLWithString:fb];
NetView *netview =[segue destinationViewController];
[netview.internet loadRequest:[NSURLRequest requestWithURL:url2]];
}
}
This is not working but I'm not getting any error just a bland view on the new screen. I'm not sure what the problem is. Can anyone help point me in the right direction? Thank You.

The problem is that the webview on the destination controller has not been loaded at this point. Just set a property on the destination controller to hold your url and then on its viewDidLoad method send the message to the webview in order to load the request. Something like this:
NetView.h
...
#property (nonatomic, copy) NSString *url;
NetView.m
...
#synthesize url;
Your code snippet:
if ([segue.identifier isEqualToString: #"toWeb"]){
NetView *netview = [segue destinationViewController];
netview.url = #"http://www.abc.com/";
}
...
NetView.m
- (void)viewDidLoad
{
NSURL *urlToLoad = [NSURL URLWithString:self.url];
[self.internet loadRequest:[NSURLRequest requestWithURL:urlToLoad]];
}

Related

Display images from URL in a Detailviewcontroller with Tableviewcontroller as a parent

Hard to explain, but I'll try;
I am making an app for showing roadcameras. I have a TableViewController which leads to a DetailViewController.
What I want to achieve is that when I click on Row 1 it shows the picture from www.url1.com. If I click on Row 2, it shows the picture from www.url2.com, and so on. I can't seem to figure out how to do this.
I first started by using this code:
_place = #[#"E6 Minnesund",
#"E6 Heia"];
_url = #[#"http://webkamera.vegvesen.no/kamera?id=450848",
#"http://webkamera.vegvesen.no/kamera?id=100102"];
But stopped because I couldn't see how I could get this working...
Any ideas?
And please don't just rate the post down because you think its stupid. I'm kinda new to Xcode and for me, this is hard. Thanks in advance
I can post the whole project folder if necessary
just write the code in didselectrowAtIndexPath
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *picker = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
picker.row=indexPath.row;
[self.navigationController pushViewController:picker];
}
and in DetailViewController
in .h
#interface DetailViewController : UIViewController
{
}
#property(nonatomic,assign)int row;
in .m
- (void)viewDidLoad
{
dispatch_queue_t myqueue = dispatch_queue_create("myqueue", NULL);
// execute a task on that queue asynchronously
dispatch_async(myqueue, ^{
NSURL *url = [NSURL URLWithString:[_url objectAtIndex:row];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
Image.image = [UIImage imageWithData:data]; //UI updates should be done on the main thread
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:myImageView];
});
});

How to display a image when there is no internet connection (web view)

I have a web view based program I was just testing. How do you display a image when there is no internet connection?
I have a basic webview
//
// XYZViewController.m
// Phantomore
//
// Created by Kevin Jin on 5/15/14.
// Copyright (c) 2014 Kevin Jin. All rights reserved.
//
#import "XYZViewController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#end
Thats it,I just want to know what I have to do to have it displaying a image when tehre is no internet connection
I just simplified mamnun answer(Get Reachability here).
You can use this method after you have implemented the reachability class
- (BOOL)isConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
In any of your class
- (void)viewDidLoad
{
[super viewDidLoad];
if([self isConnectedToInternet]){
//Hide your imageview
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
else{
// Hide your webview
// Show your imageview
// Do not forget to import reachability class on top
}
}
There is two way of doing it.
Before even performing the url request, check if internet connection is available. Use Apple provided Reachability classes or better yet use the open-source version. If internet is available load the request. If not add an UIImageView with your preferred UIImage as a subview of the webview. You can do something like this:
//inside #interface
#property(nonatomic, strong) UIImageView *imageView;
//inside -(void)viewDidLoad
self.imageView = [UIImageView alloc] initWithImage:[UIImage imageNamed:#"as.as"];
Reachability* reach = [Reachability reachabilityWithHostname:#"www.google.com"];
reach.reachableBlock = ^(Reachability*reach) {
// load request
NSString *fullURL = #"http://www.phantomore.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
};
reach.unreachableBlock = ^(Reachability*reach) {
//show imageview
[_viewWeb addSubView:self.imageView];
};
[reach startNotifier];
Implement UIWebView delegate. In the – webView:didFailLoadWithError: method, check the error. If the error is caused by no internet connection, add the imageview(same as before).

send URL to detail view with button selection in master view

I am trying to make master-detail view with generic webview in detail where the URL changes with selection of button in master view controller. Obviously I'm a novice and I need help!
in master view mfile I send through button like this:
-(IBAction)myButton1:(UIButton *) sender {
[self.detailVC location:#"http://google.com"];
}
-(IBAction)myButton2:(UIButton *) sender {
[self.detailVC location:#"http://yelp.com"];
}
in detail view mfile I have:
-(void)location(NSString *)myLocation{
_theLocation = myLocation;
}
-(void) viewDidLoad {
[super viewDidLoad];
NSURL *videoURL = [NSURL URLWithString:self.theLocation];
NSURLRequest *request = [NSURL Request requestWithURL:videoURL];
[self.webview loadRequest:request];
}
I am getting no build errors, but none of the URL's are loading. Please help!
You should know - (void)viewDidLoad Called after the controller’s view is loaded into memory , in your issue, it seems the self.detailVC is always alive in memory, that should not call viewDidLoad.
you can update the web view in - (void)setLocation(NSString *)myLocation directly:
- (void)setLocation(NSString *)myLocation
{
_location = myLocation;
NSURL *videoURL = [NSURL URLWithString:self.location];
NSURLRequest *request = [NSURL Request requestWithURL:videoURL];
[self.webview loadRequest:request];
}

How to pass value to another controller view in Xcode

How do i get the value from different UIViewController in xcode storyBoard?
I end up using prepareForSegue method.
first i create a string in my third view controller.
#property (strong,nonatomic) NSString* stringFromSecondView;
Then I gave the push segue an ID called "getDate" and in my second view class use this code below and remember to import the thirdviewcontroller.h
#import "thirdViewController.h"
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"getDate"]){
NSString *intro = _myDate.text;
thirdViewController *vc = [segue destinationViewController];
vc.stringFromSecondView = intro;
}
}
Now back to my thirdViewController.m
_myLabel.text = stringFromSecondView;
In your second VIew Controller
- (IBAction) btnPressToBringThirdView : (id) sender
{
ThirdViewController* newObject = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
newObject.labelString = #"whatever text you want";
[self.navigationController pushViewController:newObject];
[newObject release];
}
In your third viewcontroller in .h file
#property (nonatomic, retain) NSString* labelString;
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
yourLabel.text = labelString;
}
ViewController *secondview=[[ViewController alloc]init];
secondview.your var here= secondview.your var here+100;//whate you var to change if NSintager
///to save your var
[[NSUserDefaults standardUserDefaults]setInteger:secondview.your var here forKey:#"your key to save"];

How do I load html into a webview?

I have just re-designed my iPhone app using Storyboard... questin is: how do I address the the UIWebView so I can load the html? (I am a newbie when it comes to Obj-C, having done my previous app in MonoTouch, so I'm kinda lost). :D
- (void)viewDidLoad {
NSString *urlAddress = #”http://www.stackoverflow.com”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Apple Developer docs are really good resource. The usage of UIWebView is outlined here enter link description here
To load a network resource simply pass a NSURLRequest object to loadRequest.
[self.myWebView loadRequest:[NSURLRequest
requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]]];
You can also do this in viewDidLoad (a UIWebView is a subclass of UIView and inherits all its methods). For example, loading a local pdf resource.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"iPhone_User_Guide" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:#"application/pdf"
textEncodingName:#"utf-8" baseURL:nil];
}
}
To setup your UIWebView with a link, just use the following code.
var URLPath = "Insert email address here"
Then use...
loadAddressURL()
and finally...
func loadAddressURL(){
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL: requestURL!)
Webview.loadRequest(request)
}

Resources