2017-12-07 23 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, 완료 : nil) 여야합니다. 그러나 이제는 사용자가 확인을 기다리지 않고 alertView가 단독으로 사라지는 또 다른 문제가 있습니다. 그것은 약 1 초간 계속 나타납니다. 사용자가 확인을 클릭 할 때까지 어떻게 대기 상태로 유지 되나요? –

답변

0

가 왜 사용자 만 전화 번호를 선택하기를 제한하지 않는다 : 내 코드는 왜 ... 여기

나는 내 인생은 알아낼 수 없습니다 하시나요? 다른 모든 필드를 숨길 수도 있습니다. 따라서 연락처에 전화 번호가 없거나 아무 것도 표시되지 않습니다 (다른 연락처로 이동하게됩니다 ...) 여기에서 답변을 확인하십시오. Contact Address book crash on iOS 10 beta 이 경우, didSelect 함수는 전화 번호가 선택된 경우에만 호출됩니다.