IOS疯狂基础之MessageUI—短信

我们知道可以使用这样的代码在iphone中发送短信:[[UIApplication sharedApplication] openURL:@"sms:12345678"];
但这样的方式无法指定短信内容。那么我们可以使用MessageUI框架。

1. 导入框架:MessageUI.framework
2. 添加协议:
3. #import 、#import "DeviceDetection.h"



代码:


- (void)showMessageView
{
    if( [MFMessageComposeViewController canSendText] )// 判断设备能不能发送短信
    {
        MFMessageComposeViewController*picker = [[MFMessageComposeViewControlleralloc] init];
        // 设置委托
        picker.messageComposeDelegate= self;
        // 默认信息内容
        picker.body = @"nihao";
        // 默认收件人(可多个)
        picker.recipients = [NSArray arrayWithObject:@"12345678901", nil];
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                        message:@"该设备不支持短信功能"
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    }
}



#pragma mark -
#pragma mark MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result){
        case MessageComposeResultCancelled:
            NSLog(@"取消发送");
            break;
        case MessageComposeResultFailed:
            NSLog(@"发送失败");
            break;
     

你可能感兴趣的:(IOS)