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

#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

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/3344.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论