2012-05-11 30 views
0

我創建了一個基於Apple的「Tabster」示例代碼的應用程序。該應用程序運行良好,但我想添加電子郵件。我創建了一個電子郵件應用並嘗試了所有我能想到的方法,從教程中學習或閱讀,但它不斷崩潰。我在Apple Dev上提出了這個問題。論壇和幾個迴應只是「將文件複製到現有的應用程序,你應該是好的。」顯然它不是這麼簡單。我已經添加了MessageUI框架,並試圖以許多不同的方式複製這些文件,並且我仍然陷入困境。星期五以來,這個問題已經讓我從星期一開始。我想第一部分是有2個main.m文件,我已經嘗試過將它們組合起來,我嘗試將郵件的main.m文件重命名爲emailmain.m。我不知道還有什麼可以嘗試的。如何添加電子郵件到現有的iOS應用程序

對我來說,令人驚訝的是,所有的文檔以及所有關於在iOS應用程序中創建電子郵件的教程都是從創建新應用程序開始的。我錯過了什麼?如何將電子郵件添加到功能完整的應用程序中。我將不勝感激任何指導,文學鏈接或有關該主題的教程。

任何幫助我可以得到這將非常感激。還有其他幾種類型的東西我想添加到它,但我甚至不能將電子郵件實施到它!

謝謝您的幫助,您可以提供。 約翰

+0

你不能只複製main.m,並期望它工作。您需要從中複製代碼。下面的答案會有所幫助,但聽起來你需要從使用Objective-C和iOS SDK的非常基礎的教程開始。 –

回答

3

這是我一直使用的方法。它應該能夠簡單而乾淨地添加到任何UIViewController。

導入你的框架:

#import <MessageUI/MessageUI.h> 

確保您包括您在接口委託:

@interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate> 

然後在您的實現,如果你需要它是一個IBAction爲增加這種方法的變化或或類似的東西:

- (void)sendEmail 
{  
    if ([MFMailComposeViewController canSendMail]) { 
     MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init]; 
     email.mailComposeDelegate = self; 

     [email setSubject:@"My Email Subject"]; 

     [email setMessageBody:@"My message body." isHTML:NO]; 

     [self presentModalViewController:email animated:YES]; 

    } else { 
     UIAlertView *emailAlert = [[UIAlertView alloc] initWithTitle:@"Email Failure" message:@"Your device is not configured to send email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [emailAlert show]; 
    } 
} 

你可以在按鈕點擊或一個你想要的東西。它會拉起一個電子郵件作曲家的視圖,你的用戶可以發送。

0

您需要導入MessageUI框架。在你想要使用它的地方,將它導入到相應的.h文件中並設置MFMailComposeViewControllerDelegate。

#import <MessageUI/MessageUI.h> 
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> 

然後,當你想發送郵件使用下面的代碼.m文件:

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

// Optional Configuration Parameters to make life easier for the user 
[mailViewController setSubject:subjectString]; 
[mailViewController setMessageBody:messageString isHTML:YES]; 
[mailViewController setToRecipients:recipientsArray]; 

// Present the VIew Controller and clean up after ourselves 
[self presentModalViewController:mailViewController animated:YES]; 
[mailViewController release]; 

添加適當的委託方法,你可以用它來關閉該控制器一旦電子郵件發送:

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
    [self dismissModalViewControllerAnimated:YES]; 
} 
0

這裏是asample代碼創建電子郵件與圖像作爲附件。您可以根據您的需要修改它。

-(void)createEmail 
    { 
    NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain]; 
     [emailBody appendString:@"<p>Some email body text can go here</p>"]; 
     UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"]; 
     NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)]; 

     NSString *base64String = [imageData base64EncodedString]; 
     [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]]; 
     [emailBody appendString:@"</body></html>"]; 
     NSLog(@"%@",emailBody); 

    //mail composer window 
     MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init]; 
     emailDialog.mailComposeDelegate = self; 
     [emailDialog setSubject:@"My Inline Image Document"]; 
     [emailDialog setMessageBody:emailBody isHTML:YES]; 

     [self presentModalViewController:emailDialog animated:YES]; 
     [emailDialog release]; 
     [emailBody release]; 
    } 
相關問題