2012-01-30 26 views
8

Tôi đang cố gắng nhận email mà tôi chọn để gửi email. Nhưng tôi không biết làm thế nào để setToRecipients mà người dùng mà tôi đã chọn trong khung nhìn MFMailComposeViewController.Cách đặt setToRecipients bằng MFMailComposeViewController

if ([MFMailComposeViewController canSendMail]) 
    { 
     mailer = [[MFMailComposeViewController alloc] init]; 

     mailer.mailComposeDelegate = self; 
     [mailer setSubject:@"A Message from blablabl"]; 

     NSMutableArray *usersTo = [[NSMutableArray alloc] init]; 
     toRecipients = usersTo; 
     [mailer setToRecipients:toRecipients]; 

     NSString *emailBody = @"blablablabal"; 

     [mailer setMessageBody:emailBody isHTML:YES]; 

     // only for iPad 
     // mailer.modalPresentationStyle = UIModalPresentationPageSheet; 

     [self presentModalViewController:mailer animated:YES]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" 
                 message:@"Your device doesn't support the composer sheet" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 
     [alert show]; 
    } 

Đại biểu http://pastie.org/3281814

Trả lời

27

Một vài điều là sai ở đây.

1)

phương pháp setToRecipients MFMailComposeViewController của hy vọng một mảng bất biến với người nhận đã được thiết lập.

2)

Và bạn đang cài đặt nó vào một mảng có thể thay đổi trống.

Hãy thử một cái gì đó như thế này:

NSArray *usersTo = [NSArray arrayWithObject: @"[email protected]"]; 
[mailer setToRecipients:usersTo]; 

Và bạn sẽ thấy nó hoạt động.

+0

Nó doesnt work .... "[email protected] & hơn 1 \ U2026", chỉ có thể nhận được một email vì sau khi ông đặt & n người dùng ... Tôi sẽ cố gắng thay đổi kích thước của uitextfield – user1177647

+0

@Michael Dautermann Không có cách nào để gửi email khi thử nghiệm ứng dụng trong Xcode với trình mô phỏng IOS vì ứng dụng Thư bị thiếu trong Cài đặt trong trình mô phỏng IOS. Khi tôi kết nối thiết bị Iphone với máy Mac của mình và chạy ứng dụng từ Xcode trên thiết bị này, có cách hiển thị màn hình Iphone của tôi trên máy Mac để tôi có thể kiểm soát ứng dụng đang chạy trên thiết bị vật lý bằng chuột và gửi email từ màn hình hiển thị trên mac thay vì chạm vào màn hình trên chính thiết bị Iphone? Cảm ơn nhiều – bibscy

4
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]",nil]; 
    [picker setToRecipients:toRecipients]; 
0
MFMailComposer In “ Swift “ and “ Objective c “ 
********************************************* 
In objective c 
Steps: 
1. Create new project. 
2. Add a button into storyboard. 
3. In .h add header file like “Import <MessageUI/MessageUI.h>” 
4. Add delegate to ViewController “ MFMailComposeViewControllerDelegate” 

5. In Button Action  { 
          NSString *emailTitle = “” 
          NSString *messageBody = “” 
          NSArray *toRecipents = “”         
          MFMailComposeViewController *VC = [[MFMailComposeViewController]alloc init]; 
          VC.mailComposeDelegate = self; 
          [VC.setSubject: emailTitle]; 
          [VC.setMessageBody: messageBody]; 
          [VC.setToRecepents: toRecipents]; 
      if([MFMailComposeViewController canSendMail]) { 
       [self presentViewController:mc animated:YES completion:NULL]; 
      } 

6.MailComposeController Function 

     - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      NSLog(@"Mail cancelled"); 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"Mail saved"); 
      break; 
     case MFMailComposeResultSent: 
      NSLog(@"Mail sent"); 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Mail sent failure: %@", [error localizedDescription]); 
      break; 
     default: 
      break; 
    } 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

7. And also add FrameWork: MessageUI.FrameWork 

8. Run the App into mobile or Simulator.  




In Swift: 
******* 
Steps: 
1. Create New Project 

2. Add a button Storyboard 

3. In ViewController.Swift Class File Import MessageUI 

4. In Button Action ConfiguredMailComposer below Steps 
     { 
     let mailComposeViewController = configuredMailComposeViewController() 

     if MFMailComposeViewController.canSendMail() 
{ 
      self.present(mailComposeViewController, animated: true, completion: nil) 
      self.showSendmailSuccesfulAlert() 
     } else { 

      self.showSendMailErrorAlert() 

     } 
5. Implement the configure mail composer 

     func configuredMailComposeViewController() -> MFMailComposeViewController { 

     let mailComposerVC = MFMailComposeViewController() 
     mailComposerVC.mailComposeDelegate = self 
     mailComposerVC.setToRecipients(["[email protected]"]) 
     mailComposerVC.setSubject("Sending you an in-app e-mail...") 
     mailComposerVC.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false) 
     return mailComposerVC 

    } 

6. MailComposer optional method 

     func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 

     controller.dismiss(animated: true, completion: nil) 

    } 

7. After completed the step no: run the app into device or simulator. 
Các vấn đề liên quan