Phonegap 3.5.0外部Safari窗口中的打开链接不起作用

Phonegap 3.5.0 open link in external Safari window not working

本文关键字:链接 不起作用 窗口 外部 Safari Phonegap      更新时间:2023-09-26

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

"防止违约"解决方案,"删除冲突状态栏隐藏插件"解决方案,"_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:中看到插件

https://i.stack.imgur.com/pcAVX.png

我不知道该从哪里着手解决这个问题。我甚至没有得到人们说你应该用inapp浏览器得到的"完成"按钮,所以这让我觉得它工作不正常。

非常感谢所有的帮助。

好吧,伙计们,这是对我有效的答案。唯一有效的方法。我几乎尝试过所有其他存在的解决方案。就是这个。

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

那家伙的解决方案。不过,它并没有很好地解释,所以如果你觉得它令人困惑,我会详细介绍它。

我试着先简单地搜索他所说的文件,因为他不会告诉你它的位置。如果这样做,由于某种原因,您将得到笛卡尔结果,并且修改文件实际上不会起作用。下面是你如何真正做到这一点:

当您在XCode中打开项目时,在左侧边栏文件列表(项目导航器)中,

  1. 展开"CordovaLib.xcodeproj
  2. 展开"类"
  3. 展开"命令"
  4. 文件为"CDVWebViewDelegate.m"

命令+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');

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

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

低于IOS9的版本可以正常工作,但在新闻中,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应用程序需要间隙: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:;">