これはサンプルコードです:
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
@IBAction func showEmail(sender : AnyObject) {
var emailTitle = "Test Email"
var messageBody = "This is a test email body"
var toRecipents = ["[email protected]"]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result {
case MFMailComposeResultCancelled:
NSLog("Mail cancelled")
case MFMailComposeResultSaved:
NSLog("Mail saved")
case MFMailComposeResultSent:
NSLog("Mail sent")
case MFMailComposeResultFailed:
NSLog("Mail sent failure: %@", [error.localizedDescription])
default:
break
}
self.dismissViewControllerAnimated(false, completion: nil)
}
}
関数mailComposeControllerで、case式ごとにエラーが発生します。
指定された引数を受け入れるオーバーロード '〜='が見つかりませんでした。
何が悪いのですか?
Xcode 5とXcode 6の両方でMFMailComposeResult
のドキュメントを比較しました。Swiftでは、MFMailComposeResult
は構造体です
struct MFMailComposeResult {
init(_ value: CUnsignedInt) // available in iPhone 3.0
var value: CUnsignedInt
}
MFMailComposeResultCancelled
を型MFMailComposeResult
の定数として:
var MFMailComposeResultCancelled: MFMailComposeResult { get }
objective-Cの列挙型ですが:
enum MFMailComposeResult {
MFMailComposeResultCancelled,
MFMailComposeResultSaved,
MFMailComposeResultSent,
MFMailComposeResultFailed
};
typedef enum MFMailComposeResult MFMailComposeResult; // available in iPhone 3.0
コードを機能させるには、CUnsignedInt
の値を比較する必要があります。
したがって、次のコードを入力する必要があります。
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: \(error.localizedDescription)")
default:
break
}
self.dismissViewControllerAnimated(false, completion: nil)
}
Swift 2.0はこれを行います:
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
print("Mail cancelled")
case MFMailComposeResultSaved.rawValue:
print("Mail saved")
case MFMailComposeResultSent.rawValue:
print("Mail sent")
case MFMailComposeResultFailed.rawValue:
print("Mail sent failure: \(error!.localizedDescription)")
default:
break
}
controller.dismissViewControllerAnimated(true, completion: nil)
}
Swift 3.0->構文の変更
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResult.Cancelled.rawValue:
print("Mail cancelled")
case MFMailComposeResult.Saved.rawValue:
print("Mail saved")
case MFMailComposeResult.Sent.rawValue:
print("Mail sent")
case MFMailComposeResult.Failed.rawValue:
print("Mail sent failure: %@", [error!.localizedDescription])
default:
break
}
// Dismiss the mail compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}
Swift 3では、次の明確なコードを使用できます。
@IBAction func sendMail(_ sender: Any) {
print(MFMailComposeViewController.canSendMail())
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["[email protected]"])
mail.setMessageBody("<p>This is test Mail!</p>", isHTML: true)
present(mail, animated: true)
} else {
let email = "[email protected]"
if let url = URL(string: "mailto:\(email)") {
UIApplication.shared.open(url)
}
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
switch result {
case .cancelled:
print("Mail cancelled")
case .saved:
print("Mail saved")
case .sent:
self.allertInfo(_title: "Mail Info", _message: "Mail is sent successfuly", _actionTitle: "OK")
print("Mail sent")
case .failed:
self.allertInfo(_title: "Mail Info", _message: "Mail isn't sent.",
_actionTitle: "OK")
print("Mail sent failure: \(error?.localizedDescription)")
default:
break
}
}
func allertInfo(_title:String, _message:String, _actionTitle:String) {
let alert = UIAlertController(title: _title, message: _message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: _actionTitle, style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}