问题描述
我一直在ios 8 beta中使用icloud,并且 CloudKitAtlasAnIntroductiontoCloudKit
示例项目非常有用。
I have been playing with icloud in the ios 8 beta, and the CloudKitAtlasAnIntroductiontoCloudKit
sample project has been very helpful.https://developer.apple.com/library/prerelease/ios/samplecode/CloudAtlas/Introduction/Intro.html
但是我想使用 CKDiscoverAllContactsOperation
类,我根本无法在任何地方找到任何示例代码,并且在线文档不是很有帮助。
But I wanted to use the CKDiscoverAllContactsOperation
class and I cannot find any sample code for it anywhere at all and the online documentation is not very helpful. https://developer.apple.com/library/prerelease/ios/documentation/CloudKit/Reference/CKDiscoverAllContactsOperation_class/index.html
如果有人成功使用 CKDiscoverAllContactsOperation
您能帮我指出正确的方向还是显示如何调用它的工作示例吗?
If anyone has managed to successfully use CKDiscoverAllContactsOperation
could you please help point me in the right direction or show a working example of how it should be called?
这可以查看我是否甚至可以从iCloud得到响应,但什么也没得到:
I have tried this to see if I could even get an response from iCloud but nothing:
- (void)queryForRecordsOtherUsersInAddressBookcompletionHandler:(void (^)(NSArray *records))completionHandler {
CKDiscoverAllContactsOperation *discoverAllContactsOperation= [[CKDiscoverAllContactsOperation alloc] init];
[discoverAllContactsOperation setContainer:_container];
NSMutableArray *results = [[NSMutableArray alloc] init];
discoverAllContactsOperation.discoverAllContactsCompletionBlock = ^(NSArray *userInfos, NSError *operationError) {
[results addObjectsFromArray:userInfos];
};
discoverAllContactsOperation.discoverAllContactsCompletionBlock=^(NSArray *userInfos, NSError *operationError){
if (operationError) {
// In your app, handle this error with such perfection that your users will never realize an error occurred.
NSLog(@"An error occured in %@: %@", NSStringFromSelector(_cmd), operationError);
abort();
} else {
dispatch_async(dispatch_get_main_queue(), ^(void){
completionHandler(results);
});
}
};
}
并与此通话...
[self.cloudManager queryForRecordsOtherUsersInAddressBookcompletionHandler:^(NSArray *records ) {
if (records.count==0){
NSLog(@"Login name not found");
return;
}
//self.results= records;
//_loggedInRecord = self.results[0];
//NSLog(@"%@,%@",_loggedInRecord[@"lastName"],_loggedInRecord[@"firstName"]);
// [self performSegueWithIdentifier:@"loggedInSegue" sender:self ];
}];
我知道代码不应该做任何事情。再次,我只是在寻找iCloud的响应。
I know the code shouldn't really do anything. Again I was just looking for a response from iCloud.
推荐答案
在可以使用 CKDiscoverAllContactsOperation之前
操作,您首先需要请求权限。
Before you can use the CKDiscoverAllContactsOperation
operation, you first need to request for permission.
请使用方法 requestApplicationPermission:completion:
func discoverAllContacts() {
let container = CKContainer.defaultContainer()
//Request for user permission
container.requestApplicationPermission([.UserDiscoverability]) { [weak self] status, error in
switch status {
case .Granted where error == nil:
let operation = self?.discoverAllContactsOperation { usersInfo in
//do something here
}
if let operationExists = operation {
//Assuming there is a NSOperationQueue property called operationQueue
self?.operationQueue.addOperation(operationExists)
}
default:
break
}
}
}
func discoverAllContactsOperation(completionHandler: ([CKDiscoveredUserInfo]?) -> ()) -> NSOperation {
let operation = CKDiscoverAllContactsOperation()
operation.discoverAllContactsCompletionBlock = { usersInfo, error in
if error == nil {
print("Discoverd all contacts = \(usersInfo)")
completionHandler(usersInfo)
}
else {
print("Discoverd all contacts error = \(error)")
completionHandler(nil)
}
}
return operation
}
这篇关于IOS8 beta CloudKit的CKDiscoverAllContactsOperation的任何有效示例代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!