iphone - 推送 ABPersonViewController 时出现问题

标签 iphone ios addressbook

当名称来自用户添加时,我在推送 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);
}

最佳答案

我替换了您将 NSString 转换为 CFStringRef 的代码:

这是如果您使用 ARC 的话:

 CFStringRef cfstringRef = (__bridge_retained  CFStringRef)string;

但看来你不是,所以对于非 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);
}

关于iphone - 推送 ABPersonViewController 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009506/

相关文章:

iphone - iOS6自动旋转。标签栏->导航->模态

ios - 我如何使用 GLKMatrix4 将此实现转换为使用 CATransform3D

ios - 使用 Realm 从 Swift 中的文本字段保存标题

ios - 是否可以在不将版本添加到App Store的情况下添加新的TestFlight版本?

iphone - 如何将生日从我的应用程序添加到我的手机地址簿

objective-c - iOS 6 从地址簿中读取 Facebook 用户名 ID

c# - Vector3.Lerp 无法在我的相机上工作

iphone - 如何在iPhone中显示来自youtube rss feed的缩略图

ios - 使用 AVAudioEngine 进行电平测量

addressbook - iOS10 上的地址簿崩溃