是否可以从本机应用程序打开 addContactScreen?

发布于 2024-11-14 04:00:46 字数 205 浏览 5 评论 0原文

我想复制添加联系人,就像 iPhone 的屏幕一样。但我不想在默认电话簿中添加联系人,而是想在我的应用程序中使用详细信息。是否可以打开默认添加新联系人屏幕并获取所有数据?如果是的话怎么办?一个简单的代码片段将非常有帮助。这是添加联系人屏幕的图像,以更好地理解我的问题

添加新联系人

I want to replicate add contact like screen iPhone has. But I don't want to add contact in default phonebook instead I want to use the details in my app. Is it possible to open default add new contact screen and get all the data? If yes then how? A simple code snippet will be very helpful. Here is an image of add contact screen to better understand my question

Add New Contact

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难愈 2024-11-21 04:00:46

您可以尝试将联系人添加到通讯录,拉取数据,然后从通讯录中删除。这是一个相当简单的过程。

我使用此函数将所有人员数据保存到我的应用程序中的核心数据中。然后从地址簿中删除该人。

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

You can try to add the contact to the address book, pull the data and then delete it from the address book. this is a fairly simple process.

I use this function to save all the person data to core data in my app. and then to delete the person from the addressBook.

  +(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
}
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文