Using a url scheme internally with a UIWebView to change a UILabel - iphone

I've got a UIWebView which shows up a couple of screens into a UINavigationController:
First View > Second View > View with UIWebView + UILabel
Now, I display a certain page in that web view, which has a link back to my app....
myapp://foofoo
I know you can set up a custom URL with (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url and some info.plist poking, but how would change the UILabel, which is one the same screen as UIWebView, by simply clicking the myapp://foofoo link?

The best way to do this would be through a custom link, and then use the UIWebView delegate method -webView:shouldStartLoadWithRequest:navigationType: to trap requests. When you see a request come through with your link in it, you know your action has been triggered.
UIWebView Expose Objective C to JavaScript

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"scheme = %#",[[request URL] scheme]);
if([[[request URL] scheme]isEqualToString:#"myscheme"])
{
[messageLabel setText:#"HELLO!"];
return NO;
}
else return YES;
}
Make sure your view controller conforms to the UIWebViewDelegate protocol for this to work.

Related

ios uiwebview stop request when shouldstartloadwithrequest

I am receiving shouldstartloadwithrequest event and I need to stop request for some conditions. How to stop uiwebview request when shouldstartloadwithrequest? Thank you
Check for the condition, then return NO in shouldStartLoadWithRequest. Make sure to set delegate appropriately and include the UIWebViewDelegate protocol in the interface of the delegate class.
You can invoke the stopLoading method for the UIWebView. Below code will check if the webview is loading and stop it.
if([webView isLoading]) {
[webView stopLoading];
}
EDIT: After re-reading your question, I understood you want to return NO in the request to stop it from beginning to load, so you would do something like:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//Check your condition:
if(myCondition == TRUE)
return NO;
return YES;
}

Query about UIWebview

HI am making a application in which the the user enter data into text field(url) and that url is opened in the webview below that .Now when the user clicks on any link in the webview that we have opened then can i get the address of the clicked url (the url which user clicked on the webview) as i need to update(display) that url into texfiled above , so that user can come to know which url is being opened.
you can use
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
delegate for your purpose.
like
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestedUrl = [request URL];
//use requestedURL methods
// like yourtextbox.text = [requestedUrl absoluteURL];
//don't forget to return YES :)
return YES;
}
Yes there is an undocumented method. See my answer in the post Show alert view when click on a link
Yes, UIWebView has some delegate methods which can help you with this:
Check this one
– webView:shouldStartLoadWithRequest:navigationType:

UIWebView cannot click link

I am pretty sure that I understand how to catch a click on a UIWebView using the webView:shouldStartLoadWithRequest:navigationType: method, but my webView does not even allow me to click the link. I am using:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(12, top, boundsSize.width - 40.0, 400.0f)];
webView.delegate = self;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.allowsInlineMediaPlayback = YES;
webView.dataDetectorTypes = UIDataDetectorTypeAll;
// add to subview
I am loading in an HTML string rather than loading a URL:
[webView loadHTMLString:bodyHTML baseURL:nil];
Delegate method:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Loaded");
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(urlString);
}
return YES;
}//end
Everytime my webview loads I DO get the "Loaded" in my logs, so I know that the delegate method is getting called. But I can never click on a link within my UIWebView and have anything happen. It does not even look like it is allowing a pressed state on the link. The link is highlighted like a link, just won't allow clicking.
Ideas?
The reason for the UIWebView not responding to touch events is most probably this line:
webView.opaque = NO;
Try setting opaque to YES.
My understanding is that for a view to respond to touch events, that view has to be returned by the call to hitTest:withEvent: during the view hierarchy traversal performed by the event delivery mechanism.
From the hitTest:withEvent: documentation:
This method ignores view objects that are hidden, that have disabled user interaction, or have an alpha level less than 0.01.
Update
Based on new info: if the UIWebView is embedded in a UIScrollView, quote from the Apple docs:
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
Please try with following code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType==UIWebViewNavigationTypeLinkClicked)
{
[[UIApplication sharedApplication]openURL:request.URL];
return NO;
}
return YES;
}

How to launch links in UIWebView with the Safari app?

I have an small embedded UIWebView for my about-section of the app. When the user taps a link in there, the link opens in that small embedded UIWebView which of course has no navigation controls and isn't full-screen. I don't like that. Can I force it to open links with Safari instead?
You can implement the shouldStartLoadWithRequest method of the UIWebViewDelegate protocol to intercept link clicks. Inside that method you can use UIApplication's openURL method to cause the url to be opened in the appropriate application (ie. Safari for HTTP/HTTPS).
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ([url isEqual:YOUR_HOME_URL_CONTSTANT])
return YES;
[[UIApplication sharedApplication] openURL:url];
return NO;
}
You should implement shouldStartLoadWithRequest of the UIWebViewDelegate. In here you can see when the UIWebView will start loading a URL. Then you can handle which should be loaded in this webView and which should be loaded by Safari.
My code looks like:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if (inType != UIWebViewNavigationTypeLinkClicked)
{
//This was not a clicked link, so is probably the initial load
//and should be loaded in the UIWebView
return YES;
}
//This was a clicked link, so load using Safari
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
Here I am using UIWebViewNavigationType to determine whether it was a link click, and responding appropriately.

UIWebView in UIView need links to open up in browser

So i have an app that is not so much a webapp but more uses UIViewControllers and UIViews for most screens. On one particular controller i have a UIWebView control that only occupies a small portion of the UIViewController screen real estate that i load html from a web service as a string.
[self.webView loadHTMLString:marketingBlurb baseURL:nil];
what i'm wondering is if the html links in this string can open up on the browser and not in the UIWebView in my app???? How can i do this?? Is this even possible ?
Set the delegate of the webview to self, and intercept all links using webView:shouldStartLoadWithRequest:navigationType:
You can then call [UIApplication openURL] to open Safari.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
I didn't test this code, but it will get you started.