module BubbleWrap::Mail

Public Instance Methods

can_send_mail?() click to toggle source
# File motion/mail/mail.rb, line 65
def can_send_mail?
  !!MFMailComposeViewController.canSendMail
end
compose(options = {}, &callback) click to toggle source

Base method to create your in-app mail


EX

BW::Mail.compose(
  delegate: self, # optional, will use root view controller by default
  to: [ "tom@example.com" ],
  cc: [ "itchy@example.com", "scratchy@example.com" ],
  bcc: [ "jerry@example.com" ],
  html: false,
  subject: "My Subject",
  message: "This is my message. It isn't very long.",
  animated: false
) do |result, error|
  result.sent?      # => boolean
  result.canceled?  # => boolean
  result.saved?     # => boolean
  result.failed?    # => boolean
  error             # => NSError
end
# File motion/mail/mail.rb, line 25
def compose(options = {}, &callback)
  options = {
    delegate: App.window.rootViewController,
    animated: true,
    html: false,
    to: [],
    cc: [],
    bcc: [],
    subject: 'Contact'
  }.merge(options)

  @delegate = options[:delegate]
  @mailer_is_animated = options[:animated]
  @callback = callback
  @callback.weak! if @callback && BubbleWrap.use_weak_callbacks?

  @mail_controller = create_mail_controller(options)

  @delegate.presentViewController(@mail_controller, animated: @mailer_is_animated, completion: options[:completion])
end
create_mail_controller(options = {}) click to toggle source
# File motion/mail/mail.rb, line 46
def create_mail_controller(options = {})
  unless can_send_mail?
    controller = UIAlertController.alertControllerWithTitle("Email", message:"Cannot compose an email. Please run on device.", preferredStyle:UIAlertControllerStyleAlert)
    controller.addAction(UIAlertAction.actionWithTitle:"OK",style:UIAlertActionStyleDefault, handler:@callback)
    return controller
  end

  mail_controller = MFMailComposeViewController.alloc.init

  mail_controller.mailComposeDelegate = self
  mail_controller.setToRecipients(Array(options[:to]))
  mail_controller.setCcRecipients(Array(options[:cc]))
  mail_controller.setBccRecipients(Array(options[:bcc]))
  mail_controller.setSubject(options[:subject])
  mail_controller.setMessageBody(options[:message], isHTML: !!options[:html])

  mail_controller
end
mailComposeController(controller, didFinishWithResult: result, error: error) click to toggle source

Event when the MFMailComposeViewController is closed


the callback is fired if it was present in the constructor

# File motion/mail/mail.rb, line 73
def mailComposeController(controller, didFinishWithResult: result, error: error)
  @delegate.dismissModalViewControllerAnimated(@mailer_is_animated)
  @callback.call Result.new(result, error) if @callback
end