2017-04-17 56 views
0

我試圖讀取使用CloudKit從他們的iCloud帳戶的用戶名,如下(CKError代碼6)在CKContainer requestApplicationPermission

CKContainer *container = [CKContainer defaultContainer]; 
[container accountStatusWithCompletionHandler:^(CKAccountStatus accountStatus, NSError * _Nullable error) { 
    if (accountStatus == CKAccountStatusAvailable) { 
     [container requestApplicationPermission:CKApplicationPermissionUserDiscoverability completionHandler:^(CKApplicationPermissionStatus applicationPermissionStatus, NSError * _Nullable error) { 
      if (applicationPermissionStatus == CKApplicationPermissionStatusGranted) { 
       [container fetchUserRecordIDWithCompletionHandler:^(CKRecordID * _Nullable recordID, NSError * _Nullable error) { 
        if (!error && recordID) { 
         [container discoverUserIdentityWithUserRecordID:recordID completionHandler:^(CKUserIdentity * _Nullable userInfo, NSError * _Nullable error) { 
          NSLog(@"UserInfo: %@ Error: %@", userInfo, error); 
         }]; 
        } 
       }]; 
      } 
     }]; 
    } 
}]; 

但每次requestApplicationPermission服務不可用錯誤失敗,服務不可用錯誤和狀態是CKApplicationPermissionStatusCouldNotComplete。我不知道我在這裏做錯了什麼。我用多個設備和帳戶嘗試過,但結果仍然相同。我試過在一些地方建議禁用和啓用CloudKit功能,但沒有運氣。是否有任何信息需要配置我缺少的信息。下面是我的iCloud功能的外觀

iCloud Capability Configuration

+0

您是否在您的設備iCloud設置中啓用了iCloud Drive? FYI'fetchUserRecordIDWithCompletionHandler'不會給你一個用戶名,只是一個不透明的標識符 – Paulw11

+0

@ Paulw11:我有用戶登錄到iCloud和iCloud Drive啓用。曾見過關於SO的建議。另外,從'fetchUserRecordIDWithCompletionHandler'獲取標識符後,我期待'discoverUserIdentityWithUserRecordID'的用戶名。 – execv

回答

0

那麼事實證明,我需要調用requestApplicationPermission之前調用statusForApplicationPermission:。這真是令人困惑,因爲我沒有檢查權限狀態,權限請求失敗。這也與Service UnAvailable錯誤。下面是可用的代碼(不是很優雅):

CKContainer *container = [CKContainer defaultContainer]; 
[container accountStatusWithCompletionHandler:^(CKAccountStatus accountStatus, NSError * _Nullable error) { 
    if (accountStatus == CKAccountStatusAvailable) { 
     [container statusForApplicationPermission:CKApplicationPermissionUserDiscoverability completionHandler:^(CKApplicationPermissionStatus applicationPermissionStatus, NSError * _Nullable error) { 
      if (applicationPermissionStatus==CKApplicationPermissionStatusInitialState || applicationPermissionStatus==CKApplicationPermissionStatusGranted) { 
       [container requestApplicationPermission:CKApplicationPermissionUserDiscoverability completionHandler:^(CKApplicationPermissionStatus applicationPermissionStatus, NSError * _Nullable error) { 
        if (applicationPermissionStatus == CKApplicationPermissionStatusGranted) { 
         [container fetchUserRecordIDWithCompletionHandler:^(CKRecordID * _Nullable recordID, NSError * _Nullable error) { 
          if (!error && recordID) { 
           [container discoverUserIdentityWithUserRecordID:recordID completionHandler:^(CKUserIdentity * _Nullable userInfo, NSError * _Nullable error) { 
            NSLog(@"UserInfo: %@ Error: %@", userInfo, error); 
           }]; 
          } 
         }]; 
        } 
       }]; 
      } 
     }]; 
    } 
}];