Any working sample code for CKDiscoverAllContactsOperation for IOS8 beta CloudKit?

那年仲夏 提交于 2019-12-10 18:01:16

问题


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

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

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?

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);
        });

    }
};


}

and calling with this...

 [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 ];

            }];

I know the code shouldn't really do anything. Again I was just looking for a response from iCloud.


回答1:


Here is what I am using. self.container is a CKContainer set with [CKContainer defaultContainer] in the 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];
}



回答2:


Before you can use the CKDiscoverAllContactsOperation operation, you first need to request for permission.

Pls use the method 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
}


来源:https://stackoverflow.com/questions/24660018/any-working-sample-code-for-ckdiscoverallcontactsoperation-for-ios8-beta-cloudki

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!