ABAddressBookCreate deprecated

后端 未结 2 1726
独厮守ぢ
独厮守ぢ 2020-12-28 21:27

I\'m creating an application that will save a contact to the address book, but when I use ABAddressBookCreate to my code, it appear as a warning. I\'m coding in iOS 7.

相关标签:
2条回答
  • 2020-12-28 21:42

    Contact Frame work in iOS9: Can you plz see the following code

        -(void)contactsFromAddressBook{
    //ios 9+
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            //keys with fetching properties
            NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
            if (error) {
                NSLog(@"error fetching contacts %@", error);
            } else {
                NSString *phone;
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                UIImage *profileImage;
                NSMutableArray *contactNumbersArray;
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;
                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }
                    UIImage *image = [UIImage imageWithData:contact.imageData];
                    if (image != nil) {
                        profileImage = image;
                    }else{
                        profileImage = [UIImage imageNamed:@"person-icon.png"];
                    }
                    for (CNLabeledValue *label in contact.phoneNumbers) {
                      phone = [label.value stringValue];
                        if ([phone length] > 0) {
                           [contactNumbersArray addObject:phone];
                        }
                    }
                    NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
                   [self.contactsArray addObject:personDict];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableViewRef reloadData];
                });
            }
        }
    }];
    }
    
    0 讨论(0)
  • 2020-12-28 21:51

    Use ABAddressBookCreateWithOptions() instead.

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    

    More info on usage in the documentation here

    edit

    The AddressBook framework is deprecated in iOS 9 and replaced with the new and improved Contacts framework: https://developer.apple.com/library/ios/documentation/Contacts/Reference/Contacts_Framework/index.html#//apple_ref/doc/uid/TP40015328

    0 讨论(0)
提交回复
热议问题