电话、短信是手机的基础功能,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.
2、发短信
iOS中可以使用两种方式发送短信,最简单是使用openURL:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://10010"]];//发短信
上面方式无法指定短信内容,iOS4.0新加入了MFMessageComposeViewCont<wbr style="font-family: simsun; line-height: 21px;"><span style="background-color: rgb(255, 255, 255); font-family: simsun; line-height: 21px;">roller和MFMessageComposeViewCont</span><wbr style="font-family: simsun; line-height: 21px;"><span style="background-color: rgb(255, 255, 255); font-family: simsun; line-height: 21px;">rollerDelegate,</span><span style="background-color: rgb(255, 255, 255); font-family: simsun; line-height: 21px;">提供了发</span><span style="background-color: rgb(255, 255, 255); font-family: simsun; line-height: 21px;">送短信的接口,可以像发送邮件那样不用跳出程序来发送短信. 介绍可参阅<a href="http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html" target="_blank">Message UIFramework Reference</a></span></wbr></wbr>
MFMessageComposeViewController提供了操作界面使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.
messageComposeDelegate :代理,处理发送结果
recipients :收信人<列表,支持群发>
body :短信内容
Frameworks中要引入MessageUI.framework
#import<wbr></wbr><MessageUI/MessageUI.h>
添加协议:<MFMessageComposeViewCont<wbr>rollerDelegate></wbr>
#import <MessageUI/MessageUI.h>
@interface DemoViewController : UIViewController <MFMessageComposeViewControllerDelegate>
@end
调用MFMessageComposeViewController,同时实现协议MFMessageComposeViewControllerDelegate。
- (void)showMessageView
{
if( [MFMessageComposeViewController canSendText] ){
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];
controller.recipients = [NSArray arrayWithObject:@"10010"];
controller.body = @"测试发短信";
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];//修改短信界面标题
}else{
[self alertWithTitle:@"提示信息" msg:@"设备没有短信功能"];
}
}
//MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
[controller dismissModalViewControllerAnimated:NO];//关键的一句 不能为YES
switch ( result ) {
case MessageComposeResultCancelled:
[self alertWithTitle:@"提示信息" msg:@"发送取消"];
break;
case MessageComposeResultFailed:// send failed
[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];
}
发邮件可参考
* iOS入门群:83702688
* android开发进阶群:241395671
*/
参考:
http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html
分享到:
相关推荐
IOS打电话、发短信、发邮件的封装类,使用非常方便
在ios的程序内部实现打电话发短信 调用系统接口
iOS游戏应用源代码——iOS-Gamer-Cocos2D-86714e3.zip
iOS游戏应用源代码——sergiomtzlosa-MultiContactsSelector-ios.zip
iOS游戏应用源代码——jordim-InfiniteHorizontalScroll-iOS.zip
iOS开发练习小程序——秒表参考.pdf
iOS游戏应用源代码——vssilentswitch.zip
iOS游戏应用源代码——AjComboBox.zip
iOS游戏应用源代码——iosdeveloper-PresetColorPicker-ed36893.zip
IOS源码实现了: 1)打电话:用户在初始化该类后,调用打电话的方法,传入相应参数,即可完成调用系统打电话的功能。 2)发短信:用户在初始化该类后,调用发短信的方法,传入相应参数,即可完成调用系统发短信功能...
iOS原生地图开发进阶——使用导航和附近兴趣点检索1
iOS游戏应用源代码——vgrichina-ios-calendar-baf4495.zip
iOS游戏应用源代码——cocos2d-andrew0-cocoshop.zip
iOS游戏应用源代码——iosdeveloper-TwitterFeed-d779d5a.zip
iOS游戏应用源代码——iosdeveloper-ImageCropper-04cef90.zip
iOS游戏应用源代码——iosdeveloper-LaunchImageTransition-14b7fd4.zip
iOS游戏应用源代码——doukasd-iOS-Components-4678a8b.zip
iOS游戏应用源代码——SlavaBushtruk-Alterplay-iOS-dev-tips.zip
iOS游戏应用源代码——overlordnyaldee-DrawBattlefield.zip
iOS游戏应用源代码——rauluranga-TotemBalance.zip