Thursday, January 28, 2016

MFMessageComposeViewControllerDelegate not working

MFMessageComposeViewControllerDelegate not working

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.

Wednesday, January 13, 2016

Remove notification observer and close DB connection in swift

Remove notification observer and close DB connection in swift

Which is the proper method to remove notification observer or close Database connection?

Earlier we was calling 'ViewDidUnload' method for this kind of purpose.

But now it is not possible.
According to Swift Book there is deinit block.
Which is going to call immediately before a class instance is deallocated.

Remember here, deinit is not method.


e.g.
deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }