簡體   English   中英

如何在iPhone應用程序中附加文件與郵件?

[英]How to attach textfile with mail in iphone app?

我正在使用Objective-C開發一個iPhone應用程序,我必須通過附加文本文件來發送電子郵件。 有人可以幫幫我嗎?

嗨看看這個

MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:@"Subject"];
NSString *fullPath =//get path of the file
NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
[composer addAttachmentData:fileData 
                 mimeType:@"text/plain" 
                 fileName:@"File_Name"];        
NSString *emailBody = [NSString stringWithString:@"Body"];
[composer setMessageBody:emailBody isHTML:YES];

[self presentModalViewController:composer animated:YES];
[composer release];

如果您正在使用郵件編輯器視圖控制器:

[mailComposerViewController addAttachmentData:fileData mimeType:@"text/plain" fileName:@"name_of_text_file"];

這是我的應用程序中的一種工作代碼。只需遵循。 文件(文本文件)位於應用程序的文檔目錄中確保已將MessageUIframework添加到項目中並在接口文件中導入

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface Your CLass : UIViewController <MFMessageComposeViewControllerDelegate>

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if ([mailClass canSendMail])
    {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;

        [picker setSubject:@"Your Subject"];        

        NSFileManager* manager = [[NSFileManager alloc] init]; 
        NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];   
        if (![[self NoteFile] isEqualToString:@""]) //check if note is blank
 {
            NSString *NoteFilePath = [documentsDirectory 
                                           stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",[self NoteFileName]]];
            NSData *myNoteData = [NSData dataWithContentsOfFile:NoteFilePath];
            [picker addAttachmentData:myNoteData mimeType:@"text/plain" fileName:@"Name you want to give your file.txt"];
        }

        [manager release];


        // Fill out the email body text
        NSString *emailBody = @"I am attaching my file!";
        [picker setMessageBody:emailBody isHTML:NO];

        [self presentModalViewController:picker animated:YES];


        [picker release];

您可以使用其委托方法進一步檢查郵件狀態

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM