How to launch links in UIWebView with the Safari app? - iphone

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.

Related

How open link in App WebView (xCode5) to Safari.app

I just created an application made with WebView me but all the links open in the same application.
as how I can open. pdf in safari browser?
example:
to open link in facebook we use: fb://profile/257099684360698
to open link in twitter we use: twitter://user?id= 90748900
There is something to open on safari?
e.g: "safari://"
excuse my bad English.
Add this to the UIWebView delegate:
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
Look at this question on SO.

Xcode 4.3 from a button in a UIWebView open the same url in safari

I am relatively new to Xcode and I have started building an app that uses UIWebView. To make it compliant for App Store submission, Apple prefers you use Safari. To overcome this problem I want to add a button to my UIWebView navigation that, when clicked, will open the same url in Safari. An example of this can be seen in the Twitter app; they have a button that opens the currently viewed UIWebView in a Safari window.
You can use the UIWebViewDelegate's method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if iWantToOpenThisURLInSafari([request URL]) {
[UIApplication openUrl:[request URL]];
return NO; // tell the webView to not navigate to the URL, I'm handling it
} else {
return YES;
}
}
- (BOOL)iWantToOpenThisURLInSafari:(NSURL* url) [
// you just have to fill in this method.
return NO;
}
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIWebViewDelegate
EDIT: more details as requested by #PaulGraham
// You have a pointer to you webView somewhere
UIWebView *myWebView;
// create a class which implements the UIWebViewDelegate protocol
#interface MyUIWebViewDelegate:NSObject<UIWebViewDelegate>
// in this class' #implementation, implement the shouldStartLoad..method
#implementation
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if iWantToOpenThisURLInSafari([request URL]) {
[UIApplication openUrl:[request URL]];
return NO; // tell the webView to not navigate to the URL, I'm handling it
} else {
return YES;
}
}
// then, set the webView's delegate to an instance of that class
MyUIWebViewDelegate* delegate = [[MyUIWebViewDelegate alloc] init];
webView.delegate = delegate;
// your delegate will now recieve the shouldStartLoad.. messages.

Link in UIWebView seen as UIWebViewNavigationTypeOther

I am trying to open my link in an external Safari window, but for some reason it is not recognizing my link as a link. I am using the following code, the problem is that it is seeing my link as an UIWebViewNavigationTypeOther
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
What would cause it to think my link is a UIWebViewNavigationTypeOther?
Thanks
This happens when javascript code which loads a new url is called when you touch a link. Not using <a href="link">

Open Link in UIWebview not UIShared Application

I have been having a rough time trying to open links that a user clicks in a simple web view instead of multitasking and going to safari. It is quite a pain for my users to have to leave the app every time a link is clicked and I know it is probably quite simple but still am having a terrible time making this happen. Here is the code I am using but still when the link is clicked it opens safari.
If anyone can help point me in the right direction I would be greatly appreciative! Thank you!
- (void) handleURL:(NSURL*)url
{
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"%#"]]];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest
*)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"expected:%d, got:%d", UIWebViewNavigationTypeLinkClicked, navigationType);
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[UIApplication sharedApplication] ;
return NO;
}
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[web loadRequest:[NSURLRequest requestWithURL:[NSURL
URLWithString:replyTweetText.text]]];
return YES;
}
}
I'm not sure what this bit does:
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[UIApplication sharedApplication] ;
return NO;
}
The line [UIApplication sharedApplication]; creates and returns the singleton application instance. You aren't doing anything with it.
Also, both if statements are identical, so only the first will ever be hit and the method returns NO. There is no default returned value, which is bad for a non-void function. Try this instead:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}

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.