当前位置: 代码迷 >> 综合 >> iOS打电话、发短信功能
  详细解决方案

iOS打电话、发短信功能

热度:91   发布时间:2024-01-20 19:39:44.0

//一、打电话

    //1、用UIWebView加载电话,这种是合法的,可以上App Store。

   UIWebView*callWebview =[[UIWebViewalloc] init];

   NSURL *telURL =[NSURLURLWithString:@"tel:10010"];

    [callWebviewloadRequest:[NSURLRequestrequestWithURL:telURL]];

    //记得添加到view上

    [self.viewaddSubview:callWebview];

    

    

    //2、私有方法,貌似不能上App Store。

    [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"telprompt://10010"]];

    //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];


参考:

// 第一种方式打电话

[html]  view plain  copy
  1. - (void)makeACall{  
  2.      NSString *number = @"";// 此处读入电话号码  
  3.      NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number]; //number为号码字符串 如果使用这个方法 结束电话之后会进入联系人列表  
  4. }  

// 第二种方式打电话

[html]  view plain  copy
  1. - (void)makeACall{  
  2.     NSString *num = [[NSString alloc] initWithFormat:@"telprompt://%@",number]; //而这个方法则打电话前先弹框  是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核  
  3.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号  
  4. }  

//  第三种方式打电话

// 下面的代码能在应用中添加一个电话按钮,用UIWebView加载电话,这种是合法的,可以上App Store的。

[html]  view plain  copy
  1. -(void)makeACall{  
  2.     NSString *phoneNum = @"";// 电话号码  
  3.     NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];  
  4.     if ( !phoneCallWebView ) {  
  5.         phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];// 这个webView只是一个后台的View 不需要add到页面上来  效果跟方法二一样 但是这个方法是合法的  
  6.     }   
  7.     [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];  
  8. }  



//二、发短信

- (void)viewDidLoad {

    [superviewDidLoad];

   

    //iOS中可以使用两种方式发送短信,

    //1、使用openURL(但无法指定短信内容)

    [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"sms://10010"]];//发短信

    

    //2、加入MessageUI.framework

    //#import <MessageUI/MessageUI.h>

    //添加协议:<MFMessageComposeViewControllerDelegate>

    [selfshowMessage];

}


- (void)showMessage

{

    

    if( [MFMessageComposeViewControllercanSendText] ){

        

        MFMessageComposeViewController * controller = [[MFMessageComposeViewControlleralloc]init];//autorelease];

        

        controller.recipients = [NSArrayarrayWithObject:@"10010"];

        controller.body =@"测试发短信";

        controller.messageComposeDelegate =self;

        

        [selfpresentViewController:controller animated:YEScompletion:nil];

        

        [[[[controller viewControllers]lastObject] navigationItem]setTitle:@"测试短信"];//修改短信界面标题

    }else{

        

        [selfalertWithTitle:@"提示信息"msg:@"设备没有短信功能"];

    }

}


//MFMessageComposeViewControllerDelegate

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{

    

    [controller dismissViewControllerAnimated:YEScompletion:nil];//关键的一句   不能为YES

    

   switch ( result ) {

            

        caseMessageComposeResultCancelled:

            

            [selfalertWithTitle:@"提示信息"msg:@"发送取消"];

           break;

        caseMessageComposeResultFailed:// send failed

            [selfalertWithTitle:@"提示信息"msg:@"发送成功"];

           break;

        caseMessageComposeResultSent:

            [selfalertWithTitle:@"提示信息"msg:@"发送失败"];

           break;

       default:

           break;

    }

}


- (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {

    

    

   UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:title

                                                   message:msg

                                                  delegate:self

                                         cancelButtonTitle:nil

                                         otherButtonTitles:@"确定",nil];

    

    [alertshow];

    

}



  相关解决方案