Objective c 联系人选择器在模拟器中工作,但在实际的iPhone上不工作
我所有的代码都能在模拟器中完美工作。显示触点选择器,当按下触点时,在获得触点数据后将其解除。但是,当我在我的设备上尝试此操作时,选择器不会被解除,而是显示所选联系人的详细信息。从那里,你可以按各个属性,如号码和地址,但这些只是将你转移到相关的应用程序 用户为导入联系人而按下的按钮:Objective c 联系人选择器在模拟器中工作,但在实际的iPhone上不工作,objective-c,contacts,addressbook,ios8,xcode6,Objective C,Contacts,Addressbook,Ios8,Xcode6,我所有的代码都能在模拟器中完美工作。显示触点选择器,当按下触点时,在获得触点数据后将其解除。但是,当我在我的设备上尝试此操作时,选择器不会被解除,而是显示所选联系人的详细信息。从那里,你可以按各个属性,如号码和地址,但这些只是将你转移到相关的应用程序 用户为导入联系人而按下的按钮: - (IBAction)AddContactPressed:(id)sender { ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(
- (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];
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)在选择person:(ABRecordRef)person后,peoplePickerNavigationController应继续{
//创建字典以存储联系人信息
NSMutableDictionary*contactInfoDict=[[NSMutableDictionary alloc]
initWithObjects:@[@”“@”“@”“@”“]
福克斯:@[@“firstName”、@“lastName”、@“mobileNumber”、@“homeNumber”];
//名字
CFTypeRef generalCFObject;
generalCFObject=ABRecordCopyValue(person、KabbersonfirstNameProperty);
if(通用对象){
[contactInfoDict setObject:(uu bridge_transfer NSString*)generalCFObject forKey:@“firstName”];
CFRelease(generalCFObject);
}
//姓
generalCFObject=ABRecordCopyValue(person、kABPersonLastNameProperty);
if(通用对象){
[contactInfoDict setObject:(uu bridge_transfer NSString*)generalCFObject forKey:@“lastName”];
CFRelease(generalCFObject);
}
//电话号码:(家庭和手机)
ABMultiValueRef phonesRef=ABRecordCopyValue(person,kABPersonPhoneProperty);
for(int i=0;i
来自文档:
peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier:
返回值
选择“是”可对所选特性执行操作并解除选取器
否以显示选择器中的人员
所以这可能就是问题所在。在模拟器上,第一个方法在另一个设备上被调用。因此,您应该处理这两种方法中的选取,并从上述方法返回YES。原因可能是委托无法调用委托方法。这可能会导致您在模拟器和iPhone上使用不同的iOS 我的最佳猜测是,您在模拟器上使用ios8,而在ios8下,有一个新的用于选择器的委托方法
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
....// do whatever you need here
同时,您正在使用的是旧的。这不起作用。我以前尝试过改变返回类型,但没有改变模拟器或设备。