强制iOS应用程序在Safari中打开外部链接

时间:2013-02-28 15:00:42

标签: ios cordova jquery-mobile

我有一个使用PhoneGap和JQuery mobile制作的iOS应用程序。该应用程序有一些外部链接,我想在移动Safari中打开,但截至目前,它们只是在应用程序视图中打开。链接编写如下:

<a rel="external" href="wwww.example.com">Click Here</a>

我阅读了JQuery手机文档,它表示添加rel="external"可以解决这个问题,但显然不是。有任何想法吗?请注意,这是一个HTML基础应用。

2 个答案:

答案 0 :(得分:9)

最后能够通过导航到MainviewController.m并查找其中提到webView的部分,如其他帖子中提到的,然后从此更改

/* 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 ];

    }



}

我没有使用objective-c的经验,所以我不得不尝试这个,所以我很高兴能让它工作。

答案 1 :(得分:1)

尼斯帮了我一点,但它自动打开链接 通过:

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 ];

}
}