如何将电话号码返回swift应用程序?

时间:2015-07-31 16:26:16

标签: swift contacts abaddressbook abpeoplepickerview

我想让我的用户为他们的联系人中的某个人选择一个电话号码,我想将该号码返回给应用程序,以便我以后可以使用它。这是我允许用户选择其中一个联系人电话号码的代码,但是当我点击一个电话号码时,它会调用该号码(我不想发生这种情况)并且不会向我的应用程序返回任何内容。

    class ViewController: UIViewController {

var people = ABPeoplePickerNavigationController()
var addressBook: ABAddressBookRef?

func extractABAddressBookRef(abRef: Unmanaged<ABAddressBookRef>!) -> ABAddressBookRef? {
    if let ab = abRef {
        self.view.addSubview(people.view)
        return Unmanaged<NSObject>.fromOpaque(ab.toOpaque()).takeUnretainedValue()
    }
    return nil
}

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!, property: ABPropertyID, identifier: ABMultiValueIdentifier) {
    let multiValue: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue()
    let index = ABMultiValueGetIndexForIdentifier(multiValue, identifier)
    let phoneNumber = ABMultiValueCopyValueAtIndex(multiValue, index).takeRetainedValue() as! String

    println(phoneNumber)
}

1 个答案:

答案 0 :(得分:1)

我实施的解决方案只显示有电话号码的联系人,用户必须选择具体的电话号码。您的ViewController必须实现ABPeoplePickerNavigationControllerDelegate协议。

这是我的代码:

func checkAuthorization ()
{
    switch (ABAddressBookGetAuthorizationStatus())
    {
        case .NotDetermined:
            ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue(), 
            { (granted: Bool, error: CFError!) -> Void in
                if (granted)
                {
                    // Access granted, show picker
                    self.showPeoplePicker();
                }
                else
                {
                    // Custom func to show an alert
                    self.showAlertForContactPermission();
                }
            })
            break;

        case .Restricted, .Denied:
            // Custom func to show an alert
            showAlertForContactPermission();
            break;

        case .Authorized:
            // Access granted, show picker
            showPeoplePicker();
            break;
    }
}

func showPeoplePicker ()
{
    // Create picker
    let peoplePicker: ABPeoplePickerNavigationController = ABPeoplePickerNavigationController();
    peoplePicker.peoplePickerDelegate = self;

    // Show only contacts that have phone number
    peoplePicker.displayedProperties = [NSNumber(int: kABPersonPhoneProperty)];
    peoplePicker.predicateForEnablingPerson = NSPredicate(format: "phoneNumbers.@count > 0", argumentArray: nil);

    // User has to select explicity phone number
    peoplePicker.predicateForSelectionOfProperty = NSPredicate(format: "key == 'phoneNumbers'", argumentArray: nil);

    // Present picker
    presentViewController(peoplePicker, animated: true, completion: nil);
}

func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController, didSelectPerson person: ABRecord, property: ABPropertyID, identifier: ABMultiValueIdentifier)
{
    // Get name
    let name: String = ABRecordCopyCompositeName(person).takeRetainedValue() as String;

    // Get phone number selected
    let multiValuePhones: ABMultiValueRef = ABRecordCopyValue(person, property).takeRetainedValue();
    let selectedIndex: CFIndex = ABMultiValueGetIndexForIdentifier(multiValuePhones, identifier);
    let selectedPhone: String = ABMultiValueCopyValueAtIndex(multiValuePhones, selectedIndex).takeRetainedValue() as! String;

    // Get only digits
    let digitsArray: [String] = selectedPhone.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet);
    let phoneDigits: String = digitsArray.joinWithSeparator("");

    // Pass info to your func
    saveNewEmployee(name, phoneNumber: phoneDigits);
}