gapi.client.load('youtube','v3')没有使用ios使用phonegap

时间:2016-10-15 06:49:01

标签: ios cordova phonegap-plugins youtube-data-api

我使用youtube构建应用程序并使用phonegap映射api。 使用Phonegap Developer app在设备上进行测试。

var API_KEY = xxxx;

$(document).ready(function () {
    $.getScript('https://apis.google.com/js/client.js?onload=handleClientLoad');
});

function handleClientLoad() {
    alert("msg 1");
    gapi.client.load('youtube', 'v3', function () {
        alert("msg 2");            
        $.getScript('https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&callback=handleMapsLoad&key=' + API_KEY);
    });
}
function handleMapsLoad() {
    alert("msg 3");

    geocoder = new google.maps.Geocoder();


}

Chrome模拟扩展程序上的一切正常。和Android操作系统:显示:msg 1. msg 2,msg 3。

但不适用于iOS,仅限msg 1 而不是msg 2,msg 3.并且错误消息:[phonegap] [console.log] Error in Success callbackId: Geolocation191878646 : ReferenceError: Can't find variable: google

p / s:ip 6s iOS 10.0.2,我查了一下,API_KEY不受限制。

感谢任何建议。

2 个答案:

答案 0 :(得分:2)

我已经搜索并测试了所有解决方案3周,但还没有找到解决方案。我发现问题是由PhoneGap Developer App引起的,而不是YouTube API或其他配置引起的。

只有当应用程序通过TestFlightiTunes(已发布的应用)安装在真实设备(而非模拟器)上时,问题才会得到解决。

原因: - gapi库使用iframe进行网络请求,PhoneGap Developer App iTunes(当前版本1.7.6)不允许使用iframe。< / p>

答案 1 :(得分:0)

我遇到了同样的问题。 已解决将以下指令添加到config.xml

<access origin="https://*.google.com/*" />
<access origin="https://*.googleapis.com/*" />
<allow-navigation href="https://*.google.com/*" />
<allow-navigation href="https://*.googleapis.com/*" />
内容安全策略的

wss:协议(index.html中的元标记),如下所示:

<meta http-equiv="Content-Security-Policy"
    content="default-src * gap://ready file: wss:;
        script-src 'self' 'unsafe-inline' 'unsafe-eval'
            127.0.0.1:*
            http://*.google.com
            http://*.googleapis.com
            https://*.google.com
            https://*.googleapis.com;
">

说明:您必须允许gapi.client.load()生成的iframe导航到Google API。