iOS程序内发短信

1、程序外发短信

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10086"]];

  

2、程序内发短信,发完短信自动返回应用

  (1)首先导入框架MessageUI.framework

  (2) 引入头文件 #import <MessageUI/MessageUI.h>

  (3) 实现代理方法 <MFMessageComposeViewControllerDelegate, UINavigationControllerDelegate>

3、直接上代码

- (void)showMessageView{
    
    if ([MFMessageComposeViewController canSendText]) {
        
        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        
        controller.recipients = [NSArray arrayWithObject:@"10086"];
        
        controller.body = @"测试发短信";
        
        controller.messageComposeDelegate = self;
        
         [self presentViewController:controller animated:YES completion:nil];
        
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];
    }else{
        
        [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];
    }
}

  

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    switch (result) {
        case MessageComposeResultCancelled:
            [self alertWithTitle:@"提示信息" msg:@"发送取消"];
            break;
            
        case MessageComposeResultFailed:
            [self alertWithTitle:@"提示信息" msg:@"发送失败"];
            break;
            
        case MessageComposeResultSent:
            [self alertWithTitle:@"提示信息" msg:@"发送成功"];
            break;
            
        default:
            break;
    }
    
}


- (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {
    
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"确定", nil];
    
    [alert show];  
    
}

  

posted @ 2016-03-14 10:48  Mr_tao  阅读(1088)  评论(0编辑  收藏  举报