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

在SDK中打开其余接入应用的解决方案程序员

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

使用的办法以下:app

 

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

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

 

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

 

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

 

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

 

-(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"]];
    }url

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

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

 

 

这样就只要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];

        }

    }

相关文章
相关标签/搜索