您的位置:首页 > 移动开发 > IOS开发

iOS 实用功能汇总(2)

2017-04-13 16:52 162 查看

1.打电话

1.第一种方法

这种方法在iOS8上测试的是不会出现弹框,直接进入到拨打电话界面,结束通话后会回到App界面。先跳出程序再进入到到系统的打电话程。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",@"10010"]]];


2.第二种方法(慎用)

这种方法提交审核的时候,可能会被拒,所以慎用。。。最好不用。。。

这种方法会出现弹框,结束通话后会回到App界面。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",@"10010"]]];


3.第三种方法

这种方法也会有弹框,结束通话后回到App界面,这种较第一种的区别在于: 一直都在自己的app中运行,没有出去过。

NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"10010"];
UIWebView *callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[[UIApplication sharedApplication].keyWindow addSubview:callWebview];


发短信

需要导入MessageUI框架,
#import <MessageUI/MessageUI.h>


if (![MFMessageComposeViewController canSendText]) return;
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];

// 设置短信内容(这里可以设置短信内容,也可以不设置)
//vc.body = @"Hello!";

// 设置收件人列表(这里可以指定收件人,也可以不设置)
//vc.recipients = @[@"10010", @"10086"];

// 设置代理
vc.messageComposeDelegate = self;

// 显示控制器
[self presentViewController:vc animated:YES completion:nil];


还要实现代理方法
MFMessageComposeViewControllerDelegate
:

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

// 关闭短信界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MessageComposeResultCancelled) {
NSLog(@"取消发送");
} else if (result == MessageComposeResultSent) {
NSLog(@"已经发出");
} else {

NSLog(@"发送失败");
}
}


发邮件

和发短信一样,只不过是MFMailComposeViewController

/ 不能发邮件
if (![MFMailComposeViewController canSendMail]) return;

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

// 设置邮件主题
[vc setSubject:@"Hello"];
// 设置邮件内容
[vc setMessageBody:@"How are you?" isHTML:NO];
// 设置收件人列表
[vc setToRecipients:@[@"66666@qq.com"]];
// 设置抄送人列表
[vc setCcRecipients:@[@"66666@qq.com"]];
// 设置密送人列表
[vc setBccRecipients:@[@"66666@qq.com"]];

// 添加附件(一张图片)
UIImage *image = [UIImage imageNamed:@"pic.jpeg"];
NSData *data = UIImageJPEGRepresentation(image, 0.5);
[vc addAttachmentData:data mimeType:@"image/jepg" fileName:@"pic.jpeg"];

// 设置代理
vc.mailComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];


同样实现代理:

- (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(@"发送失败");
}
}


跳转AppStore

NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  实用功能