开发者

Saving contact details to existing contact using GTMABAddressBook

开发者 https://www.devze.com 2023-02-24 07:33 出处:网络
Using Google Toolbox for Mac I am trying to add contact details to an existing contact on my iPhone upon selecting it in a ABPeoplePickerNavigationController. No errors are reported by GTMABAddressBoo

Using Google Toolbox for Mac I am trying to add contact details to an existing contact on my iPhone upon selecting it in a ABPeoplePickerNavigationController. No errors are reported by GTMABAddressBook as I confirmed by stepping through the process in the debugger, yet the details don't show up in the Contacts app.

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)personRef {
    GTMABPerson *record = [GTMABPerson recordWithRecord:personRef];
    GTMABPerson *person = [[GTMABAddressBook addressBook] personForId:[record recordID]];

    GTMABMutableMultiValue *addresses = [[[person valueForProperty:kABPersonAddressProperty] mutableCopy] autorelease];
    if (!addresses) {
        addresses = [GTMABMutableMultiValue valueWithPropertyType:kABMultiDictionaryPropertyType];
    }

    NSMutableDictionary *address = [NSMutableDictionary dictionary];
    [address setObject:@"Street" forKey:(NSStrin开发者_如何学编程g *)kABPersonAddressStreetKey];
    [address setObject:@"City" forKey:(NSString *)kABPersonAddressCityKey];
    [address setObject:@"Country" forKey:(NSString *)kABPersonAddressCountryKey];
    [addresses addValue:address withLabel:(CFStringRef)@"Label"];

    [person setValue:addresses forProperty:kABPersonAddressProperty];
    // [[GTMABAddressBook addressBook] addRecord:person]; This line doesn't help either
    [[GTMABAddressBook addressBook] save];

    [self.navigationController dismissModalViewControllerAnimated:YES];
    return NO;
}

Any ideas what I may be doing wrong?


Finally figured it out: +addressBook does not return a singleton, but instead returns a newly instantiated GTMABAddressBook instance. I had to simply use the same instance to actually save the changes.

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)personRef {
    GTMABPerson *record = [GTMABPerson recordWithRecord:personRef];
    GTMABAddressBook *addressBook = [GTMABAddressBook addressBook];
    GTMABPerson *person = [addressBook personForId:[record recordID]];

    GTMABMutableMultiValue *addresses = [[[person valueForProperty:kABPersonAddressProperty] mutableCopy] autorelease];
    if (!addresses) {
        addresses = [GTMABMutableMultiValue valueWithPropertyType:kABMultiDictionaryPropertyType];
    }

    NSMutableDictionary *address = [NSMutableDictionary dictionary];
    [address setObject:@"Street" forKey:(NSString *)kABPersonAddressStreetKey];
    [address setObject:@"City" forKey:(NSString *)kABPersonAddressCityKey];
    [address setObject:@"Country" forKey:(NSString *)kABPersonAddressCountryKey];
    [addresses addValue:address withLabel:(CFStringRef)@"Label"];

    [person setValue:addresses forProperty:kABPersonAddressProperty];
    [addressBook save];

    [self.navigationController dismissModalViewControllerAnimated:YES];
    return NO;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号