我是iOS
个开发者和xCode
的全新人物。
以前我开发了android,我知道我应该与Intent一起打电话并在Android应用程序中发送短信。
现在我想开发一个简单的应用程序,当我按下Button
时,它会将SMS
发送到特定的数字。
所以我在Mac OS 10.11
上安装了VM
Ubuntu
。
并且可以连接我的iPhone
5并在真实Hello World
设备中运行我的简单iPhone
应用,而不是simulator
。
现在我在Button
中创建了一个StoryBoard
,并按照以下文章制作了一个函数:
Send SMS Message Toturial
另外看看其他链接也是类似的。
这是我简单的ViewController.swift
。
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
@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;
}
}
}
还创建了一个Simple
按钮并将其链接到上面的sendMessage
。
当我运行应用程序时,显示按钮,当我按下它时,它似乎正在做某事,但不发送短信。
任何的想法??
我真的很感激。
更新:正如我们的朋友说我在sendMessage print("Send button pressed")
中添加了Button
,所以当我按下按钮时,我会找到按钮sendMessage
。
答案 0 :(得分:1)
我终于做到了,我混淆了两个解决方案,
首先,我改变了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
功能,现在它可以正常工作。
向我的朋友@Kabiroberai寻求帮助并给予一些时间。
答案 1 :(得分:0)
继承底部函数转换为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;
}
}