iOS中小功能开发

iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话、打开网址、发邮件、发短信等

打电话

第一种方式

NSURL *url = [NSURL URLWithString:@"tel://10010"];   // iOS 10以前直接跳到拨号界面,打完电话不会回到原应用
NSURL *url = [NSURL URLWithString:@"telprompt://10010"];iOS 10以前在拨号之前会询问用户是否拨号,拨完后会回到原应用
iOS 10以后,上述两种方式相同,在拨号之前都会询问用户是否拨号,拨完号之后会回到原应用

ios10之后 openURL:已废弃,可用下面的方法替换,注意 options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} //参数是一个字典

NSURL *url = [NSURL URLWithString:@"tel://10010"];
[[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
        // 成功回调
        if(!success){
            //失败回调
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"不能完成跳转" message:@"请确认App已经安装" preferredStyle:UIAlertControllerStyleAlert];
            
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleCancel handler:nil];
            
        [aler addAction:cancelAction];
            
        [self  presentViewController:aler animated:YES completion:nil];
            
        }else{
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }

    }];

第二种方式

创建一个UIWebView来加载url,拨打完之后会自动跳到原应用

if (_webView == nil) {
    _webView = [[UIWebView alloc] initWithFrame:CGRectZero];
//注意:这个webView千万不要设置尺寸,不然会挡住其他界面,他只是用来打电话,不需要显示

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

发短信

第一种方法

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

NSURL *url = [NSURL URLWithString:@"sms://10010"];
    [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
        
    }];

第二种方式

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

// 包含主头文件
#import <MessageUI/MessageUI.h>

- (IBAction)sendMessageTwo {
    // 显示发短信的控制器
    MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
    // 设置短信内容
    vc.body = @"Hello China Unicom ?";
    // 设置收件人列表
    vc.recipients = @[@"10010", @"02010010"];
    // 设置代理,并遵守MFMessageComposeViewControllerDelegate协议
    vc.messageComposeDelegate = self;
    // 显示控制器
    [self presentViewController:vc animated:YES completion:nil];
}

#pragma mark MFMessageComposeViewControllerDelegate 的代理方法
/**
 当短信界面关闭的时候调用

 @param controller 发送短信控制器
 @param result 发送结果回调
 */
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    // 关闭短信界面
    [controller dismissViewControllerAnimated:YES completion:nil];
    if (result == MessageComposeResultCancelled) {
        NSLog(@"取消发送");
    } else if (result == MessageComposeResultSent) {
        NSLog(@"已经发出");
    } else {
        NSLog(@"发送失败");
    }
    
}

发邮件

第一种方法

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

- (IBAction)sendAddressOne {
    NSURL *url = [NSURL URLWithString:@"mailto://dengerxuan@163.com"];
    [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
        if(success){
            NSLog(@"发送成功");
        }else{
            NSLog(@"发送失败");
        }
    }];
}

第二种方法

使用MessageUI框架

// 包含头文件
#import <MessageUI/MessageUI.h>

#pragma mark - 在应用内发送邮件
//激活邮件功能
- (IBAction)sendAddressTwo {
 
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        [self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];
        return;
    }
    if (![mailClass canSendMail]) {
        [self alertWithMessage:@"用户没有设置邮件账户"];
        return;
    }
    [self displayMailPicker];
}
- (void)displayMailPicker{
    // 发送邮件控制器
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    // 设置代理
    mailPicker.mailComposeDelegate = self;
    
    // 设置主题
    [mailPicker setSubject: @"eMail主题"];
    // 添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject: @"dengerxuan@163.com"];
    [mailPicker setToRecipients: toRecipients];
    // 添加抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"dengerxuan@163.com", @"1158035983@qq.com", nil];
    [mailPicker setCcRecipients:ccRecipients];
    // 添加密送
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"1158035983@qq.com", nil];
    [mailPicker setBccRecipients:bccRecipients];
    // 添加一张图片
    UIImage *addPic = [UIImage imageNamed: @"girl.png"];
    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    // 关于mimeType:http://www.iana.org/assignments/media-types/index.html
    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];
    // 添加一个pdf附件
    NSString *file = [[NSBundle mainBundle] pathForResource:@"iOS开发进阶(唐巧).pdf" ofType:nil];
    // NSString *file = [self fullBundlePathFromRelativePath:@"iOS开发进阶(唐巧).pdf"];  // 此方法废弃
    NSData *pdf = [NSData dataWithContentsOfFile:file];
    [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"iOS开发进阶(唐巧).pdf"];
    // 设置正文
    NSString *emailBody = @"<font color='red'>eMail</font> 正文";
    [mailPicker setMessageBody:emailBody isHTML:YES];
    //    [self presentModalViewController: mailPicker animated:YES];
    [self presentViewController:mailPicker animated:YES completion:nil];
}

/**
 抽取提示框弹出的方法

 @param message 提示信息
 */
- (void)alertWithMessage:(NSString *)message{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定按钮");
    }]];
     [self presentViewController:alertVc animated:YES completion:nil] ;
}

#pragma mark MFMailComposeViewControllerDelegate 的代理方法
/**
 邮件发送后的代理方法回调

 @param controller 发送邮件的控制器
 @param result 发送结果
 @param error 发送失败
 */
- (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 options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {}

应用间跳转

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

  • 首先B应用要有自己的URL地址(在B的Info.plist文件中配置)

abc.png

此时B的URL为 mj:// ios.itcast.com

  • 接着在A应用中使用UIApplication来完成跳转
- (IBAction)openAnotherApp {
    NSURL *url = [NSURL URLWithString:@"mj://ios.itcast.com"];
    [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:^(BOOL success) {
        if(!success){
            //失败回调
            UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"不能完成跳转" message:@"请确认App已经安装" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleCancel handler:nil];
            
            [aler addAction:cancelAction];
            
            [self  presentViewController:aler animated:YES completion:nil];
            
        }else{
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }
    }];
}

应用评分

为了提高应用的用户体验,经常需要邀请用户对应用进行评分,应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论,如何跳转到AppStore,并且展示自己的应用

NSString *appid = @"您app的appid”;

NSString *str = [NSString stringWithFormat:

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

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

推荐阅读更多精彩内容