简体   繁体   中英

objective-c Completion Block issue

I am trying to get a JSON (NSString *) of the address book , in async task , however I want it to be asynchronously and with a completion block .

I have managed to retrieve that easy without the completion block , however when I am adding the completion block I have the following compiler error :

Incompatible block pointer types passing NSString *(^)(bool, CFErrorRef)' to parameter of type 'ABAddressBookRequestAccessCompletionHandler' (aka 'void (^)(bool, CFErrorRef)')

here is my code

+ (NSString *) getAddressBook:(id (^)())block {
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

if (status == kABAuthorizationStatusDenied)
{
    // if you got here, user had previously denied/revoked permission for your
    // app to access the contacts, and all you can do is handle this gracefully,
    // perhaps telling the user that they have to go to settings to grant access
    // to contacts
    [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    return nil;
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (error)
{
    NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
    if (addressBook) CFRelease(addressBook);
    return nil;
}

if (status == kABAuthorizationStatusNotDetermined)
{
    // present the user the UI that requests permission to contacts ...
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) -> THE ERROR OCCURS HERE !
                                             {...

I think the problem is within my completion blocks , but I can't find some good example/tutorial for my specific issue I would be really glad if some 1 could help me please.

Your problem is that you are declaring getAddressBook like this(if we strip block)

-(id)block

And you are trying to pass it as a function of type:

-(void)block:(BOOL)aBool error:(CFErrorRef*)error

Blocks are quite hard to get used with syntax(which is improved in swift) however I'm always refering to this site when not sure:

block syntax reference

UPDATE:

You have to actually wait for your async method finish. You may also refer to official documentation

+ (void) getAddressBook:((^)(NSString* result))block {
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

    if (status == kABAuthorizationStatusDenied)
    {
        // if you got here, user had previously denied/revoked permission for your
        // app to access the contacts, and all you can do is handle this gracefully,
        // perhaps telling the user that they have to go to settings to grant access
        // to contacts
        [[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        block(nil);
    }
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    if (error)
    {
        NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
        if (addressBook) CFRelease(addressBook);
        block(nil);
    }

    if (status == kABAuthorizationStatusNotDetermined)
    {


        // present the user the UI that requests permission to contacts ...
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) -> THE ERROR OCCURS HERE !
                                                 {
                                                     NSString* result = whatever

                                                     block(result);
                                                 });
    }
}

And use it like this:

[YourClass getAddressBook:^(NSString* result)
 {
     //now you really have result
 }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM