【问题标题】:Phonegap 3.5.0 open link in external Safari window not workingPhonegap 3.5.0 在外部 Safari 窗口中打开链接不起作用
【发布时间】:2014-09-12 22:18:05
【问题描述】:

这个问题已经被问了一百万次了,我已经尝试了所有的解决方案:

“防止默认”解决方案, “删除冲突的状态栏隐藏插件”解决方案, “_blank & _system, location=yes & no”解决方案的变体,

但似乎没有一个有效。该 URL 只是在应用程序本身中打开,无法退出。

我什至尝试过我在几个地方看到的这段代码:

var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener('loadstart', function(event) { alert('start: ' + event.url); });
ref.addEventListener('loadstop', function(event) { alert('stop: ' + event.url); });
ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); });
ref.addEventListener('exit', function(event) { alert(event.type); });

但是,该链接仍然会在应用程序中打开。没有我听说过的“完成”按钮。没有 Safari。

我的 config.xml:

<feature name="http://api.phonegap.com/1.0/device" />

<!-- Platform Build -->
<gap:platform name="ios" />

<!--
    If you do not want any permissions to be added to your app, add the
    following tag to your config.xml; you will still have the INTERNET
    permission on your app, which PhoneGap requires.
-->
<preference name="permissions"                value="none"/>

<!-- Customize your app and platform with the preference element. -->
<preference name="phonegap-version"           value="2.9.0" />          <!-- all: current version of PhoneGap -->
<preference name="orientation"                value="default" />        <!-- all: default means both landscape and portrait are enabled -->
<preference name="target-device"              value="universal" />      <!-- all: possible values handset, tablet, or universal -->
<preference name="fullscreen"                 value="true" />           <!-- all: hides the status bar at the top of the screen -->
<preference name="webviewbounce"              value="true" />           <!-- ios: control whether the screen 'bounces' when scrolled beyond the top -->
<preference name="prerendered-icon"           value="true" />           <!-- ios: if icon is prerendered, iOS will not apply it's gloss to the app's icon on the user's home screen -->
<preference name="stay-in-webview"            value="false" />          <!-- ios: external links should open in the default browser, 'true' would use the webview the app lives in -->
<preference name="ios-statusbarstyle"         value="black-opaque" />   <!-- ios: black-translucent will appear black because the PhoneGap webview doesn't go beneath the status bar -->
<preference name="detect-data-types"          value="true" />           <!-- ios: controls whether data types (such as phone no. and dates) are automatically turned into links by the system -->
<preference name="exit-on-suspend"            value="false" />          <!-- ios: if set to true, app will terminate when home button is pressed -->
<preference name="show-splash-screen-spinner" value="true" />           <!-- ios: if set to false, the spinner won't appear on the splash screen during app loading -->
<preference name="auto-hide-splash-screen"    value="true" />           <!-- ios: if set to false, the splash screen must be hidden using a JavaScript API -->
<preference name="disable-cursor"             value="false" />          <!-- blackberry: prevents a mouse-icon/cursor from being displayed on the app -->
<preference name="android-minSdkVersion"      value="7" />              <!-- android: MIN SDK version supported on the target device. MAX version is blank by default. -->
<preference name="android-installLocation"    value="auto" />           <!-- android: app install location. 'auto' will choose. 'internalOnly' is device memory. 'preferExternal' is SDCard. -->

<preference name="OpenAllExternalURLsInWebView"    value="true" />


<!-- Plugins can also be added here. -->
<gap:plugin name="org.apache.cordova.inappbrowser" />

<!-- Define app icon for each platform. -->

<!-- icons -->
<icon src="icons/ios/icon-57.png" /> <!-- default -->
<icon src="icons/ios/icon-29.png" gap:platform="ios" width="29" height="29" />
<icon src="icons/ios/icon-29_at_2x.png" gap:platform="ios" width="58" height="58" />
<icon src="icons/ios/icon-40.png" gap:platform="ios" width="40" height="40" />
<icon src="icons/ios/icon-40_at_2x.png" gap:platform="ios" width="80" height="80" />
<icon src="icons/ios/icon-50.png" gap:platform="ios" width="50" height="50" />
<icon src="icons/ios/icon-50_at_2x.png" gap:platform="ios" width="100" height="100" />
<icon src="icons/ios/icon-57.png" gap:platform="ios" width="57" height="57" />
<icon src="icons/ios/icon-57_at_2x.png" gap:platform="ios" width="114" height="114" />
<icon src="icons/ios/icon-60.png" gap:platform="ios" width="60" height="60" />
<icon src="icons/ios/icon-60_at_2x.png" gap:platform="ios" width="120" height="120" />
<icon src="icons/ios/icon-72.png" gap:platform="ios" width="72" height="72" />
<icon src="icons/ios/icon-72_at_2x.png" gap:platform="ios" width="144" height="144" />
<icon src="icons/ios/icon-76.png" gap:platform="ios" width="76" height="76" />
<icon src="icons/ios/icon-76_at_2x.png" gap:platform="ios" width="152" height="152" />
<icon src="icons/ios/icon-120.png" gap:platform="ios" width="120" height="120" />
<icon src="icons/ios/icon-120_at_2x.png" gap:platform="ios" width="240" height="240" />
<icon src="icons/ios/icon-512.png" gap:platform="ios" width="512" height="512" />
<icon src="icons/ios/icon-512_at_2x.png" gap:platform="ios" width="1024" height="1024" />

