2016-08-23 6 views
0

swift를 사용하여 연락처 세부 사항에서 MailComposeViewController를 열려고합니다. 모든 것이 잘 작동합니다. 연락처 이메일을 클릭하면 페이지가 열립니다.하지만 방금 클릭 한 이메일로 수신 이메일 입력란을 채우고 싶지만 어떻게해야할지 모르겠습니다.연락처 세부 정보에서 전자 메일 주소 받기

func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) { 

    if MFMailComposeViewController.canSendMail() { 

     let mail = MFMailComposeViewController() 

     mail.mailComposeDelegate = self 

     //This doesn't work, only asks for a string. 
     mail.setToRecipients(["\(contactProperty.value)"]) 


     self.dismissViewControllerAnimated(true, completion: {() -> Void in 
      self.presentViewController(mail, animated: true, completion: nil) 
     }) 

    } else { 
     print("send error") 
    } 
} 

난 단지 mail.setToRecipients에 문자열을 추가 할 수 있습니다

여기 내 코드입니다. contactProperty.value를 사용하면이 오류가 표시됩니다.

선택 사항 ([email protected])은 유효한 이메일 주소가 아닙니다.

+0

사용하여 교체하십시오 주소로 ... 난 그렇게 어떻게 –

+0

을 할당하기 전에 포장을 푸는 의미? 죄송합니다. – art3mis

답변

1

CNContactProperty.value은 선택형이므로 AnyObject?입니다.
따라서 안전하게 포장을 해제하고 문자열로 캐스팅해야합니다.

mail.setToRecipients(["\(contactProperty.value)"]) 

'로'

if let emailAddress = contactProperty.value as? String { 
     mail.setToRecipients([emailAddress]) 
    } 
+0

완벽하게 작동했습니다. 감사합니다. :) – art3mis