我对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;
行,我收到一个黄色警告,上面写着:分配给"id"来自不兼容的类型"UIWebViewViewController *const_strong"
这个错误是什么,如何在 Xcode 中修复它?
也许以下关于SO的答案可以解决您的问题,或者至少为您提供一些有关如何实现您要做的事情的想法:UIWebView 在 Safari 中打开链接
这是我们解决它的方法:
- (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;
}