2012-01-30 13 views
8

私は電子メールを送信するように選択した電子メールを取得しようとしています。 しかし、私はどのようにMFMailComposeViewControllerビューで選択したユーザーがsetToRecipientsを知っていません。MFMailComposeViewControllerでToToRecipientsを設定する方法

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]; 
    } 

委任 http://pastie.org/3281814

答えて

27

カップルの事はここに間違っています。

1)

MFMailComposeViewControllerのsetToRecipients方法は、既に設定受信者との不変配列を期待します。

2)

これを空白の可変配列に設定しています。

はこのような何か試してみてください:

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

をそして、あなたはそれが動作するはずです。

+0

それdoesntの仕事.... "[email protected]&1以上います\ u2026" 彼は&n個入れた後ので、 だけ私は、ユーザー... を1通の電子メールを取得することができますuitextfieldのサイズを変更しようとしています – user1177647

+0

@Michael Dautermann IOSシミュレータの設定からMail Appが見つからないので、IOSシミュレータを使ってXcodeでアプリをテストするときに電子メールを送信する方法はありません。私はMacにIphoneデバイスを接続し、このデバイス上のXcodeからアプリケーションを実行すると、Macで私のiPhoneの画面を表示する方法があるので、物理デバイス上で実行されているアプリケーションを制御できます。マウスをクリックして、iPhoneに表示された画面から電子メールを送信します。どうもありがとう – 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. 
関連する問題