您如何仅从iOS联系人中检索select属性? [英] How do you retrieve ONLY the select property from your iOS contacts?
问题描述
我们遇到了一个问题,即某些设备上的联系人数据库返回了错误的选择。有没有一种方法可以修改以下代码,使其仅从人员联系信息中返回所选项目?在这种情况下,我们希望返回从人员联系人中选择的确切电子邮件地址。
在此先感谢您的帮助!这里是代码...
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person属性:( ABPropertyID)属性标识符:(ABMultiValueIdentifier)标识符{
NSMutableDictionary * contactInfoDict = [[NSMutableDictionary alloc]
initWithObjects:@ [@@,@,@]
forKeys:@ [@ first_name,@ last_name,@ email]];
//使用常规的Core Foundation对象。
CFTypeRef generalCFObject = ABRecordCopyValue(person,kABPersonFirstNameProperty);
//获取名字。
if(generalCFObject){
[contactInfoDict setObject:(__ bridge NSString *)generalCFObject forKey:@ first_name];
CFRelease(generalCFObject);
}
//获取姓氏。
generalCFObject = ABRecordCopyValue(person,kABPersonLastNameProperty);
if(generalCFObject){
[contactInfoDict setObject:(__ bridge NSString *)generalCFObject forKey:@ last_name];
CFRelease(generalCFObject);
}
//加载选定的电子邮件地址
if(属性== kABPersonEmailProperty){
ABMultiValueRef emails = ABRecordCopyValue(person,property);
CFIndex ix = ABMultiValueGetIndexForIdentifier(电子邮件,标识符);
CFStringRef email = ABMultiValueCopyValueAtIndex(emails,ix);
emailAccount =(__bridge NSString *)(电子邮件);
[contactInfoDict setObject:emailAccount forKey:@ email];
如果(通过电子邮件发送)CFRelease(通过电子邮件发送);
,如果(发送电子邮件)CFRelease(发送电子邮件);
}
//初始化数组
if(_arrContactsData == nil){
_arrContactsData = [[NSMutableArray alloc] init];
}
//将联系人发送到服务器
[self sendContact:contactInfoDict];
//设置联系人数据
[_arrContactsData addObject:contactInfoDict];
//重新加载表格视图数据。
[self.myTable reloadData];
//关闭通讯录视图控制器。
[_addressBookController dismissViewControllerAnimated:YES完成:无];
返回否;
}
您的代码正确。 / p>
用户是否有使用iOS 8遇到问题的机会?我在iOS 8 beta 6中发现了一个错误。如果您:
-
使用电子邮件地址创建联系人:
-
编辑联系人,添加电子邮件地址并删除其他电子邮件地址之一:
(请注意,不仅要编辑其中一个电子邮件地址,还要添加一个新的电子邮件地址并删除旧的电子邮件地址。)
-
在iOS 7中,如果您遍历电子邮件地址,则会看到:
{
标识符= 1;
索引= 0;
label = _ $!< Work>!$ _;
value = work@gmail.com;
},
{
标识符= 2;
索引= 1;
label = _ $!< Other>!$ _;
value = other@gmail.com;
}
这是正确的。 (当首次添加联系人时,当我添加其他电子邮件地址时,标识符分别为
0
和1
,得到的标识符为2
;当我删除家庭电子邮件地址时,标识符为0 $ c $的电子邮件地址c>已删除,但其他标识符没有更改。)因此,如果您点击工作电子邮件地址,
shouldContinueAfterSelectingPerson
将返回1
的ABMultiValueIdentifier0的
,它将正确返回工作电子邮件地址。CFIndex
在iOS 8 Beta 6中,
ABMultiValueRef
错误地表示为:{
identifier = 0;
索引= 0;
label = _ $!< Work>!$ _;
value = work@gmail.com;
},
{
标识符= 1;
索引= 1;
label = _ $!< Other>!$ _;
value = other@gmail.com;
}
这些标识符不正确。更糟糕的是,如果您点击工作电子邮件地址,
didSelectPerson
(将替换已弃用的shouldContinueAfterSelectingPerson
)将返回一个1
中的ABMultiValueIdentifier
(这应该是本来的……看起来此委托方法返回了相应的identifier
,但是此已编辑记录的ABRecordRef
具有错误的标识符...不确定它们是如何做到的!) ,转换为1
的CFIndex
,它将正确返回其他电子邮件地址(!)。 / p>
更糟糕的是,如果您点击其他电子邮件地址,则
didSelectPerson
将返回2
的ABMultiValueIdentifierCFIndex
,您将获得-1
(因为不存在这样的ABMultiValueIdentifier
),并且因为该应用没有请检查是否尝试使用<$ c $具有该无效索引的c> ABMultiValueCopyValueAtIndex 会导致崩溃!
此错误似乎固定在iOS8 GM种子中,因此它似乎只是一个beta问题。
we're having an issue where incorrect selections are being returned from the contacts database on some devices. Is there a way to modify the below code to only return the selected item from a persons contact information? In this case we want the exact selected email address from a persons contact to be returned.
Thanks in advance for the help! Heres the code...
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property: (ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
NSMutableDictionary *contactInfoDict = [[NSMutableDictionary alloc]
initWithObjects:@[@"", @"", @""]
forKeys:@[@"first_name", @"last_name", @"email"]];
// Use a general Core Foundation object.
CFTypeRef generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);
// Get the first name.
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:@"first_name"];
CFRelease(generalCFObject);
}
// Get the last name.
generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (generalCFObject) {
[contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:@"last_name"];
CFRelease(generalCFObject);
}
// Load selected email addre'
if (property == kABPersonEmailProperty) {
ABMultiValueRef emails = ABRecordCopyValue(person, property);
CFIndex ix = ABMultiValueGetIndexForIdentifier(emails, identifier);
CFStringRef email = ABMultiValueCopyValueAtIndex(emails, ix);
emailAccount = (__bridge NSString *)(email);
[contactInfoDict setObject:emailAccount forKey:@"email"];
if (email) CFRelease(email);
if (emails) CFRelease(emails);
}
// init array
if (_arrContactsData == nil) {
_arrContactsData = [[NSMutableArray alloc] init];
}
// Send contact to server
[self sendContact:contactInfoDict];
// Set data for contacts
[_arrContactsData addObject:contactInfoDict];
// Reload the table view data.
[self.myTable reloadData];
// Dismiss the address book view controller.
[_addressBookController dismissViewControllerAnimated:YES completion:nil];
return NO;
}
Your code is correct.
Are the users experiencing the problem using iOS 8, by any chance? I discovered a bug in iOS 8 beta 6. If you:
Create a contact with email addresses:
Edit the contact, adding an email address and deleting one of the other email addresses:
(Note, don't just edit one of the email addresses, but rather add a new email address and remove and old one.)
In iOS 7, if you iterate through the email addresses, you will see:
{ identifier = 1; index = 0; label = "_$!<Work>!$_"; value = "work@gmail.com"; }, { identifier = 2; index = 1; label = "_$!<Other>!$_"; value = "other@gmail.com"; }
This is correct. (When the contact was first created, the identifiers were
0
and1
, when I add the "other" email address, that gets an identifier of2
; when I delete the "home" email address, the email address with the identifier of0
is deleted, but the other identifiers are correctly unaltered.) Thus, if you tap on the "work" email address,shouldContinueAfterSelectingPerson
will return anABMultiValueIdentifier
of1
, which translates to aCFIndex
of0
, which will correctly return the "work" email address.In iOS 8 Beta 6, however, the
ABMultiValueRef
is incorrectly represented as:{ identifier = 0; index = 0; label = "_$!<Work>!$_"; value = "work@gmail.com"; }, { identifier = 1; index = 1; label = "_$!<Other>!$_"; value = "other@gmail.com"; }
These identifiers are incorrect. Worse, if you tap on the "work" email address,
didSelectPerson
(which replaces the deprecatedshouldContinueAfterSelectingPerson
) will return aABMultiValueIdentifier
of1
(which is what it should have been ... it looks like this delegate method returns the appropriateidentifier
, but that theABRecordRef
for this edited record has the wrong identifiers ... not sure how they did that!), which translates to aCFIndex
of1
, which will correctly return the "other" email address (!).Worse, if you tapped on the "other" email address,
didSelectPerson
will return aABMultiValueIdentifier
of2
(which is what it should have been), but if you try to retrieve theCFIndex
, you'll get-1
(because no suchABMultiValueIdentifier
exists) and because the app doesn't check for this, any attempt to useABMultiValueCopyValueAtIndex
with this invalid index will result in crash!
This bug seems to be fixed in the iOS8 GM seed, so it appears to be a beta problem only.
这篇关于您如何仅从iOS联系人中检索select属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!