3

我对 Xcode 很陌生...我有一个单页 iOS 应用程序,它只有一个 UIWebView 打开一个特定的 URL。我希望页面中的任何链接都target="_blank"必须在 Safari 中打开,而不是在应用程序中。

有人能告诉我如何做到这一点吗?(我到处搜索)并告诉我在哪些文件中以及将代码放在哪里?非常感谢你!

编辑

我在 ViewController.m 文件中实现了以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Add line below so that external links & PDFs will open in Safari.app
    webView.delegate = self;

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/"]]];

}

// Add section below so that external links & PDFs will open in Safari.app
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return false;
    }
    return true;
}

但是对于该行webView.delegate = self;,我收到一条黄色警告,上面写着: Assigning to 'id'from incompatible type 'UIWebViewViewController *const_strong'

这是什么错误,如何在 Xcode 中修复它?

4

2 回答 2

1

也许以下关于 SO 的答案可以解决您的问题,或者至少给您一些关于如何实现您想要做的事情的想法: UIWebView open links in Safari

于 2012-07-06T08:45:16.787 回答
0

这是我们解决它的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    first.delegate = (id)self;
                [first loadRequest:[NSURLRequest requestWithURL:[NSURL      URLWithString:@"http://www.website.com"]]];
}

// Add section below so that external links & PDFs will open in Safari.app
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeOther) {
        NSString *checkURL = @"http://www.linkyouwanttogotoviasafari.com";
        NSString *reqURL = request.URL.absoluteString;
        if ([reqURL isEqualToString:checkURL])
             {
                 [[UIApplication sharedApplication] openURL:request.URL];
            return false;
    }
        else {
            return true;
        }
    }
    return true;
}
于 2013-07-25T19:49:29.603 回答