Thursday, January 28, 2016

Steps to work with MFMessageComposeViewController.

1. Create object of MFMessageComposeViewController class.

let messageController = MFMessageComposeViewController()

2. Setting value to object

messageController.body = message // message is String object
messageController.recipients = recipients // recipients is Array of number

3. Setting delegate of object

messageController.messageComposeDelegate = self
//messageController.delegate = self
//Above commented statement is not correct. If you used that in your code then you will never be able to return in to your app

4. Present message controller

presentViewController(messageController, animated: true, completion: { () -> Void in
                    // completion Block
                })

5. Handle delegate

//MARK: - MFMessageComposeViewControllerDelegate
    func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
switch (result) {
        case MessageComposeResultCancelled:
            print("Message was cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultFailed:
            print("Message failed")
            self.dismissViewControllerAnimated(true, completion: nil)
        case MessageComposeResultSent:
            print("Message was sent")
            self.dismissViewControllerAnimated(true, completion: nil)
        default:
            break;
        }
}

It will work like charm.
Please use messageController.messageComposeDelegate = self for setting delegate and that will work perfect.

No comments:

Post a Comment