回答編集履歴

5 修正

_Kentarou

_Kentarou score 8379

2016/06/04 19:38  投稿

実機で取りあえず動くことを確認しました。
もっとスマートな書き方があると思いますが、参考にしてみてください。
```swift
import UIKit
import MessageUI
//呼ぶクラス
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
   
   // インスタンスとして保持  
   var common: Common!  
     
   override func viewDidLoad() {
       super.viewDidLoad()
       common = Common().sendMail(self)
       Common.sendMail(self)
   }
   
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
       
       switch result.rawValue {
       case MFMailComposeResultCancelled.rawValue:
           print("Email Send Cancelled")
           break
       case MFMailComposeResultSaved.rawValue:
           print("Email Saved as a Draft")
           break
       case MFMailComposeResultSent.rawValue:
           print("Email Sent Successfully")
           break
       case MFMailComposeResultFailed.rawValue:
           print("Email Send Failed")
           break
       default:
           break
       }
       dismissViewControllerAnimated(true, completion: nil)
   }
}
 
 
// 呼ばれるクラス
class Common:NSObject, MFMailComposeViewControllerDelegate {
   
   func sendMail<T: UIViewController where T: MFMailComposeViewControllerDelegate>(vc: T) -> Common? {
   class func sendMail<T: UIViewController where T: MFMailComposeViewControllerDelegate>(vc: T) {
       if MFMailComposeViewController.canSendMail()==false {
           print("Email Send Failed")
           return nil;
           return
       }
       
       let mailViewController = MFMailComposeViewController()
       mailViewController.mailComposeDelegate = vc
       mailViewController.setSubject("エラー出ませんように")
       let toRecipients = ["noerror_nolife@gmail.com"]
       mailViewController.setToRecipients(toRecipients)
       mailViewController.setMessageBody("", isHTML: false)
       vc.presentViewController(mailViewController, animated: true, completion: nil)
       return self  
   }
}
```
追加質問の回答
---
```swift
import UIKit
import MessageUI
//呼ぶクラス
class ViewController: UIViewController {
   
   // インスタンスとして保持
   var common: Common!
   
   override func viewDidLoad() {
       super.viewDidLoad()
       common = Common().sendMail(self)
   }
}
// 呼ばれるクラス
class Common:NSObject, MFMailComposeViewControllerDelegate {
   
   var vc: UIViewController!
   
   func sendMail(vc: UIViewController) -> Common? {
       if MFMailComposeViewController.canSendMail()==false {
           print("Email Send Failed")
           return nil;
       }
       
       self.vc = vc
       let mailViewController = MFMailComposeViewController()
       mailViewController.mailComposeDelegate = self
       mailViewController.setSubject("エラー出ませんように")
       let toRecipients = ["noerror_nolife@gmail.com"]
       mailViewController.setToRecipients(toRecipients)
       mailViewController.setMessageBody("", isHTML: false)
       vc.presentViewController(mailViewController, animated: true, completion: nil)
       return self
   }
   
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
       
       switch result.rawValue {
       case MFMailComposeResultCancelled.rawValue:
           print("Email Send Cancelled")
           break
       case MFMailComposeResultSaved.rawValue:
           print("Email Saved as a Draft")
           break
       case MFMailComposeResultSent.rawValue:
           print("Email Sent Successfully")
           break
       case MFMailComposeResultFailed.rawValue:
           print("Email Send Failed")
           break
       default:
           break
       }
       vc.dismissViewControllerAnimated(true, completion: nil)
   }
}
```
4 修正

_Kentarou

_Kentarou score 8379

2016/06/04 19:33  投稿

