我在ios 8测试版中一直在使用icloud,CloudKitAtlasAnIntroductiontoCloudKit
示例项目非常有帮助。
https://developer.apple.com/library/prerelease/ios/samplecode/CloudAtlas/Introduction/Intro.html
但我想使用CKDiscoverAllContactsOperation
类,我无法在任何地方找到任何示例代码,在线文档也不是很有帮助。
https://developer.apple.com/library/prerelease/ios/documentation/CloudKit/Reference/CKDiscoverAllContactsOperation_class/index.html
如果有人设法成功使用CKDiscoverAllContactsOperation
,请帮助我指出正确的方向或展示如何调用它的工作示例?
我试过这个,看看我是否能从iCloud得到回复,但没有:
- (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的回复。
答案 0 :(得分:1)
这是我正在使用的。 self.container是CKContainer
中[CKContainer defaultContainer]
的{{1}}集。
init
答案 1 :(得分:1)
在使用CKDiscoverAllContactsOperation
操作之前,首先需要请求许可。
请使用方法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
}