标签:
电话、短信是手机的基础功能,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 alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];
    //           
 NSLog(@"str======%@",str);
[[UIApplication
 sharedApplication] openURL:[NSURL URLWithString:str]];
    
2,这种方法,打完电话后还会回到原来的程序,也会弹出提示,推荐这种
NSMutableString *
 str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];
    UIWebView *
 callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    [callWebview release];
    [str release];
3,这种方法也会回去到原来的程序里(注意这里的telprompt),也会弹出提示
NSMutableString *
 str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"186xxxx6979"];
    //           
 NSLog(@"str======%@",str);
 
   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]
 
电话、短信是手机的基础功能,iOS中提供了接口,让我们调用。这篇文章简单的介绍一下iOS的打电话、发短信在程序中怎么调用。
2、发短信
iOS中可以使用两种方式发送短信,最简单是使用openURL:
 
- [[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>
- #import <MessageUI/MessageUI.h>  
-   
- @interface DemoViewController : UIViewController <MFMessageComposeViewControllerDelegate>  
-   
- @end  
 
调用MFMessageComposeViewController,同时实现协议MFMessageComposeViewControllerDelegate。
- - (void)showMessageView  
- {  
-       
-     if( [MFMessageComposeViewController canSendText] ){  
-           
-         MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; 
-           
-         controller.recipients = [NSArray arrayWithObject:@"10010"];       
-         controller.body = @"测试发短信";          
-         controller.messageComposeDelegate = self;  
-   
-         [self presentModalViewController:controller animated:YES];  
-           
-         [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];
-     }else{  
-           
-         [self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];          
-     }      
- }  
-   
-   
-   
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{  
-       
-     [controller dismissModalViewControllerAnimated:NO];
-       
-     switch ( result ) {  
-               
-         case MessageComposeResultCancelled:  
-   
-             [self alertWithTitle:@"提示信息" msg:@"发送取消"];   
-             break;  
-         case MessageComposeResultFailed:
-             [self alertWithTitle:@"提示信息" msg:@"发送成功"];   
-             break;  
-         case MessageComposeResultSent:  
-             [self alertWithTitle:@"提示信息" msg:@"发送失败"];   
-             break;  
-         default:  
-             break;   
-     }  
- }  
-   
-   
- - (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {  
-   
-       
-     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title  
-            message:msg  
-            delegate:self  
-            cancelButtonTitle:nil  
-            otherButtonTitles:@"确定", nil];  
-                            
-    [alert show];  
-                    
- }  
-             
 
参考:
http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html
 
iOS学习笔记(十四)——打电话、发短信
标签:
原文地址:http://www.cnblogs.com/A--G/p/4538271.html