0

这就是我在加载网站时所说的:

(function($){})(window.jQuery);

$(document).ready(function () {

  window.addEventListener('cloudkitloaded', function() {

    CloudKit.configure({
      containers: [{
        containerIdentifier: 'iCloud.pl.blueworld.fieldservice',
        apiToken: 'fc363369412ad6c99020e7e0ed3e2be121008e34534cff7debe1f4f7f829d152',
        environment: 'production'
      }]
    });

    var container = CloudKit.getDefaultContainer();
    var privateDB = container.privateCloudDatabase;

    this.gotoAuthenticatedState = function(userInfo) {

      if(userInfo.isDiscoverable) {
        //success
      } else {
        //failure
      }

      container
      .whenUserSignsOut()
      .then(this.gotoUnauthenticatedState);
    };

    this.gotoUnauthenticatedState = function(error) {
      //do something of sign out

      container
      .whenUserSignsIn()
      .then(this.gotoAuthenticatedState)
      .catch(this.gotoUnauthenticatedState);
    };

    container.setUpAuth().then(function(userInfo) {

      if(userInfo) {
          this.gotoAuthenticatedState(userInfo);
      } else {
          this.gotoUnauthenticatedState();
      }
    });
  });
});

当我成功登录时,会出现正确的按钮:

在此处输入图像描述

但是每次刷新页面时,此按钮都会更改:

在此处输入图像描述

在用户手动注销之前如何保持会话和获取记录的可能性?

如何检查是否存在当前会话(用户已经通过身份验证)并使用它?

4

1 回答 1

0

将您的令牌放入一个apiTokenAuth对象中,并添加一个persist密钥。

CloudKit.configure({
    containers: [{
        containerIdentifier: '<your container>',
        environment: 'production',

        apiTokenAuth: {
            apiToken: '<your token>',
            persist: true
        }
    }]
});
于 2017-09-23T00:52:39.867 回答