Можно ли открыть addContactScreen из родного приложения? - PullRequest
0 голосов
/ 06 июня 2011

Я хочу повторить добавить контакт, как на экране iPhone. Но я не хочу добавлять контакт в телефонную книгу по умолчанию, вместо этого я хочу использовать детали своего приложения. Можно ли открыть по умолчанию добавить новый экран контактов и получить все данные? Если да, то как? Простой фрагмент кода будет очень полезен. Вот изображение экрана добавления контакта, чтобы лучше понять мой вопрос

Add New Contact

1 Ответ

1 голос
/ 06 июня 2011

Вы можете попытаться добавить контакт в адресную книгу, вытащить данные и затем удалить их из адресной книги. это довольно простой процесс.

Я использую эту функцию, чтобы сохранить все данные о человеке в основных данных в моем приложении. а затем удалить человека из адресной книги.

  +(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
}
  }
...