我正在尝试发送短信并使用该代码:
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文件并停在那里:
日志在这里:
2017-02-16 14:06:13.171616注册[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注册[24422:1035645]子系统:com.apple.UIKit,类别: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]子系统:com.apple.UIKit,类别: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] subsystem:com.apple.BackBoardServices.fence,category: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]子系统:com.apple.UIKit,类别: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,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.943772注册[24422:1035557] subsystem: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] subsystem:com.apple.BackBoardServices.fence,category: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] subsystem: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:completion:] + 5328 3 UIKit 0x0000000111a4e3da __62- [UIViewController presentViewController:animated:completion:] _ block_invoke_2 + 70 4 UIKit 0x000000011194fada + [UIView(动画)performWithoutAnimation:] + 90 5 UIKit 0x0000000111a4e370 __62- [UIViewController presentViewController:animated:completion:] _ block_invoke + 365 6 UIKit 0x0000000111a4e6df - [UIViewController _performCoordinatedPresentOrDismiss:animated:] + 530 7 UIKit 0x0000000111a4e1c5 - [UIViewController presentViewController:animated:completion:] + 179 8注册0x000000010f7f13da _TFC12Registration14ViewController17sendSMSBtnClickedfPs9AnyObject_T_ + 1002 9注册0x000000010f7f1516 _TToFC12Registration14ViewController17sendSMSBtnClickedfPs9AnyObject_T_ + 54 10 UIKit 0x0000000111898b88 - [UIApplication sendAction:to:from:forEvent:] + 83 11 UIKit 0x0000000111a1e2b2 - [UIControl sendAction:to: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 GraphicsServices 0x0000000115a13a6f GSEventRunModal + 161 24 UIKit 0x0000000111896f34 UIApplicationMain + 159 25注册0x000000010f7f911f main + 111 26 libdyld.dylib 0x000000011428f68d start + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止
请帮助解决这个问题。有什么想法吗?
也许是这个问题,因为我在模拟器上运行脚本,而不是在设备上运行?
提前致谢!!!
答案 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)
}
}