我的所有代码都在模拟器中完美运行。显示联系人选择器,当按下联系人时,在获得联系人数据后将其解除。但是,当我在我的设备上尝试此操作时,选择器不会被解除,而是显示所选联系人的详细信息。从那里你可以按下各个属性,如数字和地址,但那些只是转移到相关的应用程序。
用户按下以导入联系人的按钮:
- (IBAction)AddContactPressed:(id)sender {
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
contactsAccessible = TRUE;
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
contactsAccessible = TRUE;
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
//Test if contacts have been enabled or not
if (contactsAccessible) {
//Display contact selection screen
addressBookController = [[ABPeoplePickerNavigationController alloc] init];
addressBookController.peoplePickerDelegate = self;
[self presentViewController:addressBookController animated:YES completion:nil];
}
else{
//Display text saying the contacts could not be accessed and provide a button to ask again
}
}
以下是联系人选择器的所有代码:
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
//Creating dictionary to store contact info
NSMutableDictionary *contactInfoDict = [[NSMutableDictionary alloc]
initWithObjects:@[@"", @"", @"", @""]
forKeys:@[@"firstName", @"lastName", @"mobileNumber", @"homeNumber"]];
//First name
CFTypeRef generalCFObject;
generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge_transfer NSString *)generalCFObject forKey:@"firstName"];
CFRelease(generalCFObject);
}
//Last name
generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge_transfer NSString *)generalCFObject forKey:@"lastName"];
CFRelease(generalCFObject);
}
//Phone numbers: (home and mobile)
ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {
CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i);
CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);
if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
[contactInfoDict setObject:(__bridge_transfer NSString *)currentPhoneValue forKey:@"mobileNumber"];
}
if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
[contactInfoDict setObject:(__bridge_transfer NSString *)currentPhoneValue forKey:@"homeNumber"];
}
CFRelease(currentPhoneLabel);
CFRelease(currentPhoneValue);
}
CFRelease(phonesRef);
//Getting image if contact has image
if (ABPersonHasImageData(person)) {
NSData *contactImageData = (__bridge_transfer NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
[contactInfoDict setObject:contactImageData forKey:@"image"];
}
//Add contact to array
if (contacts == nil) {
contacts = [[NSMutableArray alloc] init];
}
[contacts addObject:contactInfoDict];
//Save contact
[userDefaults setObject:contacts forKey:@"Contacts"];
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:1)
原因可能是委托无法调用委托方法。这可能会导致您在模拟器和iPhone上使用不同的iOS。
我最好猜测你在模拟器上使用iOS 8,而在iOS 8下,选择器有一个新的委托方法
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
....// do whatever you need here
同时旧的就是你正在使用的。
答案 1 :(得分:0)
来自文档:
peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:
返回值
是执行所选属性的操作并关闭选择器。
不显示拣货员中的人。
所以这可能是个问题。在模拟器上,第一个方法在设备上被调用而另一个方法被调用。因此,您应该处理两种方法中的选择,并从上述方法中返回YES。