标签:ios8 swift ui设计 mfmessagecompose uiapplication
今天来讲一下如何让我们的APP可以访问系统的短信和电话功能。首先来说短信功能,比较简单,跟之前的做法差别不大,要使用UIApplication,它是一个单例。我们的功能是点击一个按钮,然后拨通一个内置的电话,需要在button的action中加入如下语句:
@IBAction func phoneBtn(sender:UIButton){
// var url1 = NSURL(string: "tel://"+canguanArray[0].tel)
var url1 = NSURL(string: "tel://10086")
UIApplication.sharedApplication().openURL(url1!)
}var url1 = NSURL(string: "sms://10086")
var url1 = NSURL(string: "http://blog.csdn.net/cg1991130")
首先在vc中导入头文件:
import MessageUI
class CaipinDetailViewController: UIViewController,MFMessageComposeViewControllerDelegate
func canSendText() -> Bool{
return MFMessageComposeViewController.canSendText()
}//用来指示一条消息能否从用户处发送
func configuredMessageComposeViewController() -> MFMessageComposeViewController{
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self
messageComposeVC.body = "HI! \(caipinArray[0].rest) 的 \(caipinArray[0].name) 味道很不错,邀你共享 -来自SoFun的邀请"
return messageComposeVC
}
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
} @IBAction func share(sender: UIButton) {
let shareView = ShareViewController()
self.presentViewController(shareView, animated: true, completion: nil)
}
@IBAction func message(sender: UIButton) {
if self.canSendText(){
let messageVC = self.configuredMessageComposeViewController()
presentViewController(messageVC, animated: true, completion: nil)
} else {
let errorAlert = UIAlertView(title: "不能发送", message: "你的设备没有短信功能", delegate: self, cancelButtonTitle: "取消")
}
}
swift UI专项训练40 用swift实现打电话和发短信功能
标签:ios8 swift ui设计 mfmessagecompose uiapplication
原文地址:http://blog.csdn.net/cg1991130/article/details/44407751