在SDK中打开其他接入应用的解决方案

一直以来,在iOS的开发中,在程序中打开另外一个应用是不允许。后来有正义之士用class-dump在私有API中找到了这样的功能。那就是使用UIApplication的launchApplicationWithIdentifier:suspended:来打开。

使用的办法如下:

NSString *identifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];

[[UIApplication sharedApplication] launchApplicationWithIdentifier:identifier suspended:NO];

毕竟是私有API不是一个好的办法,至少你永远都得不到App Store的认可。

在某些时候是其实我们可能还是需要这样的功能。作为一个SDK,其实还是有一种比较好的解决方案的。那就是使用UIApplication的openURL:的方法。

我们先来了解一下openURL和实现的方案。OpenURL其实是有很丰富的功能,除了简单的调用safari打开网站,还可有google地图搜索,Mail,拨打电话,发送短信,打开AppStore。

-(IBAction)openMaps {//打开地图

    // Where is Apple on the map anyway?

    NSString* addressText = @”1 Infinite Loop, Cupertino, CA 95014″;

    // URL encode the spaces

    addressText =  [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];

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

    // lets throw this text on the log so we can view the url in the event we have an issue

    NSLog(urlText);

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

    } 

    -(IBAction)openEmail {//打开mail

    // Fire off an email to apple support

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];

    } 

    -(IBAction)openPhone {//拨打电话

    // Call Google 411

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];

    } 

    -(IBAction)openSms {//打开短信

    // Text to Google SMS

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];

    } 

    -(IBAction)openBrowser {//打开浏览器

    // Lanuch any iPhone developers fav site

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunesconnect.apple.com"]];

    }

那怎样来制作从一个应用打开其他应用,这其实很简单,打开info.plist,添加一项URL types,展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme,展开URL Scheme,将Item1的内容修改为myapp其他程序可通过myapp://访问此自定义URL。

其实就是类似下面的样式。

这样就只要open这个应用的自定义url,系统就可以帮我们找到并打开这个程序。

NSURL *url = [NSURL URLWithString:@" myapp:"];

[[UIApplication sharedApplication] openURL:url];

作为SDK比普通应用的优势在于,每一个接入的应用都有一个AppId用于区分,我们就可以充分利用这个AppId来制作。

我们可以要求第三方开发者需要在他们Info.Plist中配置这样的字段,这样我们就可以在我们的SDK界面中打开对应AppId的应用,当然,这需要设备中真的有安装这个程序。

例如某应用分配AppId为111122223333,我们要求其再Info.plist定义URL Schemes为NDSDK111122223333,这样,我们在内部代码就可以准确识别是否有这样的程序。

更有甚者,我们可以通过canOpenURL这个方法来判断这台设备是否安装了这个应用,如果可以打开,返回YES,那应该是有安装这样的程序,不管是ipa还是Pxl的程序,应该都是没有问题的。

如果我们真的选择这样子做,那就需要在文档中说明清楚。但是需要注意的是,也许作为程序员,可能不是很喜欢看文档,也许你费尽心思写的文档他并没有看到。这时我们应该来一点强硬的手段,于是有了下面这段代码的功能。

1:检查用户是否配置了AppId

2:有没有准确配置Info的CFBundleURLSchemes字段

3:是不是可以正确打开。

// Check App ID:

// This is really a warning for the developer, this should not

// happen in a completed app

if (!kAppId) {

UIAlertView *alertView = [[UIAlertView alloc]

initWithTitle:@"Setup Error"

message:@"Missing app ID. You cannot run the app until you provide this in the code."

delegate:self

cancelButtonTitle:@"OK"

otherButtonTitles:nil,

nil];

[alertView show];

[alertView release];

} else {

// Now check that the URL scheme fb[app_id]://authorize is in the .plist and can

// be opened, doing a simple check without local app id factored in here

NSString *url = [NSString stringWithFormat:@"fb%@://authorize",kAppId];

BOOL bSchemeInPlist = NO; // find out if the sceme is in the plist file.

NSArray* aBundleURLTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];

if ([aBundleURLTypes isKindOfClass:[NSArray class]] &&

([aBundleURLTypes count] > 0)) {

NSDictionary* aBundleURLTypes0 = [aBundleURLTypes objectAtIndex:0];

if ([aBundleURLTypes0 isKindOfClass:[NSDictionary class]]) {

NSArray* aBundleURLSchemes = [aBundleURLTypes0 objectForKey:@"CFBundleURLSchemes"];

if ([aBundleURLSchemes isKindOfClass:[NSArray class]] &&

([aBundleURLSchemes count] > 0)) {

NSString *scheme = [aBundleURLSchemes objectAtIndex:0];

if ([scheme isKindOfClass:[NSString class]] &&

[url hasPrefix:scheme]) {

bSchemeInPlist = YES;

}

}

}

}

// Check if the authorization callback will work

BOOL bCanOpenUrl = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: url]];