<!-- launch screens -->
<gap:splash gap:platform="ios" height="480" src="splash/ios/Default-iphone.png" width="320"  />
<gap:splash gap:platform="ios" height="960" src="splash/ios/Default-iphone_x2.png" width="640" />
<gap:splash gap:platform="ios" height="1136" src="splash/ios/splash-iphone5-538h.png" width="640" />
<gap:splash gap:platform="ios" height="1024" src="splash/ios/Default-iPad-Portrait.png" width="768" />
<gap:splash gap:platform="ios" height="2008" src="splash/ios/Default-iPad-Portrait_x2.png" width="1536" />
<gap:splash gap:platform="ios" height="768" src="splash/ios/Default-iPad-Landscape.png" width="1024" />
<gap:splash gap:platform="ios" height="1496" src="splash/ios/Default-iPad-Landscape_x2.png" width="2048" />

<access origin="*"/> <!-- allow local pages -->

<!-- plugins -->

 <feature name="StatusBar">
    <param name="ios-package" onload="true" value="CDVStatusBar" />
</feature>
<preference name="StatusBarOverlaysWebView" value="true" />
<preference name="StatusBarBackgroundColor" value="#000000" />


<feature name="InAppBrowser"> 
    <param name="ios-package" value="CDVInAppBrowser" /> 
</feature>

还有我的 index.html,目前:

<a href="http://google.com" target="_system">Google</a>
[...]
<script src="scripts/vendor.js"></script>

<script src="scripts/main.js"></script>

<script src="scripts/plugins.js"></script>

<script src="phonegap.js"></script>


<script>
document.addEventListener("deviceready", onDeviceReady, false);

// function onDeviceReady() {
//     StatusBar.hide();
// }

$('a').click(function(event) {
    event.preventDefault();
    window.open($(this).attr("href"), "_system");
});
</script>

我也试过这样标签看起来像:

<a href="#" onclick="window.open('http://google.com', '_system', 'location=yes');">Google</a>

结果相同:在应用中打开。

我可以看到插件存在于 myApp/plugins 中:

http://i.imgur.com/MKmafG4.png

我不知道如何从这里解决此问题。我什至没有得到人们说你应该使用 inappbrowser 得到的“完成”按钮,所以这让我觉得它不能正常工作。

非常感谢任何和所有帮助。

【问题讨论】:

    标签: javascript cordova inappbrowser


    【解决方案1】:

    好的,伙计们,这是对我有用的答案。唯一有效的东西。我已经尝试过所有其他存在的解决方案。就是这个。

    http://www.excellentwebworld.com/phonegap-open-a-link-in-safari-or-external-browser/

    那家伙的解决方案。不过,它的解释不是很好,所以如果您觉得它令人困惑,我将对其进行扩展。

    我首先尝试简单地搜索他正在谈论的文件,因为他没有告诉你它的位置。如果你这样做,你会因为某种原因得到一个笛卡尔结果,并且修改文件实际上不会起作用。因此,您可以通过以下方式真正做到这一点:

    当你在 XCode 中打开项目时,在左侧边栏文件列表(Project Navigator)中,

    1. 展开“CordovaLib.xcodeproj”
    2. 展开“类”
    3. 展开“命令”
    4. 文件是“CDVWebViewDelegate.m”

    command+f 查找“shouldstartloadwith”,您将找到该功能。以前的旧函数是这样开始的:

    (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
    {
        BOOL shouldLoad = YES;
    

    你要把它修改成这样:

    (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
    {
    
        if([[NSString stringWithFormat:@"%@",request.URL] rangeOfString:@"file"].location== NSNotFound)
        {
            [[UIApplication sharedApplication] openURL:[request URL]];
            return NO;
        }
        BOOL shouldLoad = YES;
    

    然后,使用:

    window.open('http://google.com', '_system');
    

    哇。 Adobe 确实需要更新他们的文档。

    【讨论】:

    • 嗨乔希,我有一个类似的问题,我正在测试世界上所有的解决方案,但没有,不幸的是我无法访问 iOS 代码并且 webapp 无法在 ipad 中运行,并且不是在新窗口中打开链接,纯JS而不是Objective-c代码中的任何解决方案?¿提前谢谢。
    • 感谢您的解决方案!我不得不稍微修改它以使其工作,增加了两个条件:[[NSString stringWithFormat:@"%@",request.URL] rangeOfString:@"meteor.local"].location == NSNotFound &amp;&amp; [[NSString stringWithFormat:@"%@",request.URL] rangeOfString:@"gap://"].location == NSNotFound)
    【解决方案2】:

    我尝试了所有方法,终于找到了解决方案。 在 Android 中完美的外部链接。

    低于 IOS 9 的版本可以正常工作,但在最新的 IOS 无法使用外部链接时,我的应用在后台打开了链接,似乎链接不起作用。

    问题在于 index.html 中关于内容安全策略的元标记有误

    将此添加到 index.html 解决我的问题:

    <meta http-equiv="Content-Security-Policy" content="img-src * 'self' data:; default-src * 'self' gap: wss: ws: ; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval';">
    

    我引用了解决我问题的帖子

    很抱歉之前没有回答。

    这不是错误,cordova-ios 应用程序需要 gap:CSP 上的协议 使插件工作。

    查看默认 Cordova 应用程序包含的 CSP,并附上注释 它解释了其中的一些,以及最重要的差距:

    在 iOS 10 上,CSP 变得更加严格并阻止插件工作 如果你不包括差距:

    > <!--
    >         Customize this policy to fit your own app's needs. For more guidance, see:
    >             https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
    >         Some notes:
    >             * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
    >             * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
    >             * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
    >                 * Enable inline JS: add 'unsafe-inline' to default-src
    >         -->
    >         <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com
    > 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src
    > 'self' data: content:;">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多