Created
May 29, 2016 16:13
-
-
Save KalpeshTalkar/3a666232837461e6f6b0386b9c6e04f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MessageObject.swift | |
// | |
// Created by Kalpesh Talkar on 29/05/16. | |
// Copyright © 2016 Kalpesh Talkar. All rights reserved. | |
// | |
import UIKit | |
import MessageUI | |
@objc protocol MessageObjectDelegate { | |
func messageObject(object: MessageObject, didFinishWithResult result: MessageComposeResult) | |
} | |
@IBDesignable class MessageObject: NSObject, MFMessageComposeViewControllerDelegate { | |
@IBOutlet weak var viewController: UIViewController! | |
@IBInspectable var messageBody: String! | |
@IBInspectable var recipient: String! | |
@IBInspectable var showResultAlert: Bool = false | |
var delegate: MessageObjectDelegate? | |
// MARK: - init methods | |
override init() { | |
super.init() | |
} | |
init(viewController: UIViewController) { | |
super.init() | |
self.viewController = viewController | |
} | |
init(viewController: UIViewController, delegate: MessageObjectDelegate) { | |
super.init() | |
self.viewController = viewController | |
self.delegate = delegate | |
} | |
// MARK: - send message | |
@IBAction func sendMessage(sender: AnyObject) { | |
if messageBody == nil { | |
messageBody = "" | |
} | |
if recipient == nil { | |
recipient = "" | |
} | |
sendMessage(messageBody, recipients: [recipient]) | |
} | |
func sendMessage(message: String, recipients: [String]) { | |
if (MFMessageComposeViewController.canSendText()) { | |
let controller = MFMessageComposeViewController() | |
controller.body = messageBody | |
controller.recipients = recipients | |
controller.messageComposeDelegate = self | |
viewController.presentViewController(controller, animated: true, completion: nil) | |
} else { | |
showAlert("Error",message: "Unable to send message.") | |
} | |
} | |
// MARK: - MFMessageComposeViewControllerDelegate | |
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) { | |
viewController.dismissViewControllerAnimated(true, completion: nil) | |
if showResultAlert { | |
if result == MessageComposeResultCancelled { | |
} else if result == MessageComposeResultFailed { | |
showAlert("Error",message: "Message sending failed") | |
} else if result == MessageComposeResultSent { | |
showAlert("Success",message: "Message sent successfully.") | |
} | |
} | |
} | |
// MARK: - Error Alert | |
internal func showAlert(title: String, message: String) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) | |
let ok = UIAlertAction(title: "OK", style: .Default, handler: nil) | |
alert.addAction(ok) | |
viewController.presentViewController(alert, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment