【问题标题】:How to pick a contact from address book ios8如何从地址簿中选择联系人ios8
【发布时间】:2014-09-24 11:34:23
【问题描述】:

一年前我一直在使用 ios7 开发一个 iOS 应用程序,现在我将它升级到 ios8 ,发现有一种新的方式来挑选联系人。

我现在要做的是:

-(void)showABNewPersonViewController{
    //Calling the addresbook
    ABPeoplePickerNavigationController *picker =
    [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{

    //Getting contact email and phone number then assign it to there tb's.
    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
    CFStringRef emailID = ABMultiValueCopyValueAtIndex(emails, 0);
    _tbContactPersonEmail.text = [NSString stringWithFormat:@"%@", emailID];
    CFRelease(emailID);
    CFRelease(emails);

    ABMultiValueRef phone = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFStringRef phoneID = ABMultiValueCopyValueAtIndex(phone, 0);
    _tbContactPersonPhone.text = [NSString stringWithFormat:@"%@", phoneID];
    CFRelease(phoneID);
    CFRelease(phone);

    [self fieldValueChanged:nil];

    [self dismissViewControllerAnimated:YES completion:nil];
    return NO;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    return NO;
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

使用该代码,我可以打开地址簿并选择一个联系人,但现在它不会返回他/她的姓名和号码,而是打开所选联系人的一种详细视图。

我的 peoplePickerNavigationController 没有被调用

感谢您的帮助和快速回答¨

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    为了能够在 iOS 8 中选择联系人,您需要使用此功能:

    - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
    
    ....// do whatever you need here
    
    }
    

    当然要先符合ABPeoplePickerNavigationControllerDelegate

    示例:

    -(void)openPeoplePicker
    {
            ABPeoplePickerNavigationController *personPicker = [ABPeoplePickerNavigationController new];
    
            personPicker.peoplePickerDelegate = self;
            [self presentViewController:personPicker animated:YES completion:nil];
    }
    
    - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
    
        NSString *firstName;
        NSString *middleName;
        NSString *lastName;
        NSDate *retrievedDate;
        UIImage *retrievedImage;
    
        // get the first name
        firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    
        //get the middle name
        middleName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    
        // get the last name
        lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    
        // get the birthday
        retrievedDate = (__bridge_transfer NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);
    
        // get personPicture
        if (person != nil && ABPersonHasImageData(person))
        {
            retrievedImage = [UIImage imageWithData:(__bridge_transfer NSData*)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
        }
        else
        {
            retrievedImage = nil;
        }
    
        //set the name
        if (firstName != NULL && middleName != NULL && lastName != NULL)
        {
            retrievedName = [[NSString alloc] initWithFormat:@"%@ %@ %@",firstName,middleName,lastName];
        }
    
        if (firstName != NULL && middleName != NULL & lastName == NULL)
        {
            retrievedName = [[NSString alloc] initWithFormat:@"%@ %@",firstName, middleName];
        }
    
        if (firstName != NULL && middleName == NULL && lastName != NULL)
        {
            retrievedName = [[NSString alloc] initWithFormat:@"%@ %@",firstName,lastName];
        }
    
        if (firstName != NULL && middleName == NULL && lastName == NULL)
        {
            retrievedName = [[NSString alloc] initWithFormat:@"%@",firstName];
        }
    
        if (firstName == NULL && middleName != NULL && lastName != NULL)
        {
            retrievedName = [[NSString alloc] initWithFormat:@"%@ %@",middleName, lastName];
        }
    
        if (firstName == NULL && middleName != NULL && lastName == NULL)
        {
            retrievedName = [[NSString alloc] initWithFormat:@"%@",middleName];
        }
    
        if (firstName == NULL && middleName == NULL && lastName != NULL)
        {
            retrievedName = [[NSString alloc] initWithFormat:@"%@", lastName];
        }
    
                [self dismissViewControllerAnimated:NO completion:^(){}];
    }
    

    【讨论】:

      【解决方案2】:

      这是 Swift 中的一个示例:

      调用地址簿选择器:

      @IBAction func didTouchAddContactButton(sender: AnyObject) {
          let contactPicker = ABPeoplePickerNavigationController()
          contactPicker.peoplePickerDelegate = self
          // Just to pick an email
          let properties: [AnyObject] = [Int(kABPersonEmailProperty)]
          contactPicker.displayedProperties = properties
          // Open picker
          presentViewController(contactPicker, animated: true, completion: nil)
      }
      

      从 Person 获取电子邮件:

      extension ViewController: ABPeoplePickerNavigationControllerDelegate {
          func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
      
              // Property
              let list: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue() //kABPersonEmailProperty
              // Value index
              let index = Int(identifier) as CFIndex
              // Retrieve value
              let email: String = ABMultiValueCopyValueAtIndex(list, index).takeRetainedValue() as! String
      
              // Result
              print(email)
      
              peoplePicker.dismissViewControllerAnimated(true, completion: nil)
          }   
      }
      

      【讨论】:

        【解决方案3】:

        我更新了我的代码。这是我更新的代码。它的工作 100% 完美。

         - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
        {
           ABMultiValueRef phone = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
           CFStringRef phoneID = ABMultiValueCopyValueAtIndex(phone, 0);
           phoneNumberTextField.text = [NSString stringWithFormat:@"%@", phoneID];
           CFRelease(phoneID);
           CFRelease(phone);
        
           [self dismissViewControllerAnimated:YES completion:nil];
        
        }
        
        -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
           return NO;
        }
        
        -(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
           [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        - (void)addContactFromPhoneContacts:(id)sender
        {
            ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
            picker.peoplePickerDelegate = self;
            [self presentViewController:picker animated:YES completion:nil];
        }
        

        【讨论】:

        • 我可以打开地址簿我的问题是从地址簿中选择联系人不起作用
        • 我的 peoplePickerNavigationController 它没有被调用
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多