0

Apple の新しいフレームワークを使用して連絡先を読み込んでいます。

-(void)getAllContactsWithNewFrameworkOfApple {

    NSMutableArray *_contactsArray = [[NSMutableArray alloc]init];

    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusDenied) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can use contacts" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
        //        [self presentViewController:alert animated:TRUE completion:nil];
        return;
    }

    CNContactStore *store = [[CNContactStore alloc] init];

    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

        // make sure the user granted us access

        if (!granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // user didn't grant access;
                // so, again, tell user here why app needs permissions in order  to do it's job;
                // this is dispatched to the main queue because this request could be running on background thread
            });
            return;
        }

        NSError *fetchError;
        CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey,CNContactGivenNameKey,CNContactFamilyNameKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey]];

        BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {

            [_contactsArray addObject:contact];
            NSLog(@"I am %@", _contactsArray);

        }];

        if (!success) {
            NSLog(@"error = %@", fetchError);
        }else {

            NSLog(@"End with Success %@", _contactsArray);
            [self finalUsage:_contactsArray];

        }

    }];
 }

最終的にこの配列を使用してTableViewにロードする2番目の方法は...

-(void) finalUsage: (NSMutableArray*)_contactsArray {

items = [[NSMutableArray alloc] init];

for (CNContact *contact in _contactsArray) {

    _contact = [[Contacts alloc] init];

    _contact.contactNumber = [[NSMutableArray alloc] init];
    _contact.email = [[NSMutableArray alloc] init];


    NSLog(@"I am Given Name is %@", contact.givenName);
    NSLog(@"I am Given Name is %@", contact.familyName);


    _contact.fName = contact.givenName;
    _contact.lName = contact.familyName;


    for (CNLabeledValue * emailValue in contact.emailAddresses){
        NSString *emailString = emailValue.value;
        NSString *emailLabel = emailValue.label;

        NSLog(@"Contact is %@, %@", emailString, emailLabel);
        [_contact.email addObject:emailString];

    }

    NSLog(@"\n\n\n...");

    for (CNLabeledValue<CNPhoneNumber*> * phoneValue in contact.phoneNumbers){

        NSString *phoneString =phoneValue.value.stringValue;
        NSString *phoneLabel = phoneValue.label;

        NSLog(@"Contact is phone %@  %@", phoneString,phoneLabel);

        [_contact.contactNumber addObject:phoneString];

    }

    NSLog(@"\n\n\n");

    [items addObject:_contact];

    NSLog(@"no. of items in array %li",(unsigned long)items .count);


}

[self.tableData removeAllObjects];

 _tableData = items;

[self updateSectionTiles];

 _tableData = [self partitionObjects:_tableData collationStringSelector:@selector(fName)];

[_tableview_contacts setDelegate:self];
[_tableview_contacts setDataSource:self];

 [_tableview_contacts reloadData];

}

今ではエラーが発生します

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.


Stack:(
    0   CoreFoundation                      0x0000000183288f60 <redacted> + 148
    1   libobjc.A.dylib                     0x0000000197e3bf80 objc_exception_throw + 56
    2   CoreFoundation                      0x0000000183288e90 <redacted> + 0
    3   Foundation                          0x00000001842a72d8 <redacted> + 88
    4   Foundation                          0x00000001841294fc <redacted> + 36
    5   UIKit                               0x00000001889238c4 <redacted> + 64
    6   UIKit                               0x0000000188923b08 <redacted> + 548
    7   UIKit                               0x00000001889238d4 <redacted> + 80
    8   UIKit                               0x0000000188816a78 <redacted> + 240
    9   UIKit                               0x000000018881632c <redacted> + 116

    10  UIKit                               0x00000001888161b8 <redacted> + 504
    11  UIKit                               0x0000000188823b0c <redacted> + 1792
    12  UIKit                               0x0000000188b580e4 <redacted> + 88
    13  UIKit                               0x0000000188884e68 <redacted> + 364
    14  UIKit                               0x00000001888f8414 <redacted> + 304
    15  UIKit                               0x00000001888f7fd0 <redacted> + 296
    16  UIKit                               0x000000018890429c <redacted> + 720
    17  UIKit                               0x0000000188903e74 <redacted> + 212
    18  UIKit                               0x000000018890387c <redacted> + 1920
    19  ProjectDemo                           0x000000010014c61c -[InviteViewController finalUsage:] + 2900
    20  ProjectDemo                           0x000000010014b81c __61-[InviteViewController getAllContactsWithNewFrameworkOfApple]_block_invoke + 668
    21  Contacts                            0x0000000182cbe700 <redacted> + 72
    22  libdispatch.dylib                   0x0000000100bedca8 _dispatch_call_block_and_release + 24
    23  libdispatch.dylib                   0x0000000100bedc68 _dispatch_client_callout + 16
    24  libdispatch.dylib                   0x0000000100bfcec8 _dispatch_root_queue_drain + 2344
    25  libdispatch.dylib                   0x0000000100bfc590 _dispatch_worker_thread3 + 132
    26  libsystem_pthread.dylib             0x000000019886d470 _pthread_wqthread + 1092
    27  libsystem_pthread.dylib             0x000000019886d020 start_wqthread + 4
    )

また、Googleを使用したエラーの深いアプローチは

2015-10-24 13:15:53.079 ProjectDemo[1653:290082] WARNING: GoogleAnalytics 3.10 void GAIUncaughtExceptionHandler(NSException *) (GAIUncaughtExceptionHandler.m:49): Uncaught exception: *** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

ブロックが終了していること、または単純なコンパイルハンドラーであることをどのように知ることができますか?

ブロックの最後の行でメソッドを呼び出すと、ブロックが終了する前に呼び出されます。

ガイダンスの助けはありますか?

4

2 に答える 2