実機で取りあえず動くことを確認しました。
もっとスマートな書き方があると思いますが、参考にしてみてください。
```swift
import UIKit
import MessageUI
//呼ぶクラス
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
   
   // インスタンスとして保持  
   var common: Common!  
     
   override func viewDidLoad() {
       super.viewDidLoad()
       Common.sendMail(self)
       common = Common().sendMail(self)
   }
   
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
       
       switch result.rawValue {
       case MFMailComposeResultCancelled.rawValue:
           print("Email Send Cancelled")
           break
       case MFMailComposeResultSaved.rawValue:
           print("Email Saved as a Draft")
           break
       case MFMailComposeResultSent.rawValue:
           print("Email Sent Successfully")
           break
       case MFMailComposeResultFailed.rawValue:
           print("Email Send Failed")
           break
       default:
           break
       }
       dismissViewControllerAnimated(true, completion: nil)
   }
}
// 呼ばれるクラス(案1)
class Common:NSObject {
   class func sendMail<T: MFMailComposeViewControllerDelegate>(vc: T) {
// 呼ばれるクラス
class Common:NSObject, MFMailComposeViewControllerDelegate {
   
   func sendMail<T: UIViewController where T: MFMailComposeViewControllerDelegate>(vc: T) -> Common? {
       if MFMailComposeViewController.canSendMail()==false {
           print("Email Send Failed")
           return ;
           return nil;
       }
       
       let mailViewController = MFMailComposeViewController()
       mailViewController.mailComposeDelegate = vc
       mailViewController.mailComposeDelegate = vc
       mailViewController.setSubject("エラー出ませんように")
       let toRecipients = ["noerror_nolife@gmail.com"]
       mailViewController.setToRecipients(toRecipients)
       mailViewController.setMessageBody("", isHTML: false)
       if let vc = vc as? UIViewController {
           vc.presentViewController(mailViewController, animated: true, completion: nil)
       }
       vc.presentViewController(mailViewController, animated: true, completion: nil)
       return self
   }
}  
 
// 呼ばれるクラス(案2)  
class Common:NSObject {  
  class func sendMail(vc: UIViewController) {  
      if MFMailComposeViewController.canSendMail()==false {  
          print("Email Send Failed")  
          return ;  
      }  
       
      if vc is MFMailComposeViewControllerDelegate {  
          let mailViewController = MFMailComposeViewController()  
          mailViewController.mailComposeDelegate = vc as? MFMailComposeViewControllerDelegate  
          mailViewController.setSubject("エラー出ませんように")  
          let toRecipients = ["noerror_nolife@gmail.com"]  
          mailViewController.setToRecipients(toRecipients)  
          mailViewController.setMessageBody("", isHTML: false)  
          vc.presentViewController(mailViewController, animated: true, completion: nil)  
      }   
  }  
}
```
追加質問の回答
---
```swift
import UIKit
import MessageUI
//呼ぶクラス
class ViewController: UIViewController {
   
   // インスタンスとして保持
   var common: Common!
   
   override func viewDidLoad() {
       super.viewDidLoad()
       common = Common().sendMail(self)
   }
}
// 呼ばれるクラス
class Common:NSObject, MFMailComposeViewControllerDelegate {
   
   var vc: UIViewController!
   
   func sendMail(vc: UIViewController) -> Common? {
       if MFMailComposeViewController.canSendMail()==false {
           print("Email Send Failed")
           return nil;
       }
       
       self.vc = vc
       let mailViewController = MFMailComposeViewController()
       mailViewController.mailComposeDelegate = self
       mailViewController.setSubject("エラー出ませんように")
       let toRecipients = ["noerror_nolife@gmail.com"]
       mailViewController.setToRecipients(toRecipients)
       mailViewController.setMessageBody("", isHTML: false)
       vc.presentViewController(mailViewController, animated: true, completion: nil)
       return self
   }
   
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
       
       switch result.rawValue {
       case MFMailComposeResultCancelled.rawValue:
           print("Email Send Cancelled")
           break
       case MFMailComposeResultSaved.rawValue:
           print("Email Saved as a Draft")
           break
       case MFMailComposeResultSent.rawValue:
           print("Email Sent Successfully")
           break
       case MFMailComposeResultFailed.rawValue:
           print("Email Send Failed")
           break
       default:
           break
       }
       vc.dismissViewControllerAnimated(true, completion: nil)
   }
}
```
3 修正

_Kentarou

_Kentarou score 8379

2016/06/04 09:25  投稿

