#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

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Objective-C 中三种产生随机数的方法详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。