Swift 3.0:发送短信错误
Swift 3.0: Send an SMS Error
我正在尝试发送短信并使用该代码:
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 Registration[24422:1035557] bundleid: XLab.Registration, enable_level: 0, persist_level: 0, propagate_with_activity: 0
2017-02-16 14:06:13.172031 Registration[24422:1035557] subsystem: com.apple.siri, category: 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 Registration[24422:1035645] subsystem: com.apple.UIKit, category: 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 Registration[24422:1035635] subsystem: com.apple.BaseBoard, category: 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 Registration[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 Registration[24422:1035557] subsystem: com.apple.UIKit, category: 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 Registration[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 Registration[24422:1035557] subsystem: com.apple.UIKit, category: 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 Registration[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 Registration[24422:1035557] subsystem: com.apple.UIKit, category: 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 Registration[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 Registration[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 Registration[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 Registration[24422:1035557] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'
* First throw call stack:
(
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(Animation) 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 Registration 0x000000010f7f13da _TFC12Registration14ViewController17sendSMSBtnClickedfPs9AnyObject_T_ + 1002
9 Registration 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 Registration 0x000000010f7f911f main + 111
26 libdyld.dylib 0x000000011428f68d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
请帮助解决该问题。有什么想法吗?
也许这个问题是因为我 运行 模拟器上的脚本,而不是设备上的脚本?
提前致谢!!!
您可以使用此代码。它已经过测试并且运行良好
@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)
}
}
我正在尝试发送短信并使用该代码:
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 Registration[24422:1035557] bundleid: XLab.Registration, enable_level: 0, persist_level: 0, propagate_with_activity: 0 2017-02-16 14:06:13.172031 Registration[24422:1035557] subsystem: com.apple.siri, category: 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 Registration[24422:1035645] subsystem: com.apple.UIKit, category: 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 Registration[24422:1035635] subsystem: com.apple.BaseBoard, category: 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 Registration[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 Registration[24422:1035557] subsystem: com.apple.UIKit, category: 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 Registration[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 Registration[24422:1035557] subsystem: com.apple.UIKit, category: 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 Registration[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 Registration[24422:1035557] subsystem: com.apple.UIKit, category: 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 Registration[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 Registration[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 Registration[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 Registration[24422:1035557] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .' * First throw call stack: ( 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(Animation) 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 Registration 0x000000010f7f13da _TFC12Registration14ViewController17sendSMSBtnClickedfPs9AnyObject_T_ + 1002 9 Registration 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 Registration 0x000000010f7f911f main + 111 26 libdyld.dylib 0x000000011428f68d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException
请帮助解决该问题。有什么想法吗?
也许这个问题是因为我 运行 模拟器上的脚本,而不是设备上的脚本?
提前致谢!!!
您可以使用此代码。它已经过测试并且运行良好
@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)
}
}