2016-11-30 6 views
29

전자 메일 보내기 옵션을 사용하여 응용 프로그램을 설정하려고합니다. swift에서 이메일 보내기 3

import Foundation 
import MessageUI 
import UIKit 

class emailClass: UIViewController, MFMailComposeViewControllerDelegate { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     if !MFMailComposeViewController.canSendMail() { 
      print("Mail services are not available") 
      return 
     }   
     sendEmail() 
    } 

    func sendEmail() {  
     let composeVC = MFMailComposeViewController() 
     composeVC.mailComposeDelegate = self 
     // Configure the fields of the interface. 
     composeVC.setToRecipients(["[email protected]com"]) 
     composeVC.setSubject("Hello!") 
     composeVC.setMessageBody("Hello this is my message body!", isHTML: false) 
     // Present the view controller modally. 
     self.present(composeVC, animated: true, completion: nil) 
    } 

    func mailComposeController(controller: MFMailComposeViewController, 
          didFinishWithResult result: MFMailComposeResult, error: NSError?) { 
     // Check the result or perform other tasks. 
     // Dismiss the mail compose view controller. 
     controller.dismiss(animated: true, completion: nil) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

그래서 나는이 메시지를 얻을 : "메일 서비스를 사용할 수없는"을

나는이 코드를 가지고있다. 이제 iCloud의 시뮬레이터 장치에 로그인했습니다 ... 그래서 그렇게해야한다고 생각하지만 그렇지 않습니다. 왜이 기능이 작동하지 않습니까? 당신은 나에게 무슨 일이 잘못되었는지, 앞으로 나아갈 수있는 방법을 말해 줄 수 있습니까?

+0

당신이 이메일 주소로 장치를 구성 했

The mail compose view controller is not dismissed automatically. When the user taps the buttons to send the email or cancel the interface, the mail compose view controller calls the mailComposeController(_:didFinishWith:error:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in Listing 3. You can also use this method to check the result of the operation.

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { // Check the result or perform other tasks. // Dismiss the mail compose view controller. controller.dismiss(animated: true, completion: nil) } } 

소스를 말한다? 그렇지 않다면 ... 그러면 문제가 해결 될 수 있습니다. –

+0

시뮬레이터를 사용하고 있습니다. 전자 메일 주소로 장치를 구성하려면 어떻게합니까? 내가 설정으로 갈 때 나는 옵션 "이메일"을 볼 수 없다 .... –

+0

나는이 문제를 해결했다. 한편 iOS 장치를위한 이메일을 구성하는 링크는 ... https : // support. apple.com/en-in/HT201320 –

답변

29

시뮬레이터에서는 작동하지 않습니다. iPhone 장치에서 테스트하십시오. 당신은 응용 프로그램이

MFMailComposeViewController.canSendMail() // returns false for simulators. 

당신은 시뮬레이터를 테스트 할 수 없습니다 실제 장치에서 실행중인 경우 Apple Developer Portal - MFMailComposeViewController

+0

예. 나는 100 % 확실하다.나는 같은 유형의 앱을 개발 중이며이를 발견했다. –

+0

Ok thanks :)하지만 위 코드는 작성했습니다. 괜찮 니? –

+0

예. 코드가 정상적으로 작동하고 작동해야합니다. iPhone이 없습니까? –

4

코드가 좋은 것 같습니다과 잘 작동 참조 할 수 있습니다, 당신은 기본적인 것들을 테스트 할 수 있습니다 UI와 같은 기능, 취소/전송 버튼 클릭시 상황이 어떻게 일어나는지.

테스트하려면 장치를 사용해야합니다. 장치의 메일 응용 프로그램을 일부 메일 (예 : [email protected])로 구성해야합니다.

희망이 있습니다. 당신이 현재 뷰 컨트롤러를 알고하지는 단지 즉 RootViewController,에 표시하는 경우

// Present the view controller modally. self.present(composeVC, animated: true, completion: nil)

;-) 그 자체로 자신을 제시하는 것이

-1

코드의 문제는,

UIApplication.shared.keyWindow? .rootViewController? .present (... 내가 그것을 어떻게

다음
9

입니다. 당신이 documentati는 다음처럼 보이는 아주 잘, 나는 다른 사람을 돕는 경우에 나의 변이를 추가 할 것이라는 점을 생각했다. 게다가, 이것은 현재 (2001 년 8 월) 구문으로 조금 더 업데이트되었습니다.

MFMailComposeViewControllerDelegate 프로토콜을 준수하고 장치에서 메일을 보낼 수 있는지 확인하십시오.

import Foundation 
import UIKit 
import MessageUI 

class WelcomeViewController: UIViewController, MFMailComposeViewControllerDelegate { 


override func viewDidLoad() { 
    super.viewDidLoad() 

    if !MFMailComposeViewController.canSendMail() { 
     print("Mail services are not available") 
     return 
    } 
} 

내 앱은 IBAction을 사용하여 메일 구성을 시작합니다. 다음 mailComposeController 기능에 대한

@IBAction func sendFeedbackButtonTapped(_ sender: Any) { 

    let composeVC = MFMailComposeViewController() 
    composeVC.mailComposeDelegate = self 

    // Configure the fields of the interface. 
    composeVC.setToRecipients(["[email protected]"]) 
    composeVC.setSubject("StoryBook Feedback") 
    composeVC.setMessageBody("Hey Josh! Here's my feedback.", isHTML: false) 

    // Present the view controller modally. 
    self.present(composeVC, animated: true, completion: nil) 

} 

이 문서는 Apple Documentation: MFMailComposeViewController