2017-02-16 140 views
1

我試圖發送一個短信,並使用該代碼的短信錯誤:雨燕3.0:發送

import UIKit 
import MessageUI 

class ViewController: UIViewController { 

override func viewDidLoad() { 
super.viewDidLoad() 
} 

override func didReceiveMemoryWarning() { 
super.didReceiveMemoryWarning() 
} 

@IBAction func sendMessage(_ sender: UIButton) { 

let messageVC = MFMessageComposeViewController() 
messageVC.body = "For a good time call!" 
messageVC.recipients = ["79161887060"] 
messageVC.messageComposeDelegate = self 

self.present(messageVC, animated: true, completion: nil) 
} 
} 

extension ViewController: MFMessageComposeViewControllerDelegate { 
func messageComposeViewController(_ controller:  MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { 
switch (result.rawValue) { 
case MessageComposeResult.cancelled.rawValue: 
    print("Message was cancelled") 
    self.dismiss(animated: true, completion: nil) 
case MessageComposeResult.failed.rawValue: 
    print("Message failed") 
    self.dismiss(animated: true, completion: nil) 
case MessageComposeResult.sent.rawValue: 
    print("Message was sent") 
    self.dismiss(animated: true, completion: nil) 
default: 
    break; 
} 
} 
} 

構建成功,但是當我輕點按鈕(動作發送短信) - 調試器去的appdelegate文件並停止在那裏:

Error Aouput

日誌是在這裏:

2017年2月16日14:06:13.171616 [R egistration [24422:1035557] bundleid:XLab.Registration,enable_level:0,persist_level:0,propagate_with_activity:0 2017-02-16 14:06:13.172031註冊[24422:1035557]子系統:com.apple.siri,類別: Intents,enable_level:1,persist_level:1,default_ttl:0,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:0,privacy_setting:0,enable_private_data:0 2017-02-16 14:06:13.177495 Registration [ 24422:1035645] subsystem:com.apple.UIKit,category:HIDEventFiltered,enable_level:0,persist_level:0,default_ttl:0,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:1,privacy_setting:2,enable_private_data: 0 2017-02-16 14:06:13.180083註冊[24422:1035645]子系統:com.apple.UIKit,類別:HIDEventIncoming,enable_level:0,persist_level:0,default_ttl:0,info_ttl:0,debug_ttl:0, generate_symptoms:0,enable_oversize:1,privacy_setting: 2,enable_private_data:0 2017-02-16 14:06:13.190296註冊[24422:1035635]子系統:com.apple.BaseBoard,類別:MachPort,enable_level:1,persist_level:0,default_ttl:0,info_ttl:0, debug_ttl:0,generate_symptoms:0,enable_oversize:0,privacy_setting:0,enable_private_data:0 2017-02-16 14:06:13.204079註冊[24422:1035557] subsystem:com.apple.UIKit,category:StatusBar,enable_level: 0,persist_level:0,default_ttl:0,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:1,privacy_setting:2,enable_private_data:0 2017-02-16 14:06:13.296657註冊[24422:1035557]子系統:com.apple.UIKit,類別:GestureEnvironment,enable_level:0,persist_level:0,default_ttl:1,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:1,privacy_setting:2,enable_private_data:0 2017- 02-16 14:06:13.316493註冊配置[24422:1035557]子系統:com.apple.BackBoardServices.fence,類別:App,enable_level:1,persist_level:0,default_ttl:0,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:0,privacy_setting: 0,enable_private_data:0 2017-02-16 14:06:14.350494註冊[24422:1035557]子系統:com.apple.UIKit,類別:Touch,enable_level:0,persist_level:0,default_ttl:1,info_ttl:0, debug_ttl:0,generate_symptoms:0,enable_oversize:1,privacy_setting:2,enable_private_data:0 2017-02-16 14:06:14.351481註冊[24422:1035557] subsystem:com.apple.UIKit,category:Gesture,enable_level: 0,persist_level:0,default_ttl:1,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:1,privacy_setting:2,enable_private_data:0 2017-02-16 14:06:14.352214註冊[24422:1035557]子系統:com.apple.UIKit,類別:GestureExclusion,enable_level:0,pe rsist_level:0,default_ttl:1,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:1,privacy_setting:2,enable_private_data:0 2017-02-16 14:06:14.943772註冊[24422:1035557]子系統: com.apple.BackBoardServices.fence,category:Observer,enable_level:1,persist_level:0,default_ttl:0,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:0,privacy_setting:0,enable_private_data:0 2017- 02-16 14:06:14.944254註冊[24422:1035557]子系統:com.apple.BackBoardServices.fence,類別:Workspace,enable_level:1,persist_level:0,default_ttl:0,info_ttl:0,debug_ttl:0,generate_symptoms: 0,enable_oversize:0,privacy_setting:0,enable_private_data:0 2017-02-16 14:06:14.944580註冊[24422:1035557]子系統:com.apple.BackBoardServices。fence,category:Trace,enable_level:1,persist_level:0,default_ttl:0,info_ttl:0,debug_ttl:0,generate_symptoms:0,enable_oversize:0,privacy_setting:0,enable_private_data:0 2017-02-16 14:06 :32.331註冊[24422:1035557] *由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'應用程序試圖在目標上呈現一個零模式視圖控制器。 *第一擲調用堆棧: ( 0的CoreFoundation 0x00000001107ad34b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010fdf121e objc_exception_throw + 48 2的UIKit 0x0000000111a4b867 - [UIViewController中_presentViewController:withAnimationController:完成:] + 5328 3的UIKit 0x0000000111a4e3da __62 - [UIViewController中presentViewController:動畫:完成:] _ block_invoke_2 + 70 4的UIKit 0x000000011194fada + [UIView的(動畫)performWithoutAnimation:] + 90 5的UIKit 0x0000000111a4e370 __62- [UIViewController中presentViewController:動畫:完成:] _ block_invoke + 365 6的UIKit 0x0000000111a4e6df - [的UIViewController _performCoordinatedPresentOrDismiss:動畫:] + 530 7的UIKit 0x0000000111a4e1c5 - [UIViewController中presentViewController:動畫:完成:] + 179 8登記0x000000010f7f13da _TFC12Registration14ViewController17sendSMSBtnClickedfPs9AnyObject_T_ + 1002 9登記0x000000010f7f1516 _TToFC12Registration14ViewController17sendSMSBtnClickedfPs9AnyObject_T_ + 54 10的UIKit 0x0000000111898b88 - [UIApplication的sendAction:爲:從:forEvent:] + 83 11的UIKit 0x0000000111a1e2b2 - [UIControl sendAction:至:forEvent:] + 67 12的UIKit 0x0000000111a1e5cb - [UIControl _sendActionsForEvents:withEvent:方法] + 444 13的UIKit 0x0000000111a1d4c7 - [UIControl touchesEnded:withEvent:方法] + 668 14 UIKit的0x00000001119060d5 - [一個UIWindow _sendTouchesForEvent:] + 2747 15的UIKit 0x00000001119077c3 - [一個UIWindow的SendEvent:] + 4011 16的UIKit 0x00000001118b4a33 - [UIApplication的的SendEvent:] + 371 17的UIKit 0x00000001120a6b6d __dispatchPreprocessedEventFromEventQueue + 3248 18的UIKit 0x000000011209f817 __handleEventQueue + 4879 19的CoreFoundation 0x0000000110752311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 20的CoreFoundation 0x000000011073759c __CFRunLoopDoSources0 + 556 21的CoreFoundation 0x0000000110736a86 __CFRunLoopRun + 918 22的CoreFoundation 0x0000000110736494 CFRunLoopRunSpecific + 420 23 GraphicsService小號0x0000000115a13a6f GSEventRunModal + 161 24的UIKit 0x0000000111896f34 UIApplicationMain + 159 25登記0x000000010f7f911f主+ 111 26 libdyld.dylib 0x000000011428f68d啓動+ 1 ) 的libC++ abi.dylib:與類型的未捕獲的異常終止NSException

請幫助解決該問題。有任何想法嗎?

也許這個問題,因爲我在模擬器上運行腳本,而不是在設備上?

在此先感謝!

+2

我的第一個建議是檢查它是否在設備上工作。錯誤是:應用程序試圖在目標上呈現一個零模式視圖控制器。看來messageVC是無效的或者無法呈現。可能由於模擬器? – Darkwonder

+0

謝謝。我試圖做到這一點 –

回答

1

您可以使用此代碼。它的測試和運行良好

@IBAction func sendMessage(_ sender: UIButton) {
  
     if (MFMessageComposeViewController.canSendText()) { 
      let messageVC = MFMessageComposeViewController() 
      messageVC.body = "For a good time call!" 
      messageVC.recipients = ["79161887060"] 
      messageVC.messageComposeDelegate = self 
      self.presentViewController(messageVC, animated: true, completion: nil) 
     } 
    } 
+1

這個作品,我用它的方式。自然,它不會在模擬器上工作,因爲canSendText是錯誤的。但是如果設備沒有短信,至少不會崩潰。 –