2011-04-15 32 views
1

這是非常特殊的情況。我相信有人已經在某個地方解決了這個問題,但要找到它並不容易。如何使用ABUnknownPersonViewController與生成intially輸入的數據?

的情況:

1)一個對象將返回NSString對象的名稱地址1,地址,電話:

[anObject name]; 
[anObject address1]; 
[anObject address2]; 
[anObject name]; 

2)我想用這些對象與最初輸入的值準備ABUnknownPersonViewController ,所以用戶在將它們保存在地址簿中之前不必輸入它們。

我已經看過IOS文件,並通過谷歌和StackOverflow上搜索,找不到這種簡單的情況下正確的答案。

任何人都可以指導我嗎?

回答

2

找到了答案:它很好地記錄在iOS開發庫: http://developer.apple.com/library/ios/#samplecode/QuickContacts/Listings/Classes_QuickContactsViewController_m.html#//apple_ref/doc/uid/DTS40009475-Classes_QuickContactsViewController_m-DontLinkElementID_6

這裏是一個示例代碼,我實現返回一個ABPersonRecordRef作爲對象。我遇到的錯誤與返回後未保留ABPersonRecordRef對象有關。

- (id)personRecordUsingModelObj:(id)modelObj { 
    ABRecordRef aContact = ABPersonCreate(); 
    CFErrorRef anError = NULL; 

    NSString *name = [NSString stringWithFormat:@"%@", [modelObj name]]; 
    ABRecordSetValue(aContact, kABPersonOrganizationProperty, name, &anError); 

    ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
    ABMultiValueAddValueAndLabel(phone, [modelObj phone], kABPersonPhoneMainLabel, NULL); 

    ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError); 
    CFRelease(phone); 

    NSString *address = [NSString stringWithFormat:@"%@ %@", [modelObj addr1], [modelObj addr2]]; 
    NSMutableDictionary *dictionaryAddress = [[NSMutableDictionary alloc] initWithCapacity:0]; 
    [dictionaryAddress setObject:address forKey:(NSString *)kABPersonAddressStreetKey]; 
    [dictionaryAddress setObject:@"us" forKey:(NSString *)kABPersonAddressCountryCodeKey]; 

    ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType); 
    ABMultiValueAddValueAndLabel(address, dictionaryAddress, kABPersonAddressStreetKey, NULL); 
    [dictionaryAddress release]; 

    ABRecordSetValue(aContact, kABPersonAddressProperty, address, &anError); 
    CFRelease(address); 

    if (anError) { 
     aContact = nil; 
    } 

    [(id)aContact autorelease]; 

    return (id)aContact; 
} 
相關問題