iOS开发_记录调用系统应用

调用系统的打电话、发短信、发邮件、地图、网页、Appstore、系统设置等

运行效果

一、打电话

1.拨打电话方式1

经测试,这种方法在打完电话时也可以返回到原App

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://15212341234"] options:[NSDictionary dictionary] completionHandler:^(BOOL success) {

    NSLog(@"拨打");
}];
2.拨打电话方式2
if (_webView == nil) {
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}

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

二、发短信

需要引入 <MessageUI/MessageUI.h> ,并遵循MFMessageComposeViewControllerDelegate协议

// 显示发短信的控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
// 设置短信内容
vc.body = @"发送短信测试";
// 设置收件人列表
vc.recipients = @[@"15212341234",@"10086"];
// 设置代理
vc.messageComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];


 // 实现代理方法
#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    NSString *msgString;
    // 关闭短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    
    switch (result) {
        case MessageComposeResultCancelled:
            
            msgString = @"取消发送";
            break;
        case MessageComposeResultSent:
            
            msgString = @"已经发送";
            break;
            
        default:
            
            msgString = @"发送失败";
            break;
    }
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msgString delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}

三、发邮件

需要引入 <MessageUI/MessageUI.h> ,并遵循MFMailComposeViewControllerDelegate协议

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (!mailClass) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
    return;
}

if (![mailClass canSendMail]) {
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户没有设置邮件账户" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
    NSLog(@"用户没有设置邮件账户");
    return;
}


// 需要在手机上设置邮箱账户才会出来
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];

mailVC.mailComposeDelegate = self;

// 设置主题
[mailVC setSubject:@"eMail主题"];
// 正文
NSString *emailBody = @"eMail正文";
[mailVC setMessageBody:emailBody isHTML:YES];


// 添加收件人
NSArray *toRecipients = [NSArray arrayWithObject:@"qwerasdf@qq.com"];
[mailVC setToRecipients:toRecipients];

//    // 添加抄送
//    NSArray *ccRecipients = [NSArray arrayWithObjects:@"qqq@qq.com",@"chaosong@qq.com", nil];
//    [mailVC setCcRecipients:ccRecipients];
//
//    // 添加密送
//    NSArray *bccRecipients = [NSArray arrayWithObjects:@"misong@qq.com", nil];
//    [mailVC setBccRecipients:bccRecipients];


// 添加一张图片
UIImage *addImage = [UIImage imageNamed:@"Icon.jpg"];
NSData *imageData = UIImageJPEGRepresentation(addImage, 1.0);
[mailVC addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"Icon.jpg"];
//



//    // 添加一个pdf附件
//    NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
//    NSData *pdf = [NSData dataWithContentsOfFile:file];
//    [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];


// 添加一个视频
//    NSString *path = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"20170418.mp4"];
//    NSData *videoData = [NSData dataWithContentsOfFile:path];
//    [mailVC addAttachmentData:videoData mimeType:@"" fileName:@"20170418.mp4"];


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



 // 实现代理方法
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{

    // 关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:nil];

    NSString *msgString;
    switch (result) {
        case MFMailComposeResultCancelled:
            msgString = @"用户取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msgString = @"用户成功保存邮件";
        case MFMailComposeResultSent:
            msgString = @"用户点击发送,将邮件放到队列中,还没发送";
        case MFMailComposeResultFailed:
            msgString = @"用户试图保存或者发送邮件失败";
        default:
            msgString = @"";
            break;
    }

    NSLog(@"%@",msgString);

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msgString delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];

}

四、打开地图

NSString *addressText = @"beijing";
addressText =[addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSString  *urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];

NSLog(@"urlText=============== %@", urlText);

NSURL *url = [NSURL URLWithString:urlText];

if([[UIApplication sharedApplication] canOpenURL:url]){
    
    [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
        
        NSLog(@"地图请求完成");
    }];
}

五、打开网页

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

if([[UIApplication sharedApplication] canOpenURL:url]){
    [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
        
        NSLog(@"网址请求完成");
    }];
}

六、打开Appstore

跳转到你的App页面

NSString *myappleID = @"你的appleID";

NSString *stringURL = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@&mt=8",myappleID];
NSURL *url = [NSURL URLWithString:stringURL];

if([[UIApplication sharedApplication] canOpenURL:url]){
    
    [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
        
        NSLog(@"App Store请求完成");
    }];
}

七、打开系统设置

打开的设置界面是本App的相关设置

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

if ([[UIApplication sharedApplication] canOpenURL:url]) {
    
    [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
        
        NSLog(@"系统设置请求完成");
    }];
}

整体代码

因为有些需要在真机上测试,所以建议打包到真机上运行

AppDelegate.m中

设置为navigationController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window setBackgroundColor: [UIColor whiteColor]];
    [self.window makeKeyAndVisible];
    
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
    
    
    return YES;
}
ViewController.m中
//
//  ViewController.m
//  CallSystemAppliction
//
//  Created by HarrySun on 2017/4/18.
//  Copyright © 2017年 Mobby. All rights reserved.
//

