在iOS发送短信
问题描述:
我是iOS
正在发展中的全新产品和xCode
。 以前我开发的android和我知道我应该与意图合作拨打电话并发送短信在android应用程序。 现在我想开发一个简单的应用程序,当我按Button
它发送一个SMS
到一个特定的数字。 所以我在我的Ubuntu
上安装了Mac OS 10.11
。 并可以连接我的iPhone
5,并运行我的简单Hello World
应用程序在实际iPhone
设备而不是simulator
。 现在,我创建了一个StoryBoard
和Button
通过这篇文章让之后一个函数,: Send SMS Message Toturial在iOS发送短信
也期待在其他环节,是相似的。 这是我的简单ViewController.swift
。
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
@IBAction func sendMessage(sender: AnyObject) {
print("Send button pressed") //display a text to make sure is calling
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "TestMessage "
controller.recipients = ["xxxxxxxxx", "xxxxxxxxx"] // sending for two numbers
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
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;
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
又创造了Simple
按钮和链接,上面sendMessage
。 当我运行应用程序,显示按钮,当我按下它,它似乎是做某事,但短信不发送。 任何想法? 我真的很感激它。
更新:正如我们的朋友说我添加print("Send button pressed")
在sendMessage Button
,所以我找到按钮调用sendMessage
当我按下它。
答
我终于做到了,我用了两种解决方案, 首先,我改变了功能的sendMessage
并重新命名为sendText
,以这样的方式
@IBAction func sendText(sender: AnyObject) {
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Text Body"
controller.recipients = ["xxxxxxxxxxx"]
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}
和MessageComposeViewController
的协议是:
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
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;
}
}
,然后点击在故事板的按钮,然后按control
键并拖动按钮的ViewController并选择sendText
功能,现在它工作正常。
Thx给我的朋友@Kabiroberai寻求帮助并给予一些时间。
答
继承人的底层函数转换为SWIFT 4:
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;
}
}
你能不能把'打印( “发送按钮按下”)'在'sendMessage'功能?如果在控制台中没有看到该消息,则该函数可能不会被调用。 – kabiroberai
我在SendMessage函数前后添加了这一行,在哪里应该看到打印? 我不能在调试区域或iphone屏幕上看到, 现在我该怎么办? –
您是否将故事板中的按钮连接到代码? – kabiroberai