zoukankan      html  css  js  c++  java
  • iOS学习笔记(十四)——打电话、发短信

    电话、短信是手机的基础功能,iOS中提供了接口,让我们调用。这篇文章简单的介绍一下iOS的打电话、发短信在程序中怎么调用。

    1、打电话

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

     

           使用openURL这个API打电话结束后,返回的是系统的拨打电话界面,如何才能返回自己的应用呢?有两种方法与大家分享。

     

    第一种是用UIWebView加载电话,这种是合法的,可以上App Store的。

    代码如下:

        UIWebView*callWebview =[[UIWebView alloc] init];
        NSURL *telURL =[NSURL URLWithString:@"tel:10010"];
        [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
        //记得添加到view上
        [self.view addSubview:callWebview];
    



    第二种是私有方法,不能上App Store的(自己没试过)。 

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

     

    上面的代码只是把第一个方法中的tel为telprompt.

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示
    NSMutableString * str=[[NSMutableString allocinitWithFormat:@"tel:%@",@"186xxxx6979"];
        //            NSLog(@"str======%@",str);
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        
    2,这种方法,打完电话后还会回到原来的程序,也会弹出提示,推荐这种
    NSMutableString * str=[[NSMutableString allocinitWithFormat:@"tel:%@",@"186xxxx6979"];
        UIWebView * callWebview = [[UIWebView allocinit];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
        [callWebview release];
        [str release];

    3,这种方法也会回去到原来的程序里(注意这里的telprompt),也会弹出提示
    NSMutableString * str=[[NSMutableString allocinitWithFormat:@"telprompt://%@",@"186xxxx6979"];     //            NSLog(@"str======%@",str);     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]

    电话、短信是手机的基础功能,iOS中提供了接口,让我们调用。这篇文章简单的介绍一下iOS的打电话、发短信在程序中怎么调用。

    2、发短信

    iOS中可以使用两种方式发送短信,最简单是使用openURL:

     

    1. [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10010"]];//发短信  


        上面方式无法指定短信内容,iOS4.0新加入了MFMessageComposeViewController和MFMessageComposeViewControllerDelegate,提供了发送短信的接口,可以像发送邮件那样不用跳出程序来发送短信. 介绍可参阅Message UIFramework Reference


            MFMessageComposeViewController提供了操作界面使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.

     

    messageComposeDelegate :代理,处理发送结果

    recipients  :收信人<列表,支持群发>

    body :短信内容

    Frameworks中要引入MessageUI.framework 

    #import <MessageUI/MessageUI.h>
    添加协议:<MFMessageComposeViewControllerDelegate>

    1. #import <MessageUI/MessageUI.h>  
    2.   
    3. @interface DemoViewController : UIViewController <MFMessageComposeViewControllerDelegate>  
    4.   
    5. @end  


    调用MFMessageComposeViewController同时实现协议MFMessageComposeViewControllerDelegate。

    1. - (void)showMessageView  
    2. {  
    3.       
    4.     if( [MFMessageComposeViewController canSendText] ){  
    5.           
    6.         MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];  
    7.           
    8.         controller.recipients = [NSArray arrayWithObject:@"10010"];       
    9.         controller.body = @"测试发短信";          
    10.         controller.messageComposeDelegate = self;  
    11.   
    12.         [self presentModalViewController:controller animated:YES];  
    13.           
    14.         [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];//修改短信界面标题  
    15.     }else{  
    16.           
    17.         [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];          
    18.     }      
    19. }  
    20.   
    21.   
    22. //MFMessageComposeViewControllerDelegate  
    23.   
    24. - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{  
    25.       
    26.     [controller dismissModalViewControllerAnimated:NO];//关键的一句   不能为YES  
    27.       
    28.     switch ( result ) {  
    29.               
    30.         case MessageComposeResultCancelled:  
    31.   
    32.             [self alertWithTitle:@"提示信息" msg:@"发送取消"];   
    33.             break;  
    34.         case MessageComposeResultFailed:// send failed  
    35.             [self alertWithTitle:@"提示信息" msg:@"发送成功"];   
    36.             break;  
    37.         case MessageComposeResultSent:  
    38.             [self alertWithTitle:@"提示信息" msg:@"发送失败"];   
    39.             break;  
    40.         default:  
    41.             break;   
    42.     }  
    43. }  
    44.   
    45.   
    46. - (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {  
    47.   
    48.       
    49.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title  
    50.            message:msg  
    51.            delegate:self  
    52.            cancelButtonTitle:nil  
    53.            otherButtonTitles:@"确定", nil];  
    54.                            
    55.    [alert show];  
    56.                    
    57. }  
    58.             


    参考:

    http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html

  • 相关阅读:
    [java,2019-01-28] 枪手博弈,谁才是最后赢家
    [java,2019-01-25] 图片和二进制互转
    [java,2019-01-15] word转pdf
    [python,2018-06-29] 37%法则及其拓展解决恋爱问题
    [java,2018-06-26] 扑克牌抽牌求和问题
    [python,2018-06-25] 高德纳箭号表示法
    [java,2017-06-12] myEclipse双击无法打开文件
    OpenGL核心技术之法线贴图
    游戏中水的渲染技术系列一
    Unity 3D实现帧同步技术
  • 原文地址:https://www.cnblogs.com/A--G/p/4538271.html
Copyright © 2011-2022 走看看