MessageUI(发送短信)

在MessageUI库中发送短信使用MFMessageComposeViewController。

1 相关API

1.1 Determining If Message Composition Is Available

/// 能否发短信
///
/// - returns: Bool
public class func canSendText() -> Bool

/// 能否发主题
///
/// - returns: Bool
@available(iOS 7.0, *)
public class func canSendSubject() -> Bool

/// 能否发附件
///
/// - returns: Bool
@available(iOS 7.0, *)
public class func canSendAttachments() -> Bool

/// 附件的UTI支持
///
/// - parameter uti :  See [Uniform Type Identifiers Reference](https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Introduction/Introduction.html)
///
/// - returns: Bool
@available(iOS 7.0, *)
public class func isSupportedAttachmentUTI(uti: String) -> Bool

1.2 Accessing the Delegate

/// 监听发送结果
unowned(unsafe) public var messageComposeDelegate: MFMessageComposeViewControllerDelegate?

1.3 Setting the Initial Message Information

/// 取消添加附件的按钮
@available(iOS 7.0, *)
public func disableUserAttachments()

/// 收件人
public var recipients: [String]?
/// 内容
public var body: String?
/// 主题
public var subject: String?
/// 所有附件
public var attachments: [[NSObject : AnyObject]]? { get }

/// 添加url类型的附件
@available(iOS 7.0, *)
public func addAttachmentURL(attachmentURL: NSURL, withAlternateFilename alternateFilename: String?) -> Bool

/// 添加NSData数据的附件
@available(iOS 7.0, *)
public func addAttachmentData(attachmentData: NSData, typeIdentifier uti: String, filename: String) -> Bool

2 实战演练

2.1 源代码

//
//  ViewController.swift
//  Message
//
//  CSDN:http://blog.csdn.net/y550918116j
//  GitHub:https://github.com/937447974/Blog
//
//  Created by yangjun on 16/1/11.
//  Copyright © 2016年 阳君. All rights reserved.
//

import UIKit
import MessageUI

class ViewController: UIViewController,  MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate {

    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.
    }

    // MARK: compose message
    @IBAction func composeMessage(sender: AnyObject) {
        guard MFMessageComposeViewController.canSendText() else {
            print("不能发送短信")
            return
        }
        let messageVC = MFMessageComposeViewController()
        messageVC.messageComposeDelegate = self // 代理
        messageVC.recipients = ["18511056826"] // 收件人
        messageVC.body = "短信内容" // 内容
        // 发送主题
        if MFMessageComposeViewController.canSendSubject() {
            messageVC.subject = "阳君"
        }
        // 发送附件
        if MFMessageComposeViewController.canSendAttachments() {
            // 路径添加
            if let path = NSBundle.mainBundle().pathForResource("Info", ofType: "plist") {
                messageVC.addAttachmentURL(NSURL(fileURLWithPath: path), withAlternateFilename: "Info.plist")
            }
            // NSData添加
            if MFMessageComposeViewController.isSupportedAttachmentUTI("public.png") {
                if let image = UIImage(named: "qq") {
                    if let data = UIImagePNGRepresentation(image) {
                        // 添加文件
                        messageVC.addAttachmentData(data, typeIdentifier: "public.png", filename: "qq.png")
                    }
                }
            }
            print(messageVC.attachments) // 所有附件
        }
        // messageVC.disableUserAttachments() // 禁用添加附件按钮
        self.presentViewController(messageVC, animated: true, completion: nil)
    }

    // MARK: - MFMessageComposeViewControllerDelegate
    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
        // 关闭MFMessageComposeViewController
        controller.dismissViewControllerAnimated(true, completion: nil)
        switch result { // 发送状态
        case MessageComposeResultCancelled:
            print("Result: Mail sending cancelled") // 取消发送
        case MessageComposeResultSent: // 发送成功
            print("Result: Mail sent")
        case MessageComposeResultFailed: // 发送失败
            print("Result: Message sending failed")
        default:// 其他
            print("Result: Message not sent")
        }
    }

}

2.2 效果图

 


Appendix

Sample Code

Swift

Message UI Framework Reference

Revision History

时间描述
2016-01-12博文完成

CSDN:http://blog.csdn.net/y550918116j

GitHub:https://github.com/937447974/Blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值