链接不会在带有 PhoneGap 的 JQuery Mobile 中的外部浏览器中打开 [英] Links don't open in external browser in JQuery Mobile with PhoneGap

查看:15
本文介绍了链接不会在带有 PhoneGap 的 JQuery Mobile 中的外部浏览器中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PhoneGap 2.3.0 和 JQuery Mobile 1.2.0 中遇到问题.

I'm having a problem in PhoneGap 2.3.0 with JQuery Mobile 1.2.0.

iOS 中的任何外部链接都在应用内打开,而不是打开它们在应用内打开的 Safari,这使得用户无法在不重新启动的情况下返回应用.

Any external link iniOS opens inside the app instead of opening Safari they open inside the app, making it impossible for user to get back to the app without rebooting it.

我已经尝试了 rel="external"target="_blank" 来表明它是一个外部链接,但都没有成功.

I have tried both rel="external" and target="_blank" to indicate it's an external link, but none with success.

我已经看到带有 JQMobile 的 PhoneGap 应该采取的默认方式是我想要的方式.我发现很多人要求这种行为,但没有找到解决办法.

I have seen that the default way that PhoneGap with JQMobile should act is the way I want. I have found lots of requests for this kind of behaviour, but not the way around.

推荐答案

我将 rel="external" 添加到我的锚链接.

I added rel="external" to my anchor links.

然后在MainViewController类中添加/覆盖shouldStartLoadWithRequest方法:

And then added/overrided the shouldStartLoadWithRequest method in the MainViewController class:

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];

    // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]){
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
    }
}

这在 jQuery Mobile 1.2 和 Phonegap 2.2.0 中对我有用.它应该在 Phonegap 2.3.0 中工作相同 - 但我还没有测试过.

That works for me in jQuery Mobile 1.2 and Phonegap 2.2.0. It should work the same in Phonegap 2.3.0 - but I haven't tested that.

====================================================================================

==================================================================================

更新:

在 Phonegap 2.7.0 或更高版本中可能不需要这样做.Phonegap 现在可以在 UIWebView、Safari 或 InAppBrowser 组件中打开链接.我个人喜欢 InAppBrowser 组件,因为它对于很多用例来说似乎是更好的用户体验.如果您想在 Safari 中打开链接,您现在可以使用 Javascript 执行此操作:

There may not be any need to do this in Phonegap 2.7.0 or above. Phonegap can now open links in either of the UIWebView, Safari or the InAppBrowser component. Personally I like the InAppBrowser component, as it seems to be a better user experience for a lot of use cases. If you want to open links in Safari you can now do this now using Javascript:

window.open('http://whitelisted-url.com', '_system');

或者对于 InAppBrowser:

or this for the InAppBrowser:

window.open('http://whitelisted-url.com', '_blank');

查看此处了解更多信息:

Have a look here for more information:

http://wiki.apache.org/cordova/InAppBrowserhttp://docs.phonegap.com/en/2.7.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser

这篇关于链接不会在带有 PhoneGap 的 JQuery Mobile 中的外部浏览器中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