I'm want detect link clicks in a UIWebView which is inside one of my ViewControllers and then initiate a new ViewController which is basically another UIWebView with the detected link.
heres my code:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if (self.interceptLinks){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
BrowserViewController *browser = [storyboard instantiateViewControllerWithIdentifier:@"browserViewController"];
NSLog(@"sending url== %@", url);
[browser openBrowserWithUrl:url];
[self performSegueWithIdentifier:@"browserDetail" sender:self];
return NO;
}
else {
self.interceptLinks = TRUE;
return YES;
}
}
Then in BrowserViewController.h
@property (strong, nonatomic) NSURL *incomingURL;
@property (strong, nonatomic) IBOutlet UIWebView *browserView;
- (void)openBrowserWithUrl:(NSURL *)url;
Then in BrowserViewController.m
- (void)openBrowserWithUrl:(NSURL *)url
{
self.incomingURL = url;
NSLog(@"incomming_URL==%@", self.incomingURL);
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad_url == %@", self.incomingURL);
self.browserView.delegate = self;
self.browserView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.browserView loadRequest:[NSMutableURLRequest requestWithURL:self.incomingURL]];
[self.view addSubview:_browserView];
// Do any additional setup after loading the view.
}
The NSLog inside the "openBrowserWithUrl" returns the desired NSURL, but the one inside "viewDidLoad" returns null, what am I doing wrong here ??
Calling performSegueWithIdentifier
will actually instantiate a new instance of BrowserViewController
. I'm not familiar enough with storyboards to know how to do this, but there is a UIViewController
callback that will let you know when a segue is being performed, so that you can pass it the incomingURL
.
If you want to perform it manually, instead of using storyboards, you can replace your performSegue
call with
[self.navigationController pushViewController:browser animated:YES];