美文网首页iOS Developer
iOS开发_记录调用系统应用

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

作者: CN_HarrySun | 来源:发表于2017-04-20 11:04 被阅读41次

调用系统的打电话、发短信、发邮件、地图、网页、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<发送邮件>

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

相关文章

网友评论

    本文标题:iOS开发_记录调用系统应用

    本文链接:https://www.haomeiwen.com/subject/wmwkzttx.html