How to open external link from UIWebView not in my App, but in Safari? SWIFT

点点圈 提交于 2019-12-13 09:10:30

问题


I would like to open all external links which are in my application (UIWebView) not inside the app, but in Safari. How can I do that? I have implement UiWebViewDelegate.

Working solution for my question bellow:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked{
    UIApplication.sharedApplication().openURL(request.URL!)
    return false
}
    return true
}

回答1:


@Leo Dabus thanks a lot for this hint, but I guess I have a better solution:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    if navigationType == UIWebViewNavigationType.LinkClicked{
        UIApplication.sharedApplication().openURL(request.URL!)
        return false
    }
    return true
}

this one works perfect.




回答2:


Solution for objective-c :- in ViewController.h

@interface ViewController : UIViewController<UIWebViewDelegate>

in ViewController.m

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if (navigationType == UIWebViewNavigationTypeLinkClicked){
        NSURL *url = request.URL;
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        return NO;
    }
    return YES;
}


来源:https://stackoverflow.com/questions/34472491/how-to-open-external-link-from-uiwebview-not-in-my-app-but-in-safari-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!