Module: BubbleWrap::Mail

Defined in:
motion/mail/mail.rb,
motion/mail/result.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.can_send_mail? ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'motion/mail/mail.rb', line 65

def can_send_mail?
  !!MFMailComposeViewController.canSendMail
end

.compose(options = {}, &callback) ⇒ Object

Base method to create your in-app mail

EX BW::Mail.compose( delegate: self, # optional, will use root view controller by default to: [ "[email protected]" ], cc: [ "[email protected]", "[email protected]" ], bcc: [ "[email protected]" ], 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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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 = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object

Event when the MFMailComposeViewController is closed

the callback is fired if it was present in the constructor



73
74
75
76
# 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