Xcode 在 Safari 中打开 target=_blank 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11354324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode Open target=_blank links in Safari
提问by adamdehaven
I'm pretty new to Xcode... I have a single page iOS app that just has a UIWebView opening a specific URL. I would like any links within the pages that have target="_blank"
to open in Safari, rather than inside the app.
我对 Xcode 很陌生...我有一个单页 iOS 应用程序,它只有一个 UIWebView 打开一个特定的 URL。我希望页面中的任何链接都target="_blank"
必须在 Safari 中打开,而不是在应用程序中打开。
Can someone tell me how to accomplish this? (I've searched everywhere) and also tell me in which files and where to put the code? Thank you SOOO much!!
有人能告诉我如何做到这一点吗?(我到处搜索)并告诉我在哪些文件中以及将代码放在哪里?非常感谢你!!
EDIT
编辑
I implemented the following code in my ViewController.m file:
我在 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;
}
But for the line webView.delegate = self;
I am getting a yellow warning that says:
Assigning to 'id'from incompatible type 'UIWebViewViewController *const_strong'
但是对于这一行,webView.delegate = self;
我收到了一条黄色警告,上面写着:
从不兼容的类型“UIWebViewViewController *const_strong”分配给“id”
What is this error, and how can I fix it in Xcode?
这是什么错误,我该如何在 Xcode 中修复它?
回答by Rohit Gupta
Perhaps following answer on SO can solve your problem or atleast give you some ideas on how to achieve what you are trying to do: UIWebView open links in Safari
也许以下关于 SO 的回答可以解决您的问题,或者至少给您一些关于如何实现您正在尝试做的事情的想法: UIWebView open links in Safari
回答by Dwight Mix
This is the way we solved it:
我们是这样解决的:
- (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;
}