我是Xcode的新手...我有一个单页iOS应用程序,只有一个UIWebView打开一个特定的URL。我想在target="_blank"
页面中的任何链接在Safari中打开,而不是在应用程序内打开。
有人可以告诉我如何做到这一点吗? (我到处搜索)并告诉我哪些文件以及放置代码的位置?谢谢SOOO !!
修改
我在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;
行,我收到一条黄色警告:
从不兼容的类型“UIWebViewViewController * const_strong”
这是什么错误,如何在Xcode中修复它?
答案 0 :(得分:1)
也许关于SO的回答可以解决您的问题或至少给您一些关于如何实现您想要做的事情的想法: UIWebView open links in Safari
答案 1 :(得分: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;
}