iOS webview使用Safari加载另一个URL

时间:2015-08-19 14:34:12

标签: ios uiwebview

我正在为iOS制作应用,使用UIwebview在应用中加载我的网站。我想在Safari中打开所有链接(除了我的网站)。阅读了很多文章并尝试了许多不同的建议仍然无法实现。

我不熟悉编码,使用Xcode 6.3.2

这是我的ViewController.m代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *fullURL = @"http://www.mywebsite.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_viewWeb loadRequest:requestObj];
}


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

    // Check if this was a click event and then some other criteria for determining if you want to launch Safari.
    if (navigationType == UIWebViewNavigationTypeLinkClicked
        && [ [ request.URL scheme ] isEqualToString: @"http" ] ) {
        [[UIApplication sharedApplication] openURL:request.URL];

    // Return false to indicate to the UIWebView to not navigate to the linked target
    return false;
}

// Return true so that the UIWebView loads the link target
return true;
}

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

@end

请帮助

1 个答案:

答案 0 :(得分:0)

更新您的方法

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

之类的:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestURL = [[request URL] absoluteString];

    if ([requestURL rangeOfString:@"mydomain.com"].location != NSNotFound) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    return YES;
}
相关问题