0

iPhoneの画面のように連絡先を追加したい。しかし、デフォルトの電話帳に連絡先を追加したくない代わりに、アプリで詳細を使用したいと考えています。デフォルトの新規連絡先追加画面を開いてすべてのデータを取得することはできますか? はいの場合、どのように?シンプルなコード スニペットが非常に役立ちます。これは、私の質問をよりよく理解するための連絡先追加画面の画像です

新しい連絡先を追加

4

1 に答える 1

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
}
  }
于 2011-06-06T08:27:08.243 に答える