実機で取りあえず動くことを確認しました。
もっとスマートな書き方があると思いますが、参考にしてみてください。
```swift
import UIKit
import MessageUI
//呼ぶクラス
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
   
   override func viewDidLoad() {
       super.viewDidLoad()
       Common.sendMail(self)
   }
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
       
       switch result.rawValue {
       case MFMailComposeResultCancelled.rawValue:
           print("Email Send Cancelled")
           break
       case MFMailComposeResultSaved.rawValue:
           print("Email Saved as a Draft")
           break
       case MFMailComposeResultSent.rawValue:
           print("Email Sent Successfully")
           break
       case MFMailComposeResultFailed.rawValue:
           print("Email Send Failed")
           break
       default:
           break
       }
       dismissViewControllerAnimated(true, completion: nil)
   }
}
// 呼ばれるクラス(案1)
class Common:NSObject {
   class func sendMail<T: MFMailComposeViewControllerDelegate>(vc: T) {
       if MFMailComposeViewController.canSendMail()==false {
           print("Email Send Failed")
           return ;
       }
       
       let mailViewController = MFMailComposeViewController()
       mailViewController.mailComposeDelegate = vc
       mailViewController.setSubject("エラー出ませんように")
       let toRecipients = ["noerror_nolife@gmail.com"]
       mailViewController.setToRecipients(toRecipients)
       mailViewController.setMessageBody("", isHTML: false)
       if let vc = vc as? UIViewController {
           vc.presentViewController(mailViewController, animated: true, completion: nil)
       }
   }
}
// 呼ばれるクラス(案2)
class Common:NSObject {
  class func sendMail(vc: UIViewController) {
      if MFMailComposeViewController.canSendMail()==false {
          print("Email Send Failed")
          return ;
      }
     
      if vc is MFMailComposeViewControllerDelegate {
          let mailViewController = MFMailComposeViewController()
          mailViewController.mailComposeDelegate = vc as? MFMailComposeViewControllerDelegate
          mailViewController.setSubject("エラー出ませんように")
          let toRecipients = ["noerror_nolife@gmail.com"]
          mailViewController.setToRecipients(toRecipients)
          mailViewController.setMessageBody("", isHTML: false)
          vc.presentViewController(mailViewController, animated: true, completion: nil)
      } 
  }
}
```  
 
追加質問の回答  
---  
 
```swift  
import UIKit  
import MessageUI  
 
//呼ぶクラス  
class ViewController: UIViewController {  
     
   // インスタンスとして保持  
   var common: Common!  
     
   override func viewDidLoad() {  
       super.viewDidLoad()  
       common = Common().sendMail(self)  
   }  
}  
 
// 呼ばれるクラス  
class Common:NSObject, MFMailComposeViewControllerDelegate {  
     
   var vc: UIViewController!  
     
   func sendMail(vc: UIViewController) -> Common? {  
       if MFMailComposeViewController.canSendMail()==false {  
           print("Email Send Failed")  
           return nil;  
       }  
         
       self.vc = vc  
       let mailViewController = MFMailComposeViewController()  
       mailViewController.mailComposeDelegate = self  
       mailViewController.setSubject("エラー出ませんように")  
       let toRecipients = ["noerror_nolife@gmail.com"]  
       mailViewController.setToRecipients(toRecipients)  
       mailViewController.setMessageBody("", isHTML: false)  
       vc.presentViewController(mailViewController, animated: true, completion: nil)  
       return self  
   }  
     
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {  
         
       switch result.rawValue {  
       case MFMailComposeResultCancelled.rawValue:  
           print("Email Send Cancelled")  
           break  
       case MFMailComposeResultSaved.rawValue:  
           print("Email Saved as a Draft")  
           break  
       case MFMailComposeResultSent.rawValue:  
           print("Email Sent Successfully")  
           break  
       case MFMailComposeResultFailed.rawValue:  
           print("Email Send Failed")  
           break  
       default:  
           break  
       }  
       vc.dismissViewControllerAnimated(true, completion: nil)  
   }  
}  
 
```
2 修正

_Kentarou

_Kentarou score 8379

2016/06/03 22:31  投稿

