2

名前が追加されたユーザーからのものである場合、ABpersonViewController をプッシュする際に問題が発生し、すべてが完全に機能しますが、名前がデフォルトのシミュレーターエントリからのものである場合、機能しませんコードで詳細に説明します

-(void)showPersonViewController:(NSString *)name  
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSString *string = name;
    NSLog(@"%@",string);
    CFStringRef cfstringRef = (CFStringRef)string;
    NSArray *peoplee = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, cfstringRef);
    NSLog(@"%@",peoplee);
    // 1ST QUESTION when contact is from defalut contact nslog is null but when from user added then it has value I dont understand why this is happening 
    if ((peoplee != nil) && [peoplee count])
    {
        ABRecordRef person = (ABRecordRef)[peoplee objectAtIndex:0];
        ABPersonViewController *picker = [[ABPersonViewController alloc] init];
        picker.personViewDelegate = self;
        picker.displayedPerson = person;

        picker.allowsEditing = YES;
        [self.navigationController pushViewController:picker animated:YES];
    }
    else
    {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Could not find Appleseed in the Contacts application"
                                                       delegate:nil
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:nil];
        [alert show];
    }
    CFRelease(addressBook);
}
4

1 に答える 1

2

NSString を CFStringRef に変換するコードを置き換えました。

これは、ARC を使用している場合です。

 CFStringRef cfstringRef = (__bridge_retained  CFStringRef)string;

しかし、あなたはそうではないようです.Non ARCの場合:

CFStringRef cfstringRef = (CFStringRef)string;

-(void)showPersonViewController
{
  ABAddressBookRef addressBook = ABAddressBookCreate();
  NSString *string = @"Appleseed";
  CFStringRef cfstringRef = (CFStringRef)string;
  NSArray *peoplee = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook, cfstringRef);
  NSLog(@"%@",peoplee); // does not print null if you have Appleseed as your contact
  if ((peoplee != nil) && [peoplee count])
  {
    ABRecordRef person = (ABRecordRef)[peoplee objectAtIndex:0];
    ABPersonViewController *picker = [[ABPersonViewController alloc] init];
    picker.personViewDelegate = self;
    picker.displayedPerson = person;
    // Allow users to edit the person’s information
    picker.allowsEditing = YES;
    [self.navigationController pushViewController:picker animated:YES];
  }
  else
  {
    // Show an alert if "Appleseed" is not in Contacts
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Could not find Appleseed in the Contacts application"
                                                   delegate:nil
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:nil];
    [alert show];
  }
  CFRelease(addressBook);
}
于 2013-08-02T06:13:18.797 に答える