强制iOS应用程序在safari中打开外部链接
问题描述:
我有使用PhoneGap和JQuery mobile制作的iOS应用程序。该应用程序有一些我想在移动Safari中打开的外部链接,但截至目前,它们只是在应用程序视图中打开。这些链接是这样写的:强制iOS应用程序在safari中打开外部链接
<a rel="external" href="wwww.example.com">Click Here</a>
我读的JQuery移动文档和它说,加入rel="external"
会解决这个问题,但显然不是。有任何想法吗?请记住,这是一个基于HTML的应用程序。
答
最后能够通过导航到MainviewController.m和寻找一个部分做在其他职位提到然后从该
/* Comment out the block below to over-ride */
/*
- (void) webViewDidStartLoad:(UIWebView*)theWebView
{
return [super webViewDidStartLoad:theWebView];
}
- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
{
return [super webView:theWebView didFailLoadWithError:error];
}
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
*/
将其更改为这个
/**
* Start Loading Request
* This is where most of the magic happens... We take the request(s) and process the response.
* From here we can re direct links and other protocalls to different internal methods.
*/
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// add any other schemes you want to support, or perform additional
// tests on the url before deciding what to do -jm
if([[url scheme] isEqualToString:@"http"] ||
[[url scheme] isEqualToString:@"https"])
{
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else
{
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
它提到web视图
我没有Objective-C的经验,所以我不得不尝试这个,所以我很高兴我能够实现它。
答
尼斯帮了我一点,但automtically 打开链接通过将:
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
}
它的工作,现在当用户点击一个网址为http://或https://它它在Safari的
所以完全我得到这个代码打开:
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if([[url scheme] isEqualToString:@"http"] ||
[[url scheme] isEqualToString:@"https"])
{
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else
{
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
是由有'rel ='一个错字? – jjv360 2013-02-28 15:03:11
是的,这是我的错,它的拼写正确的HTML,我只是写在我自己。拼写已被纠正在后 – mhartington 2013-02-28 15:05:24
可能重复[PhoneGap:在Safari中打开外部URL](http://stackoverflow.com/questions/10244965/phonegap-opening-external-urls-in-safari) – 2013-02-28 15:12:27