IOS 开发,调用打电话,发短信,打开网址

时间:2022-11-10 13:48:05

1、调用 自带mail

 

1 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://zhangzhi11@163.com"]];

 

2、调用 电话phone

 

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

 

3、调用 SMS

 

1 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];

 

4、调用自带 浏览器 safari

1 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.object-c.com"]];

 

调用phone可以传递号码,调用SMS 只能设定号码,不能初始化SMS内容。

若需要传递内容可以做如下操作:

加入:MessageUI.framework

 

#import <MessageUI/MFMessageComposeViewController.h>

 

 

实现代理:MFMessageComposeViewControllerDelegate

代码如下:

 

 1 //调用sendSMS函数
 2 //内容,收件人列表
 3 - (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
 4 {
 5     MFMessageComposeViewController *mfmessagecomposecontroller = [[[MFMessageComposeViewController alloc] init] autorelease];
 6     if([MFMessageComposeViewController canSendText])
 7     {
 8         mfmessagecomposecontroller.body = bodyOfMessage;   
 9         mfmessagecomposecontroller.recipients = recipients;
10         mfmessagecomposecontroller.messageComposeDelegate = self;
11         [self presentModalViewController:mfmessagecomposecontrolleranimated:YES];
12     }   
13 }
14 
15 // 处理发送完的响应结果
16 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
17 {
18   [self dismissModalViewControllerAnimated:YES];
19  
20   if (result == MessageComposeResultCancelled)
21     NSLog(@"取消发送")
22   else if (result == MessageComposeResultSent)
23     NSLog(@"发送成功")  
24   else 
25     NSLog(@"发送失败")  
26 }
27 //发送邮件的为:
28 //导入#import <MessageUI/MFMailComposeViewController.h>
29 //实现代理:MFMailComposeViewControllerDelegate
30 //发送邮件
31 
32 -(void)sendMail:(NSString *)subject content:(NSString *)content{
33     MFMailComposeViewController *controller = [[[MFMailComposeViewController alloc] init] autorelease];
34     if([MFMailComposeViewController canSendMail])
35     {
36         [controller setSubject:subject];
37         [controller setMessageBody:content isHTML:NO];
38         controller.mailComposeDelegate = self;
39         [self presentModalViewController:controller animated:YES];
40     }    
41 
42 }
43 
44 
45 //邮件完成处理
46 -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
47     [self dismissModalViewControllerAnimated:YES];
48     if (result == MessageComposeResultCancelled)
49         NSLog(@"取消发送");
50     else if (result == MessageComposeResultSent)
51         NSLog(@"发送成功"); 
52     else 
53         NSLog(@"发送失败");  
54 }

 

默认发送短信的界面为英文的,解决办法为:

在.xib 中的Localization添加一組chinese就ok了