我正在开发一个应用程序,您点击一个按钮,它会发送一条已填写的短信,每当我点击该按钮时,都会显示错误...
我导入了messageUI框架并添加了MFMessageComposeViewControllerDelegate类。
@IBAction func sendMessage(sender: AnyObject) {
var messageVC = MFMessageComposeViewController()
messageVC.body = "Enter a message";
messageVC.recipients = ["Enter tel-nr"]
messageVC.messageComposeDelegate = self;
self.presentViewController(messageVC, animated: false, completion: nil)
}
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
switch (result.value) {
case MessageComposeResultCancelled.value:
println("Message was cancelled")
self.dismissViewControllerAnimated(true, completion: nil)
case MessageComposeResultFailed.value:
println("Message failed")
self.dismissViewControllerAnimated(true, completion: nil)
case MessageComposeResultSent.value:
println("Message was sent")
self.dismissViewControllerAnimated(true, completion: nil)
default:
break;
}
}
答案 0 :(得分:0)
您确定要测试的设备是否可以发送消息?模拟器没有文本消息功能。试试这个:
@IBAction func sendMessage(sender: AnyObject) {
if MFMessageComposeViewController.canSendText() == false {
println("Cannot send text")
return
}
var messageVC = MFMessageComposeViewController()
messageVC.body = "Enter a message";
messageVC.recipients = ["Enter tel-nr"]
messageVC.messageComposeDelegate = self;
self.presentViewController(messageVC, animated: false, completion: nil)
}