简体   繁体   中英

UIWebView open link in Safari not working?

I'm working on a FAQ page in my iPhone App. I have some question links on top will direct to answer in bottom. Some answers have external links which I want to open in Safari instead of UIWebView.

I found this code from here :

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
     if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        [[UIApplication sharedApplication] openURL:[inRequest URL]];
        return NO;
     }

     return YES;
}

For some reason I need to click the link twice to open the link. First time touch the link it highlight's it. Second time touch the link it goes to the destination or for my external links it opens in Safari. However if I remove the above code it works on the first touch but external links opens in my WebView.

Note : I have a UIWebView delegate name wvFAQ. Do you think I'm missing something on the code?

Question : How to open the link in single click?

Finally found the answer to my question from here

Here is the answer:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; 
{
     NSURL *requestURL =[ [ request URL ] retain ]; 
     if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ]) 
         && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) { 
         return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; 
     } 
     [ requestURL release ]; 
     return YES; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM