iOS 实现发送邮件和短信

导入framework

导入MessageUI.framework

发送邮件

首先需要在.h文件中实现下面的操作:
#import <MessageUI/MFMailComposeViewController.h>
然后实现下面的代理 :MFMailComposeViewControllerDelegate

- (void)businessContactWithMail
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
        {
            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;
            [picker setToRecipients:[NSArray arrayWithObject:@""]];
            [picker setSubject:[NSString stringWithFormat:@""]];
            
            UIDevice *device = [UIDevice currentDevice];
            
            NSString *content=[NSString stringWithFormat:@""];
            [picker setMessageBody:content isHTML:NO];
            [self presentViewController:picker animated:YES completion:^{
                
            }];
        }
        else
        {
            [NDUI showAlertWithInfo:@"您的设备尚未配置邮件账号" title:@"提示"];
        }
    } else {
        [NDUI showAlertWithInfo:@"您的设备不支持邮件功能" title:@"提示"];
    }
}
#pragma mark - Mail and SMS delegate
// 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 dismissViewControllerAnimated:YES completion:nil];
}


短信发送


首先需要在.h文件中实现下面的操作:
#import <MessageUI/MFMailComposeViewController.h>
然后实现下面的代理 :MFMessageComposeViewControllerDelegate
- (void)shareWithSMS
{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
	BOOL smsEnabled=NO;
	if (messageClass != nil) { 			
		// Check whether the current device is configured for sending SMS messages
		if ([messageClass canSendText]) {
            MFMessageComposeViewController *smsClident = [[MFMessageComposeViewController alloc] init];
            smsClident.messageComposeDelegate = self;
            
            NSString *messageBody = [NSString stringWithFormat:@""];
            
            [smsClident setBody:messageBody];
            [self presentModalViewController:smsClident animated:YES];
		}
		else {	
			[NDUI showAlertWithInfo:@"您的设备不支持此短信功能" title:@"提示"];
		}
	}
	else {
		
		[NDUI showAlertWithInfo:@"您的设备不支持短信功能" title:@"提示"];
	}
    if(!smsEnabled){
        return;
    }
    
	
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissModalViewControllerAnimated:YES];
}




你可能感兴趣的:(ios,邮件,Objective-C,email,短信)