if (!bSchemeInPlist || !bCanOpenUrl) {

UIAlertView *alertView = [[UIAlertView alloc]

initWithTitle:@"Setup Error"

message:@"Invalid or missing URL scheme. You cannot run the app until you set up a valid URL scheme in your .plist."

delegate:self

cancelButtonTitle:@"OK"

otherButtonTitles:nil,

nil];

[alertView show];

[alertView release];

}

}


【IOS】在SDK中打开其他接入应用的解决方案的更多相关文章

  1. 在高版本SDK中打开现存低版本SDK工程

    直接打开低版本SDK工程会出现错误提示:“Unable to resolve target 'android-xx” 解决方法: 1.将project.properties文件中的“target=an ...

  2. 在IOS应用中打开另外一个应用的解决方案

    最近要在IOS中实现一个应用启动另外一个应用的功能,搜了一些资料,使用UIApplication的openURL:的方法就能实现,现在整理和大家分享一下! 注册自定义URL协议 首先被启动的应用需要向 ...

  3. ios在项目中打开word文档、ppt等总结

    最近在项目开发中遇到下载附件文档预览需求,在这里总结一下我的实现方法,本文最后会附带我写的demo下载地址 这里我总结了三种实现方法(1)用webView预览(2)通过UIDocumentIntera ...

  4. 在iOS应用程序中打开设备设置界面及其中某指定的选项界面

    摘自:http://stackoverflow.com/questions/8246070/ios-launching-settings-restrictions-url-scheme [[UIApp ...

  5. Windows10中打开git bash闪退解决方案

    重装系统后打开gitbash莫名其妙闪退... 究其原因,好像是盗版系统的null.sys文件损坏 那就在这里附上null.sys文件的下载链接: https://pan.baidu.com/s/1V ...

  6. 在iOS应用程序中使用Frida绕过越狱检测

           阿里聚安全在之前的三篇博客中介绍了利用Frida攻击Android应用程序,整个过程仿佛让开发者开启上帝视角,在本篇博客中,我们将会介绍在iOS应用程序中使用Frida绕过越狱检测.即使 ...

  7. 开发者所需要知道的 iOS 10 SDK 新特性

    转自:https://onevcat.com/2016/06/ios-10-sdk/ 写的很好啊.哈哈哈 总览 距离 iPhone 横空出世已经过去了 9 个年头,iOS 的版本号也跨入了两位数.在我 ...

  8. IOS-当遇到tableView整体上移时的解决方案

    方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView或collectionView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关 ...

  9. 【Unity游戏开发】接入UWA_GOT的iOS版SDK以后无法正常出包

    一.正文 问: RT,最近有看到UWA_GOT工具新增了iOS版本的支持,于是下载了最新的工具包进行了接入测试.是按照文档直接将UWA_GOTv2.0.1_iOS.unitypackage导入进了Un ...

随机推荐

  1. LaTeX中titlesec宏包的使用

    在 xelatex 中使用 \usepackage 指令使用 titlesec 宏包时,可以指定一些格式选项,如下: \usepackage[center]{titlesec} 其中 center 可 ...

  2. SQL Server 错误18456

    第一步. 错误发生的场景 第二步. 找到引起错误的原因 第1步. 查看windows日志文件. 运行中输入 eventvwr (event viewer)打开日志文件查看器, 第三步. 解决方案,由第 ...

  3. Linux下 nginx + 最新版php5.5 安装配置详解

    1.nginx的安装: 首先nginx的安装需要依赖最基础的三个包,这里面我们不设计更多的扩展模块,只是安装最基础的三个包, zlib 此包主要是对http内容进行gzip压缩,减少网络传输流量 PC ...

  4. JSONP(转)

    1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面.动态网页.web服务.WCF,只要是跨域请求,一律不准: 2.不过我们又发现,Web页面上调用js文件时则不 ...

  5. MySQL 5.6 中 TIMESTAMP 的变化

    http://www.williamsang.com/archives/818.html

  6. 如何获取版本的 Internet 信息服务器 (IIS)

    不同的操作系统对应不同IIS http://support.microsoft.com/kb/224609/zh-cn

  7. Nutch环境搭建

    1. 环境准备 HOST:Ubuntu12.04LTS JDK: jdk-7u45-linux-i586.rpm Nutch:apache-nutch-1.7-bin.tar.gz Solr:solr ...

  8. 【具体数学 读书笔记】1.2 Lines in the Plane

    本节介绍平面划分问题,即n条直线最多把一个平面划分为几个区域(region). 问题描述: "What is the maximum number Ln of regions defined ...

  9. OpenStack IdentityService Keystone V3 API Curl实战

    v3 API Examples Using Curl <Tokens> 1,Default scope 获取token Get an token with default scope (m ...

  10. Android解决异常apk on device '0292bea1': Unable to open sync connection!

    方式一:使用手机管家(如腾讯手机管家,只要拖动发射火箭就行了)清理一下正在运行的后台程序. 方式二:把USB数据线拔了重新链接. 方法三:找到USB调试,关掉USB调试,然后重新开启.在设置 --&g ...