2016-02-06 70 views
1

當我試圖發送一個短信,並有控制器設置像這樣,但我得到一個錯誤信息'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <Ally.TextMessageController: 0x7fd5361278e0>.'我在我的故事板確信,我將它連接到TextMessageController,所以我不太確定是什麼導致了崩潰。應用程序嘗試對目標呈現零模態視圖控制器發送文本

class TextMessageController: UIViewController, MFMessageComposeViewControllerDelegate { 

    var phone: String? 
    override func viewDidLoad() { 
     super.viewDidLoad() 



     print(phone) 
     var messageVC = MFMessageComposeViewController() 

     messageVC.body = "Hey I need help, are you available"; 
     messageVC.recipients = ["555555555"] 
     messageVC.messageComposeDelegate = self; 

     presentViewController(messageVC, animated: false, completion: nil) 

     // Do any additional setup after loading the view. 
    } 


    func canSendText() -> Bool { 
     return MFMessageComposeViewController.canSendText() 
    } 

    func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) { 

     self.dismissViewControllerAnimated(true, completion: nil) 
     switch (result.rawValue) { 
     case MessageComposeResultCancelled.rawValue: 
      print("Message was cancelled") 
      self.dismissViewControllerAnimated(true, completion: nil) 
     case MessageComposeResultFailed.rawValue: 
      print("Message failed") 
      self.dismissViewControllerAnimated(true, completion: nil) 
     case MessageComposeResultSent.rawValue: 
      print("Message was sent") 
      self.dismissViewControllerAnimated(true, completion: nil) 
     default: 
      break; 
     } 
    } 

以下是錯誤消息

'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <Ally.TextMessageController: 0x7fd5361278e0>.' 

回答

7

你應該先檢查設備是否可以發送文字信息,然後只出示。例如模擬器不能發送文本消息,因此你的代碼會崩潰。與模擬器一起,用戶的設備可能不會被設置爲傳遞消息。所以進行以下檢查

if messageVC.canSendText() { 
    presentViewController(messageVC, animated: false, completion: nil) 
} 
相關問題