从Safari中的UIWebView打开外部链接

时间:2015-09-07 08:59:43

标签: ios safari uiwebview

我想修改我的代码,以便在Safari上打开外部链接,而不是我应用的应用内浏览器。 html文件是从内部路径加载的,所以我不知道如何让应用程序理解必须在safari中打开所有外部链接。

这是我的ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *fullURL = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_viewWeb loadRequest:requestObj];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我该如何修改它? 谢谢!

1 个答案:

答案 0 :(得分:1)

您需要在UIWebView委托方法中处理此问题,并使用 UIWebViewNavigationType 进行检查。

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

        if(navigationType == UIWebViewNavigationTypeLinkClicked){

          [MAIN_APPLICATION_DELEGATE openURL:request.URL];
    }
}