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

简介:

在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];
        }
    }









本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/701942,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
JSON Serverless API
Serverless 应用引擎常见问题之通过SDK修改调度器报错如何解决
Serverless 应用引擎(Serverless Application Engine, SAE)是一种完全托管的应用平台,它允许开发者无需管理服务器即可构建和部署应用。以下是Serverless 应用引擎使用过程中的一些常见问题及其答案的汇总:
25 0
|
3月前
|
Java API 开发工具
支付与银行业线上客户协议应用中的DocuSign集成方式选择——SDK和API
跨境支付公司和Docusign进行集成时,碰到问题时的解决方案。分别用SDK和API集成后的各自使用体验。
48 2
支付与银行业线上客户协议应用中的DocuSign集成方式选择——SDK和API
|
5月前
|
开发工具 Android开发
应用研发平台EMAS的用户反馈SDK确实使用了WebView
应用研发平台EMAS的用户反馈SDK确实使用了WebView
41 6
|
12天前
|
弹性计算 运维 Serverless
Serverless 应用引擎产品使用之在阿里函数计算中,使用阿里云API或SDK从函数计算调用ECS实例的服务如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
41 4
|
13天前
|
运维 Serverless API
Serverless 应用引擎产品使用之在阿里函数中sdk可以被中层引用如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
16 0
|
4月前
|
存储 人工智能 前端开发
Vercel 推出 AI SDK、AI 应用模板,快速构建 AI 应用!
Vercel 推出 AI SDK、AI 应用模板,快速构建 AI 应用!
209 0
|
5月前
|
开发工具 数据安全/隐私保护
您需要检查并确保应用是否正确集成了EMAS的SDK
您需要检查并确保应用是否正确集成了EMAS的SDK
27 1
|
19天前
|
JavaScript Java Maven
云效产品使用常见问题之android sdk 构建出aar后,上传到私有maven仓库失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
4月前
|
安全 开发工具 Android开发
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
413 0
|
7月前
|
API 开发工具 Android开发
解决 Android App 上架 Google play后 ,签名变更,第三方sdk无法登录
解决 Android App 上架 Google play后 ,签名变更,第三方sdk无法登录
152 0