iOS点击发送短信按钮跳转到手机短信界面实现发送短信

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#import "ViewController.h"
 
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",NSHomeDirectory());
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 40, 30);
    [button setTitle:@"发送" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.backgroundColor=[UIColor redColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}
 
- (void)buttonClick {
    
    //首先判断当前设备是否可以发送短信
    if([MFMessageComposeViewController canSendText])
    {
        MFMessageComposeViewController *mc=[[MFMessageComposeViewController alloc] init];
         
        //设置委托
        mc.messageComposeDelegate=self;
         
        //短信内容
        mc.body=[[NSString alloc] initWithUTF8String:"你好啊" ];
         
        //设置短信收件方
        mc.recipients=[NSArray arrayWithObject:@"10010"];
         
        [self presentViewController:mc animated:YES completion:nil];
    }else{
     
        [[[UIAlertView alloc] initWithTitle:nil message:@"抱歉,没有此功能" delegate:self cancelButtonTitle:@"?"
                          otherButtonTitles:nil, nil] show];
     
    }
}
     
//短信发送的处理结果
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    switch (result)
    {
        case MessageComposeResultSent:
            NSLog(@"text message sent successfully");
            break;
        case MessageComposeResultCancelled:
            NSLog(@"text message cancelled");
            [self dismissViewControllerAnimated:YES completion:nil];
            break;
        case MessageComposeResultFailed:
            NSLog(@"text message failed");
            break;
        default:
            NSLog(@"error happens");
            break;  
    }
     
}
@end

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Python 判断一个进程是否存在

下一篇:Python生成验证码实例