Email Composer with Attachments iOS Phonegap

时间:2012-11-26 21:25:29

标签: ios email cordova attachment

我正在尝试通过emailcomposerwithattachment插件创建here来附加PDF。

问题是当电子邮件编辑器出现时,它显示附加了PDF,但是在发送之后,当我检查电子邮件时,文件不存在。

这是我调用它的代码,我已经检查过以确保文件路径设置正确并且看起来是(使用在应用程序中其他位置的子浏览器中工作的相同文件)。

有什么建议吗?另外,我正在使用cordova 2.1.0

这是我调用插件的代码:

var strPath = window.location.href; 
var path = encodeURI(strPath.substr(0,strPath.lastIndexOf('/')) + URL); 
window.plugins.emailComposer.showEmailComposer(subject,body,[],[],[],true,[path]);

这是EmailComposer代码:

- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                [options valueForKey:@"toRecipients"], @"toRecipients",
                                [options valueForKey:@"ccRecipients"], @"ccRecipients",
                                [options valueForKey:@"bccRecipients"], @"bccRecipients",
                                [options valueForKey:@"subject"], @"subject",
                                [options valueForKey:@"body"], @"body",
                                [options valueForKey:@"bIsHTML"], @"bIsHTML",
                                [options valueForKey:@"attachments"], @"attachments",
                                nil];
    [self showEmailComposerWithParameters:parameters];
}

-(void) showEmailComposerWithParameters:(NSDictionary*)parameters {

    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;

    // set subject
    @try {
        NSString* subject = [parameters objectForKey:@"subject"];
        if (subject) {
            [mailComposer setSubject:subject];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"EmailComposer - Cannot set subject; error: %@", exception);
    }

    // set body
    @try {
        NSString* body = [parameters objectForKey:@"body"];
        BOOL isHTML = [[parameters objectForKey:@"bIsHTML"] boolValue];
        if(body) {
            [mailComposer setMessageBody:body isHTML:isHTML];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"EmailComposer - Cannot set body; error: %@", exception);
    }

    // Set recipients
    @try {
        NSArray* toRecipientsArray = [parameters objectForKey:@"toRecipients"];
        if(toRecipientsArray) {
            [mailComposer setToRecipients:toRecipientsArray];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"EmailComposer - Cannot set TO recipients; error: %@", exception);
    }

    @try {
        NSArray* ccRecipientsArray = [parameters objectForKey:@"ccRecipients"];
        if(ccRecipientsArray) {
            [mailComposer setCcRecipients:ccRecipientsArray];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"EmailComposer - Cannot set CC recipients; error: %@", exception);
    }

    @try {
        NSArray* bccRecipientsArray = [parameters objectForKey:@"bccRecipients"];
        if(bccRecipientsArray) {
            [mailComposer setBccRecipients:bccRecipientsArray];
        }
    }
    @catch (NSException *exception) {
        NSLog(@"EmailComposer - Cannot set BCC recipients; error: %@", exception);
    }

    @try {
        int counter = 1;
        NSArray *attachmentPaths = [parameters objectForKey:@"attachments"];
        if (attachmentPaths) {
            for (NSString* path in attachmentPaths) {
                @try {
                    NSLog(@"setting the path: %@", path);
                    if ([path hasSuffix:@".pdf"]) {
                        NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
                        [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"attachment%d.pdf", counter]];
                    } else {
                        // supposed image
                        UIImage *image = [UIImage imageWithContentsOfFile:path];
                        NSData *data = UIImagePNGRepresentation(image);
                        [mailComposer addAttachmentData:data mimeType:@"image/png" fileName:[NSString stringWithFormat:@"attachment%d.png", counter]];
                    }
                    counter++;
                }
                @catch (NSException *exception) {
                    NSLog(@"Cannot attach file at path %@; error: %@", path, exception);
                }
            }
        }
    }
    @catch (NSException *exception) {
        NSLog(@"EmailComposer - Cannot set attachments; error: %@", exception);
    }

    if (mailComposer != nil) {
        [self.viewController presentModalViewController:mailComposer animated:YES];
    } else {
        [self returnWithCode:RETURN_CODE_EMAIL_NOTSENT];
    }
    [mailComposer release];
}

0 个答案:

没有答案