暂无描述

APPEmailComposer.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright 2013-2014 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. #import "APPEmailComposer.h"
  19. #import "APPEmailComposerImpl.h"
  20. #import <Cordova/CDVAvailability.h>
  21. #ifndef __CORDOVA_4_0_0
  22. #import <Cordova/NSData+Base64.h>
  23. #endif
  24. #import <MessageUI/MFMailComposeViewController.h>
  25. #import <MobileCoreServices/MobileCoreServices.h>
  26. #include "TargetConditionals.h"
  27. @interface APPEmailComposer ()
  28. @property (nonatomic, retain) CDVInvokedUrlCommand* command;
  29. /**
  30. * Implements the plugin functionality.
  31. */
  32. @property (nonatomic, retain) APPEmailComposerImpl* impl;
  33. @end
  34. @implementation APPEmailComposer
  35. #pragma mark -
  36. #pragma mark Lifecycle
  37. - (void)pluginInitialize
  38. {
  39. _impl = [[APPEmailComposerImpl alloc] init];
  40. }
  41. #pragma mark -
  42. #pragma mark Public
  43. /**
  44. * Checks if the mail composer is able to send mails.
  45. *
  46. * @param callbackId
  47. * The ID of the JS function to be called with the result
  48. */
  49. - (void) isAvailable:(CDVInvokedUrlCommand*)command
  50. {
  51. [self.commandDelegate runInBackground:^{
  52. NSString* scheme = @"mailto";
  53. if (!command.arguments || command.arguments.count >= 1){
  54. scheme = command.arguments[0];
  55. }
  56. NSArray* boolArray = [_impl canSendMail:scheme];
  57. CDVPluginResult* result;
  58. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  59. messageAsMultipart:boolArray];
  60. [self.commandDelegate sendPluginResult:result
  61. callbackId:command.callbackId];
  62. }];
  63. }
  64. /**
  65. * Shows the email composer view with pre-filled data.
  66. *
  67. * @param properties
  68. * The email properties like subject, body, attachments
  69. */
  70. - (void) open:(CDVInvokedUrlCommand*)command
  71. {
  72. NSDictionary* props = command.arguments[0];
  73. _command = command;
  74. [self.commandDelegate runInBackground:^{
  75. if (![props objectForKey:@"app"]) {
  76. [props setValue:@"mailto" forKey:@"app"];
  77. }
  78. NSString* scheme = [props objectForKey:@"app"];
  79. if (![self canUseAppleMail:scheme]) {
  80. [self openURLFromProperties:props];
  81. return;
  82. }
  83. if (TARGET_IPHONE_SIMULATOR) {
  84. [self informAboutIssueWithSimulators];
  85. [self execCallback];
  86. }
  87. else {
  88. [self presentMailComposerFromProperties:props];
  89. }
  90. }];
  91. }
  92. #pragma mark -
  93. #pragma mark MFMailComposeViewControllerDelegate
  94. /**
  95. * Delegate will be called after the mail composer did finish an action
  96. * to dismiss the view.
  97. */
  98. - (void) mailComposeController:(MFMailComposeViewController*)controller
  99. didFinishWithResult:(MFMailComposeResult)result
  100. error:(NSError*)error
  101. {
  102. [controller dismissViewControllerAnimated:YES completion:NULL];
  103. [self execCallback];
  104. }
  105. #pragma mark -
  106. #pragma mark Private
  107. /**
  108. * Displays the email draft.
  109. *
  110. * @param draft
  111. * The email composer view
  112. */
  113. - (void) presentMailComposerFromProperties:(NSDictionary*)props
  114. {
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. MFMailComposeViewController* draft =
  117. [_impl mailComposerFromProperties:props delegateTo:self];
  118. [self.viewController presentViewController:draft
  119. animated:YES
  120. completion:NULL];
  121. });
  122. }
  123. /**
  124. * Instructs the application to open the specified URL.
  125. *
  126. * @param url
  127. * A mailto: compatible URL.
  128. */
  129. - (void) openURLFromProperties:(NSDictionary*)props
  130. {
  131. NSURL* url = [_impl urlFromProperties:props];
  132. [[UIApplication sharedApplication] openURL:url];
  133. }
  134. /**
  135. * If the specified app if the buil-in iMail framework can be used.
  136. *
  137. * @param scheme
  138. * An URL scheme.
  139. * @return
  140. * true if the scheme does refer to the email: scheme.
  141. */
  142. - (BOOL) canUseAppleMail:(NSString*) scheme
  143. {
  144. return [MFMailComposeViewController canSendMail] && [scheme hasPrefix:@"mailto"];
  145. }
  146. /**
  147. * Presents a dialog to the user to inform him about an issue with the iOS8
  148. * simulator in combination with the mail library.
  149. */
  150. - (void) informAboutIssueWithSimulators
  151. {
  152. dispatch_async(dispatch_get_main_queue(), ^{
  153. [[[UIAlertView alloc] initWithTitle:@"Email-Composer"
  154. message:@"Please use a physical device."
  155. delegate:NULL
  156. cancelButtonTitle:@"OK"
  157. otherButtonTitles:NULL] show];
  158. });
  159. }
  160. /**
  161. * Invokes the callback without any parameter.
  162. */
  163. - (void) execCallback
  164. {
  165. CDVPluginResult *result = [CDVPluginResult
  166. resultWithStatus:CDVCommandStatus_OK];
  167. [self.commandDelegate sendPluginResult:result
  168. callbackId:_command.callbackId];
  169. }
  170. @end