IOS 调发短信和邮件界面

时间:2021-12-22 08:57:48


苹果官方demo下载地址:https://developer.apple.com/library/ios/samplecode/MessageComposer/Introduction/Intro.html

发短信比较简单:

1.首先倒入系统库:MessageUI.framework  CoreGraphics.framework


2.  实现代理  

    MFMailComposeViewControllerDelegate,    ----发邮件

    MFMessageComposeViewControllerDelegate,---发短信

    UINavigationControllerDelegate

3. 实现:(包括发短信和发邮件)


以下代码为官方的demo中的demo

#pragma mark - Compose Mail/SMS

// -------------------------------------------------------------------------------
//displayMailComposerSheet
// Displays an email composition interface inside the application.
// Populates all the Mail fields.
// -------------------------------------------------------------------------------
- (void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello from California!"];

// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];

[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];

[self presentViewController:picker animated:YES completion:NULL];
}

// -------------------------------------------------------------------------------
//displayMailComposerSheet
// Displays an SMS composition interface inside the application.
// -------------------------------------------------------------------------------
- (void)displaySMSComposerSheet
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;

// You can specify one or more preconfigured recipients. The user has
// the option to remove or add recipients from the message composer view
// controller.
<span style="color:#ff0000;"> /* picker.recipients = @[@"Phone number here"]; */ 此处可以填写需要发送的手机号码</span>

// You can specify the initial message text that will appear in the message
// composer view controller.
<span style="color:#ff0000;"> picker.body = @"Hello from California!"; 短信内容</span>
<span style="color:#ff0000;"> 打开短信界面时,就会看到短信内容和手机号码自动填充好了。</span>
[self presentViewController:picker animated:YES completion:NULL];
}


#pragma mark - Delegate Methods

// -------------------------------------------------------------------------------
//mailComposeController:didFinishWithResult:
// Dismisses the email composition interface when users tap Cancel or Send.
// Proceeds to update the message field with the result of the operation.
// -------------------------------------------------------------------------------
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
self.feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
self.feedbackMsg.text = @"Result: Mail sending canceled";
break;
case MFMailComposeResultSaved:
self.feedbackMsg.text = @"Result: Mail saved";
break;
case MFMailComposeResultSent:
self.feedbackMsg.text = @"Result: Mail sent";
break;
case MFMailComposeResultFailed:
self.feedbackMsg.text = @"Result: Mail sending failed";
break;
default:
self.feedbackMsg.text = @"Result: Mail not sent";
break;
}

[self dismissViewControllerAnimated:YES completion:NULL];
}

// -------------------------------------------------------------------------------
//messageComposeViewController:didFinishWithResult:
// Dismisses the message composition interface when users tap Cancel or Send.
// Proceeds to update the feedback message field with the result of the
// operation.
// -------------------------------------------------------------------------------
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
self.feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
self.feedbackMsg.text = @"Result: SMS sending canceled";
break;
case MessageComposeResultSent:
self.feedbackMsg.text = @"Result: SMS sent";
break;
case MessageComposeResultFailed:
self.feedbackMsg.text = @"Result: SMS sending failed";
break;
default:
self.feedbackMsg.text = @"Result: SMS not sent";
break;
}

[self dismissViewControllerAnimated:YES completion:NULL];
}