2011-06-06 168 views
0

我想複製像iPhone屏幕添加聯繫人。但我不想在默認電話簿中添加聯繫人,而是希望在我的應用中使用詳細信息。是否可以打開默認添加新的聯繫人屏幕並獲取所有數據?如果是,那麼如何?一個簡單的代碼片段將非常有幫助。這裏是添加聯繫人屏幕的圖像,以便更好地理解我的問題是否可以從本機應用程序打開addContactScreen?

Add New Contact

回答

1

你可以嘗試將聯繫人添加到地址簿,提取數據,然後從地址簿中刪除。這是一個相當簡單的過程。

我使用此功能將所有人員數據保存到我的應用程序中的核心數據。然後從地址簿中刪除該人。

+(void)savePersonDetails:(Person*)person{ 

ABAddressBookRef addressBook = ABAddressBookCreate(); 
ABRecordRef ref = ABAddressBookGetPersonWithRecordID(addressBook,[person.ID intValue]); 
ABMutableMultiValueRef multiPhones = ABRecordCopyValue(ref, kABPersonPhoneProperty); 

for (CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) { 
    NSString *phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(multiPhones, i); 
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiPhones, i); 
    NSString *phoneNumberLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 
    CFRelease(locLabel); 


    Phone *phone =(Phone*)[NSEntityDescription insertNewObjectForEntityForName:@"Phone" inManagedObjectContext:person.managedObjectContext]; 
    phone.number = phoneNumber; 
    phone.label = phoneNumberLabel; 
    phone.person = person; 
    [person addPhonesObject:phone]; 

    [person release]; 
    CFRelease(phoneNumber); 
    CFRelease(phoneNumberLabel); 

} 

    CFRelease(multiPhones); 


    ABMutableMultiValueRef multiEmail = ABRecordCopyValue(ref, kABPersonEmailProperty); 

for (CFIndex i = 0; i < ABMultiValueGetCount(multiEmail); i++) { 
    NSString *mail = (NSString*)ABMultiValueCopyValueAtIndex(multiEmail, i); 
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiEmail, i); 
    NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

    Mail *mailEntity =(Mail*)[NSEntityDescription insertNewObjectForEntityForName:@"Mail" inManagedObjectContext:person.managedObjectContext]; 
    mailEntity.mail = mail; 
    mailEntity.label = mailLabel; 
    mailEntity.person = person; 
    [person addMailsObject:mailEntity]; 

    CFRelease(locLabel); 
    [mail release]; 
    [mailLabel release]; 
} 
    CFRelease(multiEmail); 


    ABMultiValueRef streets = ABRecordCopyValue(ref, kABPersonAddressProperty); 
for (CFIndex j = 0; j<ABMultiValueGetCount(streets);j++){ 
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j); 
    CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(streets, j); 
    CFStringRef lbl = ABAddressBookCopyLocalizedLabel(typeTmp); 
    NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy]; 
    NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy]; 
    NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy]; 
    NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy]; 
    NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy]; 





    Address *addressEntity =(Address*)[NSEntityDescription insertNewObjectForEntityForName:@"Address" inManagedObjectContext:person.managedObjectContext]; 
    addressEntity.label = (NSString*)lbl; 
    addressEntity.street = street; 
    addressEntity.city = city; 
    addressEntity.state = state; 
    addressEntity.zip = zip; 
    addressEntity.country = country; 


    [street release]; 
    [city release]; 
    [state release]; 
    [zip release]; 
    [country release]; 
    CFRelease(dict); 
    CFRelease(lbl); 
    CFRelease(typeTmp); 
    addressEntity.person = person; 
    [person addAddressesObject:addressEntity]; 
} 
CFRelease(streets); 



ABMutableMultiValueRef multiURL = ABRecordCopyValue(ref, kABPersonURLProperty); 

for (CFIndex i = 0; i < ABMultiValueGetCount(multiURL); i++) { 

    NSString *url = (NSString*)ABMultiValueCopyValueAtIndex(multiURL, i); 
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiPhones, i); 
    NSString *urlLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel); 

    Url *urlEntity =(Url*)[NSEntityDescription insertNewObjectForEntityForName:@"Url" inManagedObjectContext:person.managedObjectContext]; 
    urlEntity.url = url; 
    urlEntity.label = urlLabel; 
    urlEntity.person = person; 
    [person addUrlsObject:urlEntity]; 



    CFRelease(locLabel); 
    [urlLabel release]; 
    [url release]; 
} 
    CFRelease(multiURL); 


ABAddressBookRemoveRecord(addressBook, ref, nil); 
ABAddressBookSave(addressBook, nil); 

CFRelease(addressBook); 

if (![person.managedObjectContext save:&error]) { 
    // Update to handle the error appropriately. 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    exit(-1); // Fail 
} 
    } 
+0

我也想顯示在我的app.Could你請你的代碼轉換爲簡單的插入 - >獲取添加新聯繫人屏幕 - > storeIntoOwnDb - > deleteFromAddressBook?我無法理解,我如何在自己的數據庫中管理多個電話和電子郵件字段?你能幫助這種方式嗎?像什麼應該是聯繫人的表格模式和所有相關的東西?當提取所有細節時,如何創建數據Modal? – 2011-06-06 08:39:27

+0

嗨拉胡爾。那麼你在問什麼是一個很好的答案。你將不得不在coreData上多學一點。我真的建議你這樣做,因爲它會爲你打開很多喧囂。同時我可以推薦你檢查sensibleTableView。他們有一個非常好的API來創建自己的表單,並以最低的技能將數據存儲在覈心數據中。在他們的網站查看。 – shannoga 2011-06-06 11:05:57

+0

我不想使用核心數據我想使用sqlite – 2011-06-06 14:33:15

相關問題