iOS 打电话 发短信 邮件 打开网址 调用应用等合集

时间:2022-11-10 13:52:55
iOS中的很多功能都是非常简单的,几行代码就搞定了,比如打电话、打开网址、发邮件、发短信等,这里总结几个比较常用的:
1.打电话 方式一:最简单最直接的方式直接跳到拨号界面

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

[[UIApplication sharedApplication] openURL:url];

缺点:

 

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


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

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

[[UIApplication sharedApplication] openURL:url];

缺点:

此处调用的是私有API,所以可能不会被审核通过,一般制作越狱后的插件或APP使用


方式三(目前发现最终极的方法):创建一个UIWebView来加载URL,拨完后能自动回到应用

if (_telWebView == nil) {

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

}

[_telWebView loadRequest:[NSURLRequest requestWithURL:[NSURLURLWithString:@"tel://10010"]]];

 

 

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


2.发短信

方式一:直接跳到发短信界面

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

[[UIApplication sharedApplication] openURL:url];

缺点:不能指定短信内容而且不能自动回到应用


方式二(推荐):发完后会自动回到原应用且可自定义短信内容 首先导入MessageUI框架并包含主头文件#import <</span>MessageUI/MessageUI.h>

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) {

        SJLog(@"取消发送!");

    } else if(result == MessageComposeResultSent) {

        SJLog(@"发送成功!");

    } else {

        SJLog(@"发送失败!");

    }

 

}


3.发邮件 方式一:

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

[[UIApplication sharedApplication] openURL:url];

缺点:发完邮件后不会自动回到原应用


方式二:

 

使用控制器:MFMailComposeViewController

// 不能发邮件

if (![MFMailComposeViewController canSendMail])

{

    return;

}


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


// 设置邮件主题

[vc setSubject:@"会议"];

// 设置邮件内容

[vc setMessageBody:@"今天下午开会吧" isHTML:NO];

// 设置收件人列表

[vc setToRecipients:@[@"643055866@qq.com"]];

// 设置抄送人列表

[vc setCcRecipients:@[@"1234@qq.com"]];

// 设置密送人列表

[vc setBccRecipients:@[@"56789@qq.com"]];


// 添加附件(一张图片)

UIImage *image = [UIImage imageNamed:@"lufy.jpeg"];

NSData *data = UIImageJPEGRepresentation(image, 0.5);

[vc addAttachmentData:data mimeType:@"image/jepg"fileName:@"lufy.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) {

        SJLog(@"取消发送!");

    } else if (result == MFMailComposeResultSent) {

        SJLog(@"发送成功!");

    } else {

        SJLog(@"发送失败!");

    }

 

}


4.打开常见文件

我们常想打开一些常见文件,比如htmltxtPDFPPT等;但不知道用什么APP打开,其实它们都可以使用UIWebView打开,我们要做得只是告诉UIWebView文件的URL即可


 

如果需要打开一个远程的共享资源,比如:http,也可以调用Safari浏览器:

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

[[UIApplication sharedApplicationopenURL:url];


5.应用间跳转

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


iOS 打电话 发短信 邮件 打开网址 调用应用等合集

这样这个APP的URL地址就是:ysj://blog.sina.com.cn/sharebase


接着在需要跳转的APP中使用以下代码完成跳转:

NSURL *url = [NSURL URLWithString:@"ysj://blog.sina.com.cn/sharebase"];

[[UIApplication sharedApplication] openURL:url];


6.应用评分

有时候需要用户为我们的应用进行评分(其实并没有什么卵用 —— 都是拒绝T^T),那怎么做呢?


思想:其实就是跳转到Appstore展示自己的应用,然后由用户自己评论

跳转Appstore并展示应用

方式一:

NSString *appid = @"444934666";

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

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


方式二:

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


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