123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /*
- Copyright 2013-2014 appPlant UG
-
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- */
-
- #import "APPEmailComposer.h"
- #import "APPEmailComposerImpl.h"
- #import <Cordova/CDVAvailability.h>
- #ifndef __CORDOVA_4_0_0
- #import <Cordova/NSData+Base64.h>
- #endif
- #import <MessageUI/MFMailComposeViewController.h>
- #import <MobileCoreServices/MobileCoreServices.h>
-
- #include "TargetConditionals.h"
-
- @interface APPEmailComposer ()
-
- @property (nonatomic, retain) CDVInvokedUrlCommand* command;
-
- /**
- * Implements the plugin functionality.
- */
- @property (nonatomic, retain) APPEmailComposerImpl* impl;
-
- @end
-
- @implementation APPEmailComposer
-
- #pragma mark -
- #pragma mark Lifecycle
-
- - (void)pluginInitialize
- {
- _impl = [[APPEmailComposerImpl alloc] init];
- }
-
- #pragma mark -
- #pragma mark Public
-
- /**
- * Checks if the mail composer is able to send mails.
- *
- * @param callbackId
- * The ID of the JS function to be called with the result
- */
- - (void) isAvailable:(CDVInvokedUrlCommand*)command
- {
- [self.commandDelegate runInBackground:^{
- NSString* scheme = @"mailto";
- if (!command.arguments || command.arguments.count >= 1){
- scheme = command.arguments[0];
- }
- NSArray* boolArray = [_impl canSendMail:scheme];
- CDVPluginResult* result;
-
- result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
- messageAsMultipart:boolArray];
-
- [self.commandDelegate sendPluginResult:result
- callbackId:command.callbackId];
- }];
- }
-
- /**
- * Shows the email composer view with pre-filled data.
- *
- * @param properties
- * The email properties like subject, body, attachments
- */
- - (void) open:(CDVInvokedUrlCommand*)command
- {
- NSDictionary* props = command.arguments[0];
-
- _command = command;
-
- [self.commandDelegate runInBackground:^{
- if (![props objectForKey:@"app"]) {
- [props setValue:@"mailto" forKey:@"app"];
- }
- NSString* scheme = [props objectForKey:@"app"];
-
- if (![self canUseAppleMail:scheme]) {
- [self openURLFromProperties:props];
- return;
- }
-
- if (TARGET_IPHONE_SIMULATOR) {
- [self informAboutIssueWithSimulators];
- [self execCallback];
- }
- else {
- [self presentMailComposerFromProperties:props];
- }
- }];
- }
-
- #pragma mark -
- #pragma mark MFMailComposeViewControllerDelegate
-
- /**
- * Delegate will be called after the mail composer did finish an action
- * to dismiss the view.
- */
- - (void) mailComposeController:(MFMailComposeViewController*)controller
- didFinishWithResult:(MFMailComposeResult)result
- error:(NSError*)error
- {
- [controller dismissViewControllerAnimated:YES completion:NULL];
-
- [self execCallback];
- }
-
- #pragma mark -
- #pragma mark Private
-
- /**
- * Displays the email draft.
- *
- * @param draft
- * The email composer view
- */
- - (void) presentMailComposerFromProperties:(NSDictionary*)props
- {
- dispatch_async(dispatch_get_main_queue(), ^{
- MFMailComposeViewController* draft =
- [_impl mailComposerFromProperties:props delegateTo:self];
-
- [self.viewController presentViewController:draft
- animated:YES
- completion:NULL];
- });
-
- }
-
- /**
- * Instructs the application to open the specified URL.
- *
- * @param url
- * A mailto: compatible URL.
- */
- - (void) openURLFromProperties:(NSDictionary*)props
- {
- NSURL* url = [_impl urlFromProperties:props];
-
- [[UIApplication sharedApplication] openURL:url];
- }
-
- /**
- * If the specified app if the buil-in iMail framework can be used.
- *
- * @param scheme
- * An URL scheme.
- * @return
- * true if the scheme does refer to the email: scheme.
- */
- - (BOOL) canUseAppleMail:(NSString*) scheme
- {
- return [MFMailComposeViewController canSendMail] && [scheme hasPrefix:@"mailto"];
- }
-
- /**
- * Presents a dialog to the user to inform him about an issue with the iOS8
- * simulator in combination with the mail library.
- */
- - (void) informAboutIssueWithSimulators
- {
- dispatch_async(dispatch_get_main_queue(), ^{
- [[[UIAlertView alloc] initWithTitle:@"Email-Composer"
- message:@"Please use a physical device."
- delegate:NULL
- cancelButtonTitle:@"OK"
- otherButtonTitles:NULL] show];
- });
- }
-
- /**
- * Invokes the callback without any parameter.
- */
- - (void) execCallback
- {
- CDVPluginResult *result = [CDVPluginResult
- resultWithStatus:CDVCommandStatus_OK];
-
- [self.commandDelegate sendPluginResult:result
- callbackId:_command.callbackId];
- }
-
- @end
|