任何适用于 IOS8 beta CloudKit 的 CKDiscoverAllContactsOperation 示例代码?
Posted
技术标签:
【中文标题】任何适用于 IOS8 beta CloudKit 的 CKDiscoverAllContactsOperation 示例代码?【英文标题】:Any working sample code for CKDiscoverAllContactsOperation for IOS8 beta CloudKit? 【发布时间】:2014-07-09 17:20:05 【问题描述】:我在 ios 8 beta 中一直在玩 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 的回应。
【问题讨论】:
【参考方案1】:这是我正在使用的。 self.container 是一个 CKContainer
集合,[CKContainer defaultContainer]
在 init
中。
-(void)queryForAllUsers: (void (^)(NSArray *records))completionHandler
CKDiscoverAllContactsOperation *op = [[CKDiscoverAllContactsOperation alloc] init];
[op setUsesBackgroundSession:YES];
op.queuePriority = NSOperationQueuePriorityNormal;
[op setDiscoverAllContactsCompletionBlock:^(NSArray *userInfos, NSError *error)
if (error)
NSLog(@"An error occured in %@: %@", NSStringFromSelector(_cmd), error);
//abort();
else
// NSLog(@"Number of records in userInfos is: %ld", (unsigned long)[userInfos count]);
dispatch_async(dispatch_get_main_queue(), ^(void)
completionHandler(userInfos);
);
];
[self.container addOperation:op];
【讨论】:
仍然无法正常工作。如果您发现任何关于此的信息,请告诉我。 确保将您的操作添加到队列中,例如:[self.container addOperation:op]; @BillJohnson 我在 userInfos 中得到了零。知道可能出了什么问题吗?我正在使用您在上面发布的相同代码。【参考方案2】:在您可以使用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
【讨论】:
以上是关于任何适用于 IOS8 beta CloudKit 的 CKDiscoverAllContactsOperation 示例代码?的主要内容,如果未能解决你的问题,请参考以下文章
错误域= NSURLErrorDomain代码= -1005“网络连接丢失。”
Cloudkit 身份验证仅适用于我的 alt 帐户,不适用于 Dev 或 Tester 帐户
在 iOS8 中: UIPopoverController presentPopoverFromRect 不再适用于 keyWindow
等效 [UIPopoverController setContentViewController:(UIViewController *) 动画:(BOOL)];适用于 iOS8