実機で取りあえず動くことを確認しました。
もっとスマートな書き方があると思いますが、参考にしてみてください。
```swift
import UIKit
import MessageUI
//呼ぶクラス
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
   
   override func viewDidLoad() {
       super.viewDidLoad()
       Common.sendMail(self)
   }
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
       
       switch result.rawValue {
       case MFMailComposeResultCancelled.rawValue:
           print("Email Send Cancelled")
           break
       case MFMailComposeResultSaved.rawValue:
           print("Email Saved as a Draft")
           break
       case MFMailComposeResultSent.rawValue:
           print("Email Sent Successfully")
           break
       case MFMailComposeResultFailed.rawValue:
           print("Email Send Failed")
           break
       default:
           break
       }
       dismissViewControllerAnimated(true, completion: nil)
   }
}
//呼ばれるクラス
// 呼ばれるクラス(案1)
class Common:NSObject {
   class func sendMail<T: MFMailComposeViewControllerDelegate>(vc: T) {
       if MFMailComposeViewController.canSendMail()==false {
           print("Email Send Failed")
           return ;
       }
       
       let mailViewController = MFMailComposeViewController()
       mailViewController.mailComposeDelegate = vc
       mailViewController.setSubject("エラー出ませんように")
       let toRecipients = ["noerror_nolife@gmail.com"]
       mailViewController.setToRecipients(toRecipients)
       mailViewController.setMessageBody("", isHTML: false)
       if let vc = vc as? UIViewController {
           vc.presentViewController(mailViewController, animated: true, completion: nil)
       }
   }
}
 
// 呼ばれるクラス(案2)  
class Common:NSObject {  
  class func sendMail(vc: UIViewController) {  
      if MFMailComposeViewController.canSendMail()==false {  
          print("Email Send Failed")  
          return ;  
      }  
       
      if vc is MFMailComposeViewControllerDelegate {  
          let mailViewController = MFMailComposeViewController()  
          mailViewController.mailComposeDelegate = vc as? MFMailComposeViewControllerDelegate  
          mailViewController.setSubject("エラー出ませんように")  
          let toRecipients = ["noerror_nolife@gmail.com"]  
          mailViewController.setToRecipients(toRecipients)  
          mailViewController.setMessageBody("", isHTML: false)  
          vc.presentViewController(mailViewController, animated: true, completion: nil)  
      }   
  }  
}  
```
1 修正

_Kentarou

_Kentarou score 8379

2016/06/03 20:55  投稿

実機で取りあえず動くことを確認しました。
もっとスマートな書き方があると思いますが、参考にしてみてください。
```swift
import UIKit
import MessageUI
//呼ぶクラス
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
   
   override func viewDidLoad() {
       super.viewDidLoad()
       Common.sendMail(self)
   }
   func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
       
       switch result.rawValue {
       case MFMailComposeResultCancelled.rawValue:
           print("Email Send Cancelled")
           break
       case MFMailComposeResultSaved.rawValue:
           print("Email Saved as a Draft")
           break
       case MFMailComposeResultSent.rawValue:
           print("Email Sent Successfully")
           break
       case MFMailComposeResultFailed.rawValue:
           print("Email Send Failed")
           break
       default:
           break
       }
       dismissViewControllerAnimated(true, completion: nil)
   }
}
//呼ばれるクラス
class Common:NSObject {
   class func sendMail(vc: UIViewController) {
   class func sendMail<T: MFMailComposeViewControllerDelegate>(vc: T) {
       if MFMailComposeViewController.canSendMail()==false {
           print("Email Send Failed")
           return ;
       }
       
       if vc is MFMailComposeViewControllerDelegate {
           let mailViewController = MFMailComposeViewController()
           mailViewController.mailComposeDelegate = vc as? MFMailComposeViewControllerDelegate
           mailViewController.setSubject("エラー出ませんように")
           let toRecipients = ["noerror_nolife@gmail.com"]
           mailViewController.setToRecipients(toRecipients)
           mailViewController.setMessageBody("", isHTML: false)
       let mailViewController = MFMailComposeViewController()
       mailViewController.mailComposeDelegate = vc
       mailViewController.setSubject("エラー出ませんように")
       let toRecipients = ["noerror_nolife@gmail.com"]
       mailViewController.setToRecipients(toRecipients)
       mailViewController.setMessageBody("", isHTML: false)
       if let vc = vc as? UIViewController {
           vc.presentViewController(mailViewController, animated: true, completion: nil)
       }
   }
}
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る