IOS 調發短信和郵件界面



蘋果官方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];
}





注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com