#import "ViewController.h"
#import <MessageUI/MessageUI.h>

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"调用系统应用";
    
    self.titleArray = @[@"打电话",@"发短信",@"发邮件",@"地图",@"打开网页",@"Appstore",@"系统设置"];
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
            [self call];
            break;
        case 1:
            [self sendMessage];
            break;
        case 2:
            [self sendEmail];
            break;
        case 3:
            [self openMap];
            break;
        case 4:
            [self openUrl];
            break;
        case 5:
            [self openAppstore];
            break;
        case 6:
            [self openSystemset];
            break;
        default:
            break;
    }
}

- (void)call{
    
    // 拨打电话一:
    //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://15212341234"] options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
    //
    //        NSLog(@"拨打");
    //    }];
    
    
    // 拨打电话二:
    if (_webView == nil) {
        
        _webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    }
    
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://15212341234"]]];
}


- (void)sendMessage{
    
    // 显示发短信的控制器
    MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
    // 设置短信内容
    vc.body = @"发送短信测试";
    // 设置收件人列表
    vc.recipients = @[@"15212341234",@"10086"];
    // 设置代理
    vc.messageComposeDelegate = self;
    // 显示控制器
    [self presentViewController:vc animated:YES completion:nil];
}


- (void)sendEmail{
    
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        return;
    }
    
    if (![mailClass canSendMail]) {
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户没有设置邮件账户" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        NSLog(@"用户没有设置邮件账户");
        return;
    }
    
    
    // 需要在手机上设置邮箱账户才会出来
    MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
    
    mailVC.mailComposeDelegate = self;
    
    // 设置主题
    [mailVC setSubject:@"eMail主题"];
    // 正文
    NSString *emailBody = @"eMail正文";
    [mailVC setMessageBody:emailBody isHTML:YES];
    
    
    // 添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject:@"qwerasdf@qq.com"];
    [mailVC setToRecipients:toRecipients];
    
    //    // 添加抄送
    //    NSArray *ccRecipients = [NSArray arrayWithObjects:@"qqq@qq.com",@"chaosong@qq.com", nil];
    //    [mailVC setCcRecipients:ccRecipients];
    //
    //    // 添加密送
    //    NSArray *bccRecipients = [NSArray arrayWithObjects:@"misong@qq.com", nil];
    //    [mailVC setBccRecipients:bccRecipients];
    
    
    // 添加一张图片
    UIImage *addImage = [UIImage imageNamed:@"Icon.jpg"];
    NSData *imageData = UIImageJPEGRepresentation(addImage, 1.0);
    [mailVC addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"Icon.jpg"];
    //
    
    
    
    //    // 添加一个pdf附件
    //    NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];
    //    NSData *pdf = [NSData dataWithContentsOfFile:file];
    //    [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];
    
    
    // 添加一个视频
    //    NSString *path = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"20170418.mp4"];
    //    NSData *videoData = [NSData dataWithContentsOfFile:path];
    //    [mailVC addAttachmentData:videoData mimeType:@"" fileName:@"20170418.mp4"];
    
    
    [self presentViewController:mailVC animated:YES completion:nil];
    
    
    
    
}


- (void)openMap{
    
    NSString *addressText = @"beijing";
    addressText =[addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    
    NSString  *urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
    
    NSLog(@"urlText=============== %@", urlText);
    
    NSURL *url = [NSURL URLWithString:urlText];
    
    if([[UIApplication sharedApplication] canOpenURL:url]){
        
        [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
            
            NSLog(@"地图请求完成");
        }];
        
    }
}



- (void)openUrl{
    
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    
    if([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
            
            NSLog(@"网址请求完成");
        }];
    }
}




- (void)openAppstore{
    
    NSString *myappleID = @"你的appleID";
    
    NSString *stringURL = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@&mt=8",myappleID];
    NSURL *url = [NSURL URLWithString:stringURL];
    
    if([[UIApplication sharedApplication] canOpenURL:url]){
        
        [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
            
            NSLog(@"App Store请求完成");
        }];
    }
}


- (void)openSystemset{
    
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        
        [[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:^(BOOL success) {
            
            NSLog(@"系统设置请求完成");
        }];
    }
}




#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    
    // 关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:nil];
    
    NSString *msgString;
    switch (result) {
        case MFMailComposeResultCancelled:
            msgString = @"用户取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msgString = @"用户成功保存邮件";
        case MFMailComposeResultSent:
            msgString = @"用户点击发送,将邮件放到队列中,还没发送";
        case MFMailComposeResultFailed:
            msgString = @"用户试图保存或者发送邮件失败";
        default:
            msgString = @"";
            break;
    }
    
    NSLog(@"%@",msgString);
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msgString delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
    
}




#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    NSString *msgString;
    // 关闭短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    
    
    switch (result) {
        case MessageComposeResultCancelled:
            
            msgString = @"取消发送";
            break;
        case MessageComposeResultSent:
            
            msgString = @"已经发送";
            break;
            
        default:
            
            msgString = @"发送失败";
            break;
    }
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msgString delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
相关链接:

iOS 开发中常用的小功能(打电话,发短信...)
MFMailComposeViewController<发送邮件>

年轻,就要有上路的渴望,要与勇敢同行 。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容