I have a ViewController with uiwebvew. This is called to show the webcontents.
My problem is , if the webcontent has links, I want to show that link page in the another instance of same ViewController.
So far I'm unable to do so. I guess I'm missing something here.
Could anyone point my problem here.
Thanks in advance.
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
PageDetailsViewController *pdvc = [self.storyboard instantiateViewControllerWithIdentifier:#"PageDetails"];
NSURL *websiteUrl = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[pdvc.webview loadRequest:urlRequest];
[self.navigationController pushViewController:pdvc animated:YES];
return NO;
}
return YES;
When I try to load in same viewcontroller, it's fine it loads. So, my guess is , there may be something wrong with my webview delegate or navigationcontroller.
Any ideas??
pdvc.webview will be nil because its view has not been loaded at the time you're trying to reference it. You should pass the URL to pdvc, and let that controller load its web view in viewDidLoad.
Related
I have an iOS app with an UIWebView, and I need to receive a href url from server side and then present the web page which is a HTML5 from that href in my UIWebView:
MyViewController *myVC = [[MyViewController alloc] init];
myVC.url = serverSideResponse.href;
[self.navigationController presentViewController:myVC animated:YES completion:nil];
In the MyViewController.m, I have this:
_webView = [[UIWebView alloc] initWithFrame:myFrame];
[self.view addSubView:_webView];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[_webView loadRequest:request];
So the web page is from server side and I want my iOS app/webview to know if the button was clicked on that page. To be more specific, if that button was clicked, then I shut my webview (meaning removeFromSuperView).
Now what I can see is that when I check the source code of that web page, I know the button id.
The url I received was: https://api.uber.com/v1/surge-confirmations/edbfdd9a-276b-4b85-b3f4-5a0c948a17fe. And when I was loading it in my web view, it has a button and when I clicked it nothing happened, only console logged the click event and the request was: https://api.uber.com/v1/surge-confirmations/edbfdd9a-276b-4b85-b3f4-5a0c948a17fe# <--there comes an extra #.
But when I put the url in my desktop browser to present it and click the button, it can redirect to the next url (https://www.uber.com) as expected.
Please advise, thanks!
Thanks to #Wain and #palme, I found one of the UIWebView delegate callback methods
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
can do this.
Parameter request and navigationType can tell you what navigationType triggered what request.
I'm trying to make it so that the back button will only be enabled when the user presses a link and/or if canGoBack = TRUE. I tried using an if statement in the body but that didn't work. I then tried using what I could find online and it still didn't work. My code is below. What am I doing wrong? Thanks!
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSString *url = webView.request.URL.absoluteString;
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
backButton.enabled = TRUE;
}
return TRUE;
}
shouldStartLoadWithRequest() won't get called if you haven't set the web view's delegate.
In order to use UIWebView its typically set as an outlet property of a view controller, so you should have something like this:
IBOutlet UIWebView* webView;
The webView has a delegate property which needs setting to your view controller, you can set delegates in a storyboard, but its more explicit to set it in code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.delegate = self;
99% of the time you just set delegates, but UIWebView's delegate is funny in that it needs setting to nil when finished with. So add this:
- (void)viewDidUnload
{
[self.webView stopLoading];
self.webView.delegate = nil;
UIWebView and WKWebView both have a canGoBack and canGoForward property on them. You can use KVO to see when these are enabled, and enable/disable your back and forward buttons accordingly.
http://nshipster.com/key-value-observing/
I load a webpage in a uiwebviewcontroller,after click one link, it will pop a popovercontroller, is it possible to get this controller in my code?
Actually, this popovercontroller is not created by my code. The html detect the webpage is loaded by iDevice and pop this. That means, if I use safari to open webpage, it also will display this popovercontroller.
Thanks.
implement the UIWebView delegate method.
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
if([urlString isEqualToString :#"abc.com") // link on which want to open popoverview
{
UIPopoverController itemPopover = [[UIPopoverController alloc] initWithContentViewController:viewController];
[itemPopover presentPopoverFromRect:customCell.addImage.bounds inView:webViewpermittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
return NO; //if wanted to load the link on UIWebview return YES else return NO
}
return YES;
}
I have this method that goes back to previous webpage in a web view under certain circumstance and it works fine. However, I want to implement a function that checks if I am on the original web page and if so, when I push the back button it takes me to a navigation view controller. The original page is this code [webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];. Now I need to figure out how to check if I am on that page and if so I need to execute this code: [self.navigationController popViewControllerAnimated:YES];. Any help will be appreciated.
- (void)back
{
if ([webView canGoBack]) {
[webView goBack];
} else {
[webView loadHTMLString:self.item.description baseURL:[NSURL URLWithString:self.item.link]];
}
}
You can use the delegate method shouldStartLoadWithRequest of UIWebViewDelegate to check what contains in the url. if you find it then just go NavigationController
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[request.URL absoluteString] rangeOfString:#"http://yourURL"].location!=NSNotFound) {
[self.navigationController popViewControllerAnimated:YES];.
}
}
You need to add
<UIWebViewDelegate>
to the header. Then set webView.delegate to yes;
Once you have it set as a delegate, then you can get the absoluteString and the requestURL, and override the method. With the absoluteString you can check for a substring, text, domain, basically anything you want! You override shouldStartLoadWithRequest or webViewDidFinishLoad:
-(void)webViewDidFinishLoad:(UIWebView *)webView
-(void)shouldStartLoadWithRequest :(UIWebView *)webView
That calls everytime any webpage is loaded.
Thanks for viewing my question. Let me describe the app first. I have a tab bar based app for iOS5.1 that is using storyboard and ARC. There are four tabs with a view controller in each one that shows a webview with local HTML files (each view is a different set of html files to display).
Currently when a user touches a http or https link, it'll open in Safari. However, I now want it to open within app via a modal view. I currently have the interface created and ready to show, but I can't pass the URL to the modal's "webview2". I've looked for many examples to do this using RSURL and strings and etc, but can't get any of them to work. Some of the example code given didn't seem to work for my situation.
In my FirstViewController.m file (which is same in the other views), I have...
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//Gets the link.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
NSLog(#"url:%#",request); //Get's the url itself
//If it's an external link...
if ([[URL scheme] isEqualToString:#"http"] ||
[[URL scheme] isEqualToString: #"https" ]) {
[[UIApplication sharedApplication] openURL:[request URL]];
NSLog(#"Opened in Safari");
return NO;
}
//If it's an email address...
else if ([[URL scheme] isEqualToString:#"mailto"]) {
[webView loadRequest:request];
[[UIApplication sharedApplication] openURL:[request URL]];
NSLog(#"Opened in Mail App");
return NO;
}
}
return YES;
}
Which works fine if I want the URL to open in Safari. So in this part...
//If it's an external link...
if ([[URL scheme] isEqualToString:#"http"] ||
[[URL scheme] isEqualToString: #"https" ]) {
[[UIApplication sharedApplication] openURL:[request URL]];
NSLog(#"Opened in Safari");
return NO;
}
I need it to open a modal view with the clicked URL, not Safari.
So what I'm asking is...
What is the best way to get this URL saved, then open the modal view and then in the modal webview open the URL that was just touched?
I already have the modal view made with a dismiss button and etc. I just need it to load with the clicked URL when it loads.
I can load the modal via the line...
[self performSegueWithIdentifier:#"ModalWebView" sender:self];
but that just loads it without any URL info and a blank webview, of course. The modal uses WebViewController.h/m.
If anyone could tell me how to open and pass on the URL so it loads in the segue "ModalWebView", I would appreciate it. Please let me know if I need to make any properties or anything else that I should include in my WebViewController.h/m that runs the modal view. Maybe -(void)prepareForSegue needs to be used? If so, how?
I realize the answer maybe simple, but I'm still a beginner and have spent days trying to find a clear answer. I've found answers to much of my questions on this site by searching before, but I'm afraid I'm stuck on this one.
Thank you for your time and patience.
In your view controller, implement the prepareForSegue method and add the following code.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(#"Source Controller = %#", [segue sourceViewController]);
NSLog(#"Destination Controller = %#", [segue destinationViewController]);
NSLog(#"Segue Identifier = %#", [segue identifier]);
if ([segue.identifier isEqualToString:#"ModalWebView"]) {
ModalWebViewController *wVC = [segue destinationViewController];
wVC.url = [NSURL URLWithString:article.url]; // replace article.url with your url.
}
}
In my code above, the URL that will be used is set from my article.url object. To get this to work, you need to get the url from the method shouldStartLoadWithRequest.
// CurrentViewController.h
#property (strong, nonatomic) NSURL *targetUrl;
// CurrentViewController.m
//If it's an external link...
if ([[URL scheme] isEqualToString:#"http"] ||
[[URL scheme] isEqualToString: #"https" ]) {
targetUrl = url;
[self performSegueWithIdentifier:#"ModalWebView" sender:self];
return NO;
}
If using the above, we now replace article.url in the first code block with targetUrl.
if ([segue.identifier isEqualToString:#"ModalWebView"]) {
ModalWebViewController *wVC = [segue destinationViewController];
wVC.url = targetUrl;
}
In your view controller for your modal web view.h file create a NSURL.
//ModalWebViewController.h
#property (strong, nonatomic) NSURL *url;
In your implementation file (.m) create the method viewDidAppear
//ModalWebViewController.m
-(void) viewDidAppear:(BOOL)animated {
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
}
Hope I've explained that enough for you to be able to implement.