2011-02-01 26 views
37

Làm cách nào để gửi email trong ứng dụng mà không cần rời khỏi ứng dụng.Mục tiêu C: Gửi email mà không cần thoát khỏi ứng dụng

này hoạt động:

-(void) sendEmailTo:(NSString *)to withSubject:(NSString *)subject withBody:(NSString *)body { 
NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@", 
         [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
         [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
         [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]]; 
} 

nhưng đi vào ứng dụng thư để gửi. Có cách nào để làm điều này mà không cần rời khỏi ứng dụng?

+0

Bạn có thể tìm thấy [này] (http: // www. Hướng dẫn iphonedevsdk.com/forum/tutorial-discussion/43633-quick-tutorial-how-add-mfmailcomposeviewcontroller.html) hữu ích. – Vin

+0

Tôi đã tìm thấy [this] (http://www.codingexplorer.com/mfmailcomposeviewcontroller-send-email-in-your-apps/) hướng dẫn tốt hơn http://www.codingexplorer.com/mfmailcomposeviewcontroller-send-email-in- ứng dụng của bạn/ – andi

Trả lời

48

Có. Sử dụng MFMailComposeViewController.

// From within your active view controller 
if([MFMailComposeViewController canSendMail]) { 
    MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init]; 
    mailCont.mailComposeDelegate = self; 

    [mailCont setSubject:@"yo!"]; 
    [mailCont setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 
    [mailCont setMessageBody:@"Don't ever want to give you up" isHTML:NO]; 

    [self presentModalViewController:mailCont animated:YES]; 
    [mailCont release]; 
} 


// Then implement the delegate method 
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
    [self dismissModalViewControllerAnimated:YES]; 
} 
+0

Loại công việc này, nhưng nó sẽ không chính xác màn hình thư khi được thực hiện – Chris

+0

Bạn cũng cần triển khai phương thức ủy nhiệm mà tôi đã thêm vào. – kubi

+0

Phần thứ hai đó cũng có trong tệp triển khai, đúng không? Bởi vì nó vẫn đóng băng hoặc khi tôi nhấn hủy hoặc gửi trên màn hình email – Chris

12

Cập nhật dành cho iOS 6. Xin lưu ý rằng đây sử dụng ARC và không sử dụng các trình bày quan điểm phương thức phản đối:

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

Và sau đó mã để trình bày màn hình email:

- (IBAction)emailButtonPushed:(id)sender { 

    if([MFMailComposeViewController canSendMail]) { 
     MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init]; 
     mailCont.mailComposeDelegate = self; 
     [mailCont setSubject:@"Your email"]; 
     [mailCont setMessageBody:[@"Your body for this message is " stringByAppendingString:@" this is awesome"] isHTML:NO]; 
     [self presentViewController:mailCont animated:YES completion:nil]; 
    } 

} 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
    //handle any error 
    [controller dismissViewControllerAnimated:YES completion:nil]; 
} 
25
  1. Thêm MessageUI framework:

    • Bấm vào dự án
    • Chọn "Xây dựng giai đoạn"
    • Mở rộng "Liên kết Binary Với Libraries"
    • Bấm "+" và gõ "Message" để tìm "MessageUI" khuôn khổ, sau đó thêm.
  2. Trong điều khiển xem hiện tại thêm nhập khẩu và thực hiện một giao thức:

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

Thêm phương pháp:

-(void)sendEmail { 
     // From within your active view controller 
     if([MFMailComposeViewController canSendMail]) { 
      MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init]; 
      mailCont.mailComposeDelegate = self;  // Required to invoke mailComposeController when send 

      [mailCont setSubject:@"Email subject"]; 
      [mailCont setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 
      [mailCont setMessageBody:@"Email message" isHTML:NO]; 

      [self presentViewController:mailCont animated:YES completion:nil]; 
     } 
    } 

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
     [controller dismissViewControllerAnimated:YES completion:nil]; 
    } 
Các vấn đề liên quan