2016-11-30 25 views
29

Tôi đang cố gắng thiết lập ứng dụng có tùy chọn gửi email.Gửi email từ swift 3

tôi có mã này:

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]"]) 
     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. 
    } 
} 

Vì vậy, tôi nhận được tin nhắn này: "Dịch vụ thư không có sẵn". Bây giờ tôi đã đăng nhập vào thiết bị mô phỏng trong iCloud ... Vì vậy, tôi nghĩ rằng nó nên làm điều đó nhưng nó không phải. Tại sao điều này không hoạt động? Bạn có thể cho tôi biết điều gì sai và làm thế nào tôi có thể tiến lên?

+0

Bạn có cấu hình thiết bị của bạn với một địa chỉ email? nếu không, thì hãy làm ... và nó có thể giải quyết vấn đề của bạn. –

+0

Tôi đang sử dụng trình mô phỏng. Tôi làm cách nào để định cấu hình thiết bị bằng địa chỉ email? khi tôi đi đến cài đặt tôi không thể thấy tùy chọn "email" .... –

+0

Tôi điều, bạn đã giải quyết vấn đề này .. Trong khi đó, đây là liên kết để định cấu hình email cho thiết bị iOS ... https: // support. apple.com/en-in/HT201320 –

Trả lời

29

Nó sẽ không hoạt động với trình mô phỏng. Vui lòng kiểm tra nó trên thiết bị iPhone. Bạn có thể tham khảo Apple Developer Portal - MFMailComposeViewController

+0

Có. Tôi chắc chắn 100%.Tôi đang làm việc trên cùng một loại ứng dụng và tìm thấy điều này. –

+0

Ok cảm ơn :) nhưng mã tôi đã viết ở trên. Là nó ổn? –

+0

Có. Mã có vẻ tốt và nên hoạt động. Bạn không có bất kỳ iPhone nào? –

4

Mã có vẻ là tốt và hoạt động tốt nếu ứng dụng đang chạy trong một thiết bị thực

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

Bạn không thể kiểm tra nó trên mô phỏng, bạn sẽ có thể thử nghiệm những điều cơ bản như giao diện người dùng, Làm thế nào những điều đang xảy ra trên các nút bấm Hủy/Gửi.

Để kiểm tra, Bạn phải sử dụng thiết bị, Ứng dụng Thư trong thiết bị phải được định cấu hình bằng một số thư (ví dụ: [email protected]).

Hy vọng điều đó sẽ hữu ích.

-1

Vấn đề với mã của bạn là, rằng

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

trình bày chính nó trong bản thân ;-)

Nếu bạn không biết điều khiển xem hiện tại, chỉ hiển thị nó trên RootViewController, tức là

UIApplication.shared.keyWindow? .rootViewController? .present (...

9

đây là cách tôi đã làm nó. Dường như bạn làm theo các documentati rất tốt, tôi nghĩ tôi sẽ thêm biến thể của mình trong trường hợp nó giúp người khác. Thêm vào đó, cú pháp này được cập nhật thêm một chút cho cú pháp hiện tại (tháng 8 năm 2017).

Tuân thủ giao thức MFMailComposeViewControllerDelegate và kiểm tra xem thiết bị có thể gửi thư không.

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 
    } 
} 

Ứng dụng của tôi sử dụng IBAction để khởi tạo thành phần thư.

@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) 

} 

Về chức năng mailComposeController sau, các tài liệu nói

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) 
    } 
} 

Nguồn Apple Documentation: MFMailComposeViewController