2017-12-07 25 views
1

CNContactPickerViewControllerを使用して連絡先を選択できるプログラムを作成しています。選択した連絡先にNOTの電話番号がある場合、その電話番号にエラーが表示され、OKを押すとContactPickerViewControllerに戻ります。私はブレークポイントでコードを通過し、正しく実行されていますが、エラーポップアップが表示されません。私は私の人生のために理由を理解することはできませんCNContactStoreに電話番号があり、エラーポップオーバーが表示されていないか確認してください。

は...ここに私のコードは次のとおりです。

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) { 

    if contact.phoneNumbers.first?.value.stringValue != nil{ 
     //@TODO: check for repeats in people array 

     // do something with contact 
     let newPerson = Person(firstName: contact.givenName, 
           lastName: contact.familyName, 
           profileImage: #imageLiteral(resourceName: "capitalizing_on_the_economic_potential_of_foreign_entrepreneurs_feature.png")) 

     if contact.imageDataAvailable == true{ 
      newPerson.profileImage = UIImage(data: contact.imageData!)! 
     } 

     // this is for the full name 
     let fullname = "\(contact.givenName) \(contact.familyName)" 
     print("The selected name is: \(fullname)") 
     let phoneNum = contact.phoneNumbers.first?.value.stringValue 
     print("The selected phone num is: \(phoneNum!)") 

     //appends data to new activity model for prep to send back to home vc 
     newActivity.people.append(newPerson) 
     print("the people in the new activity array are: \(newActivity.people)") 

     peopleCollection.reloadData() 

    } else { 
     print("error has no number") 
     let alertController = UIAlertController(title: "Error: Person has no number!", message: "", preferredStyle: .alert) 
     let confirmAction = UIAlertAction(title: "Ok", style: .default, handler: { 
      alert -> Void in 
     }) 
     //add actions to alert sheet 
     alertController.addAction(confirmAction) 
     self.present(alertController, animated: true, completion: nil) 
     //the code executes here correctly, but it does not present the alertController 
    } 
    //this is for phone number without dashes 
    //print("the selected phone number is: \((contact.phoneNumbers[0].value).value(forKey: "digits") as! String)") 
} 
+0

編集:最後のコメントアウトされていない行はpicker.present(alertController、animated:true、completion:nil)である必要があります。しかし、今、別の問題があります。アラートビューは、ユーザーが[OK]をクリックするのを待つことなく、単独で消えます。それは約1秒間ポップアップされたままです。ユーザーが[OK]をクリックするまで、どのようにポップアップ表示されるようにするには? –

答えて

0

は、あなただけの電話番号を選択するユーザーを制限するものではないのはなぜ?他のすべてのフィールドを非表示にすることもできるので、電話番号が表示されます。連絡先には電話番号が表示されません(別の連絡先に移動するようになります...) ここで回答を確認し、 Contact Address book crash on iOS 10 beta この場合、didSelect関数は電話番号が選択されている場合にのみ呼び出されます。

関連する問題