Objective C: Send email without leaving app

前端 未结 3 1775
悲哀的现实
悲哀的现实 2020-11-30 22:19

How do I send an email within an app without leaving the app.

This works:

-(void) sendEmailTo:(NSString *)to withSubject:(NSString *)subject withBo         


        
3条回答
  •  星月不相逢
    2020-11-30 23:00

    1. Add MessageUI framework:

      • Click on the project
      • Select "Build Phases"
      • Expand "Link Binary With Libraries"
      • Click "+" and type "Message" to find "MessageUI" framework, then add.
    2. In current view controller add import and implement a protocol:

      #import  
      #import  
      @interface MyViewController : UIViewController
      

    Add methods:

        -(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:@"myFriends@email.com"]];
                [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];
        }
    

提交回复
热议问题