您的位置:首页 > 其它

调用手机打电话发信息等

2016-03-29 10:34 309 查看
1、调用 自带mail

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzlzh.com"]];

2、调用 电话phone

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

3、调用 SMS

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

4、调用自带 浏览器 safari

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.hzlzh.com"]];

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

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

加入:MessageUI.framework

#import <MessageUI/MFMessageComposeViewController.h>

实现代理:MFMessageComposeViewControllerDelegate

调用sendSMS函数

//内容,收件人列表

- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients

{

MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];

if([MFMessageComposeViewController canSendText])

{

controller.body = bodyOfMessage;

controller.recipients = recipients;

controller.messageComposeDelegate = self;

[self presentModalViewController:controller animated:YES];

}

}

// 处理发送完的响应结果

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

{

[self dismissModalViewControllerAnimated:YES];

if (result == MessageComposeResultCancelled)

NSLog(@"Message cancelled")

else if (result == MessageComposeResultSent)

NSLog(@"Message sent")

else

NSLog(@"Message failed")

}

发送邮件的为:

导入#import <MessageUI/MFMailComposeViewController.h>

实现代理:MFMailComposeViewControllerDelegate

//发送邮件

-(void)sendMail:(NSString *)subject content:(NSString *)content{

MFMailComposeViewController *controller = [[[MFMailComposeViewController alloc] init] autorelease];

if([MFMailComposeViewController canSendMail])

{

[controller setSubject:subject];

[controller setMessageBody:content isHTML:NO];

controller.mailComposeDelegate = self;

[self presentModalViewController:controller animated:YES];

}

}

//邮件完成处理

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error{

[self dismissModalViewControllerAnimated:YES];

if (result == MessageComposeResultCancelled)

NSLog(@"Message cancelled");

else if (result
== MessageComposeResultSent)

NSLog(@"Message sent");

else

NSLog(@"Message failed");

}

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

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

最简单最直接的方式:直接跳到拨号界面

1

NSURL *url = [NSURL URLWithString:@"tel://10010"];

[[UIApplication sharedApplication] openURL:url];

缺点

电话打完后,不会自动回到原应用,直接停留在通话记录界面

2

拨号之前会弹框询问用户是否拨号,拨完后能自动回到原应用

NSURL *url = [NSURL URLWithString:@"telprompt://10010"];

[[UIApplication sharedApplication] openURL:url];

缺点

因为是私有API,所以可能不会被审核通过

3

创建一个UIWebView来加载URL,拨完后能自动回到原应用

if (_webView == nil) {

_webView = [[UIWebView alloc] initWithFrame:CGRectZero];

}

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];

需要注意的是:这个webView千万不要添加到界面上来,不然会挡住其他界面

发短信-方法1

直接跳到发短信界面,但是不能指定短信内容,而且不能自动回到原应用

NSURL *url = [NSURL URLWithString:@"sms://10010"];

[[UIApplication sharedApplication] openURL:url];

发短信-方法2

如果想指定短信内容,那就得使用MessageUI框架

包含主头文件

#import

显示发短信的控制器

MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];

// 设置短信内容

vc.body = @"吃饭了没?";

// 设置收件人列表

vc.recipients = @[@"10010", @"02010010"];

// 设置代理

vc.messageComposeDelegate = self;

// 显示控制器

[self presentViewController:vc animated:YES completion:nil];

代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用

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

{

// 关闭短信界面

[controller dismissViewControllerAnimated:YES completion:nil];

if (result == MessageComposeResultCancelled) {

NSLog(@"取消发送");

} else if (result == MessageComposeResultSent) {

NSLog(@"已经发出");

} else {

NSLog(@"发送失败");

}

}

发邮件-方法1

用自带的邮件客户端,发完邮件后不会自动回到原应用

NSURL *url = [NSURL URLWithString:@"mailto://10010@qq.com"];

[[UIApplication sharedApplication] openURL:url];

发邮件-方法2

跟发短信的第2种方法差不多,只不过控制器类名叫做:MFMailComposeViewController

假设发送的邮件内容如右图所示,代码实现看备注

邮件发送后的代理方法回调,发完后会自动回到原应用

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error

{

// 关闭邮件界面

[controller dismissViewControllerAnimated:YES completion:nil];

if (result == MFMailComposeResultCancelled) {

NSLog(@"取消发送");

} else if (result == MFMailComposeResultSent) {

NSLog(@"已经发出");

} else {

NSLog(@"发送失败");

}

}

打开其他常见文件

如果想打开一些常见文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打开

只需要告诉UIWebView文件的URL即可

至于打开一个远程的共享资源,比如http协议的,也可以调用系统自带的Safari浏览器

NSURL *url = [NSURL URLWithString:@”http://www.baidu.com"];

[[UIApplication sharedApplication] openURL:url];

有时候,需要在本应用中打开其他应用,比如从A应用中跳转到B应用

首先,B应用得有自己的URL地址(在Info.plist中配置)

B应用的URL地址就是:mj://ios.itcast.cn

接着在A应用中使用UIApplication完成跳转

NSURL *url = [NSURL URLWithString:@"mj://ios.itcast.cn"];

[[UIApplication sharedApplication] openURL:url];

应用评分

为了提高应用的用户体验,经常需要邀请用户对应用进行评分

应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论

如何跳转到AppStore,并且展示自己的应用

方法1

NSString *appid = @"444934666";

NSString *str = [NSString stringWithFormat:

@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appid];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

方法2

NSString *str = [NSString stringWithFormat:

@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

#pragma mark - 使用系统邮件客户端发送邮件

-(void)launchMailApp

{

NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];

//添加收件人

NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];

[mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];

//添加抄送

NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];

[mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];

//添加密送

NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];

[mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];

//添加主题

[mailUrl appendString:@"&subject=my email"];

//添加邮件内容

[mailUrl appendString:@"&body=<b>email</b> body!"];

NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: