iOS中的gapi.client.init()时,undefined不是对象 - Ionic

时间:2017-05-09 16:59:02

标签: angular ionic-framework google-api gapi

我的离子应用程序上有这段代码,它在浏览器上工作正常但在设备中没有。在设备上我收到错误" undefined不是对象"当它到达gapi.client.init

在index.html中,我上传了

<script src="https://apis.google.com/js/api.js"></script> 

抛出错误的代码

  gapi.client.init({
        'apiKey': 'api',
        'discoveryDocs': [discoveryUrl],
        'clientId': 'clientId',
        'scope': SCOPE
    }).then(()  => { ;
      console.log("youtube: initialized");
      this.GoogleAuth = gapi.auth2.getAuthInstance();
      console.log(this.GoogleAuth);
this.GoogleAuth.isSignedIn.listen(this.updateSigninStatus.bind(this));

      // Handle initial sign-in state. (Determine if user is already signed in.)
      var user = this.GoogleAuth.currentUser.get();
      this.setSigninStatus();
    });

1 个答案:

答案 0 :(得分:0)

我猜默认安全设置阻止了cordova访问google api.js文件,因此gapi全局对象未定义。您可以尝试将类似<access origin="*"/>的内容添加到config.xml

“undefined不是对象”的另一个原因可能是加载了api.js,但是可以访问全局gapi对象需要一些时间。我使用以下代码

        var checkGapiInterval = setInterval(function(){
            if(typeof(gapi) == 'undefined'){
                console.log('gApi is undefined, will try later')
            } else if(typeof(gapi.client) == 'undefined'){
                console.log('gApi.client is undefined, will try later')
            } else {
                clearInterval(checkGapiInterval);
                gapi.client.load(that.API_NAME, that.API_VERSION, function(){
                    resolve();
                }, that.GAPI_ENDPOINT)
            }
        }, 1000)