IOS9中打开另一个App

//=================================================================================================================


最近要在IOS中实现一个应用启动另外一个应用的功能,搜了一些资料,使用UIApplication的openURL:的方法就能实现,现在整理和大家分享一下!

注册自定义URL协议

首先被启动的应用需要向iPhone注册一个自定义URL协议。这是在你的项目文件夹的info.plist文件进行的(就是你改变应用程序图标的同一个文件)。
Step1. 右键,选择“Add Row”Step2. Key值选择“URL types”
Step3. 打开“Item 0″,然后为该key增加一个URL identifier。可以是任何值,但建议用“反域名”(例如 “com.fcplayer.testHello”)。
Step4. 在“Item 0”下再加一行。
Step5. 选择“URL Schemes” 作为Key。
Step6. 输入你的URL协议名 (例如“testHello://” 应写做“testHello”)。如果有必要,你可以在这里加入多个协议。
操作截图如下:

访问自定义URL

在主应用程序中通过访问自定义URL启动另外一个应用:

[csharp]  view plain copy
  1. NSURL * myURL_APP_A = [NSURL URLWithString:@"testHello://"];  
  2. if ([[UIApplication sharedApplication] canOpenURL:myURL_APP_A]) {  
  3.     NSLog(@"canOpenURL");  
  4.     [[UIApplication sharedApplication] openURL:myURL_APP_A];  
  5. }  

自定义处理URL

有些时候我们除了启动还需向另外一个应用发送参数,这是也可以通过自定义的URL来实现,如:

testHello://
testHello://com.fcplayer.testHello
testHello://config=1&abar=2

这时我们在被启动应用中就必须进行自定义处理,在delegate中实现该消息(Cocos2d加在AppDelegate中),例如:
- (BOOL)application:(UIApplication *)applicationhandleOpenURL:(NSURL*)url {   // Do something withthe url here }

通常,我们会从参数中解析出URL以便在视图中显示或者存储到UserPreference。下面的例子把URL存储为User Preference的url变量中或者打印出来:

[csharp]  view plain copy
  1. -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url  
  2. {  
  3.     if (!url) {  return NO; }  
  4.     NSString *URLString = [url absoluteString];  
  5.     NSLog(@"%@",URLString);  
  6.     //[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"];  
  7.     //[[NSUserDefaults standardUserDefaults] synchronize];  
  8.     return YES;  
  9. }  

其他

基本上至此我们就已经实现一个应用程序中启动另外一个应用的功能,但是为了是我们的代码更加强壮,我在网上又找了一段访问代码,如下:

[csharp]  view plain copy
  1. // 检查用户是否配置了AppId  
  2. // 有没有准确配置Info的CFBundleURLSchemes字段  
  3. // 是不是可以正确打开  
  4. if (!kAppId) {  
  5.     UIAlertView *alertView = [[UIAlertView alloc]  
  6.                               initWithTitle:@"Setup Error"  
  7.                               message:@"Missing app ID. You cannot run the app until you provide this in the code."  
  8.                               delegate:self  
  9.                               cancelButtonTitle:@"OK"  
  10.                               otherButtonTitles:nil,  
  11.                               nil];  
  12.     [alertView show];  
  13.     [alertView release];  
  14. else {  
  15.     // Now check that the URL scheme fb[app_id]://authorize is in the .plist and can  
  16.     // be opened, doing a simple check without local app id factored in here  
  17.     NSString *url = [NSString stringWithFormat:@"fb%@://authorize",kAppId];  
  18.     BOOL bSchemeInPlist = NO; // find out if the sceme is in the plist file.  
  19.     NSArray* aBundleURLTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];  
  20.     if ([aBundleURLTypes isKindOfClass:[NSArray class]] &&  
  21.         ([aBundleURLTypes count] > 0)) {  
  22.         NSDictionary* aBundleURLTypes0 = [aBundleURLTypes objectAtIndex:0];  
  23.         if ([aBundleURLTypes0 isKindOfClass:[NSDictionary class]]) {  
  24.             NSArray* aBundleURLSchemes = [aBundleURLTypes0 objectForKey:@"CFBundleURLSchemes"];  
  25.             if ([aBundleURLSchemes isKindOfClass:[NSArray class]] &&  
  26.                 ([aBundleURLSchemes count] > 0)) {  
  27.                 NSString *scheme = [aBundleURLSchemes objectAtIndex:0];  
  28.                 if ([scheme isKindOfClass:[NSString class]] &&  
  29.                     [url hasPrefix:scheme]) {  
  30.                     bSchemeInPlist = YES;  
  31.                 }  
  32.             }  
  33.         }  
  34.     }  
  35.     // Check if the authorization callback will work  
  36.     BOOL bCanOpenUrl = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: url]];  
  37.     if (!bSchemeInPlist || !bCanOpenUrl) {  
  38.         UIAlertView *alertView = [[UIAlertView alloc]  
  39.                                   initWithTitle:@"Setup Error"  
  40.                                   message:@"Invalid or missing URL scheme. You cannot run the app until you set up a valid URL scheme in your .plist."  
  41.                                   delegate:self  
  42.                                   cancelButtonTitle:@"OK"  
  43.                                   otherButtonTitles:nil,  
  44.                                   nil];  
  45.         [alertView show];  
  46.         [alertView release];  
  47.     }  
  48. }  


另外还有一段启动其他应用的代码:

[csharp]  view plain copy
  1. -(IBAction)openMaps {//打开地图  
  2.     // Where is Apple on the map anyway?  
  3.     NSString* addressText = @”1 Infinite Loop, Cupertino, CA 95014″;  
  4.     // URL encode the spaces  
  5.     addressText =  [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];  
  6.     NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];  
  7.     // lets throw this text on the log so we can view the url in the event we have an issue  
  8.     NSLog(urlText);  
  9.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];  
  10.     }  
  11.     -(IBAction)openEmail {//打开mail  
  12.     // Fire off an email to apple support  
  13.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];  
  14.     }  
  15.     -(IBAction)openPhone {//拨打电话  
  16.     // Call Google 411  
  17.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];  
  18.     }  
  19.     -(IBAction)openSms {//打开短信  
  20.     // Text to Google SMS  
  21.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];  
  22.     }  
  23.     -(IBAction)openBrowser {//打开浏览器  
  24.     // Lanuch any iPhone developers fav site  
  25.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunesconnect.apple.com"]];  
  26. }  

附参考链接:http://blog.csdn.net/arthurchenjs/article/details/6920631




  

IOS开发~两个App互相调用

分类: IOS   273人阅读  评论(1)  收藏  举报

1、新建两个项目:AppOne,AppTwo;

2、分别在其属性列表中添加如下:

AppOwn:



AppTwo:



3、分别实现代码:

AppOwn的 viewController.m 中添加代码:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    NSURL *url  = [NSURLURLWithString:@"AppTwo:"];


    if ([[UIApplicationsharedApplicationcanOpenURL:url])

    {

        NSLog(@"canOpenURL");

        [[UIApplication sharedApplicationopenURL:url];

    } else

    {

        NSLog(@"can not OpenURL");

    }

}


AppTwn的 viewController.m 中添加代码:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    NSLog(@"touchesBegan");

    

    NSURL *url  = [NSURLURLWithString:@"AppOne:"];

    

    if ([[UIApplicationsharedApplicationcanOpenURL:url])

    {

        NSLog(@"canOpenURL");

        [[UIApplication sharedApplicationopenURL:url];

    } else

    {

        NSLog(@"can not OpenURL");

    }

}


4、分别运行下两个项目,然后无论把其中一个项目进程杀掉,或者使其进入休眠,都可以通过另一个项目来打开另一个项目。



//=================================================================================================================


做各大平台分享的时候由于账号混乱,导致分享回调的时候启动了另一个app,这个时候我在猜想这些SDK是怎么唤醒没有启动的其他app的,不难想象,肯定跟AppDelegate处理回调openUrl有关,于是搜了一下才发现,一个应用程序可以唤醒另一个应用程序,只怪我菜,好啦,现在告诉不知道的朋友怎么来实现一个app怎么唤醒另一个app,以及让两个app之间互相通信。。。


测试工程操作如下

1.新建一个app1,在Info.plist文件的信息属性列表里新建一组,类型是URL types    设置如下
IOS <wbr>唤醒另一个APP(app之间互相通信)
这里最关键的部分在于URL Schemes数组里的Item 0,后面的填写的字符串就是你用来通信的命令前缀“achao”,URL identifier只是一个标示符,随意填写
然后再AppDelegate里处理重载下面的回调方法

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

    if ([[url scheme] isEqualToString:@"achao"])

    {

        NSLog(@"%@",url);

    }

    return YES;

}

可以看见[url scheme]这个命令是为了拿到url的scheme,就是命令前缀“achao”

2.新建app2,这个app什么都不用操作,只需要去唤醒app1即可,于是我们在viewDidLoad里写上这一句

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"achao://hello"]];

"achao"就是app1里的url scheme,我叫它命令前缀(我怀疑apple的应用程序装上过后有个像通知中心一样的应用程序来统一管理,而每个应用程序的url scheme都会在那里被记录,以供其他app来调用该app,至于url scheme属于哪个应用程序,当然是和app的Bundle identifier相关的),格式采用“前缀://..."

3.我们关闭app1,app2,然后再启动app2,发现app2启动过后唤醒了app1,并且成功跳转;我们再关闭app1,app2,然后我们打开app1进行监测,发现app1被启动后,进入了

IOS <wbr>唤醒另一个APP(app之间互相通信)

这就实现了两个app之间的唤醒和通信

4.当然这时候你可能才想到,那不是很多应用程序都会被其他垃圾程序调用了,查找资料过后,原来还有后续
我们重载这个方法

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

    if ([sourceApplication isEqualToString:@"AC.achao.com"])

    {

        NSLog(@"%@", sourceApplication);    //来源于哪个appBundle identifier

        NSLog(@"scheme:%@", [url scheme]);  //url scheme

        NSLog(@"query: %@", [url query]);   //查询串  “?...”格式访问

        return YES;

    }

    else 

        return NO;

 

}

这就满足我们的需求了,我们可以通过sourceApplication来判断来自哪个app以决定要不要唤醒自己的app,也可以通过[url query]来获得查询串,这个时候我们需要更改app2的访问方式才能获得这个参数

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"achao://hello?name=achao-AC"]];

我们也可以直接在safari离输入"achao://hello?name=achao-AC"来访问我们的app1,这个时候sourceApplication就是@"com.apple.mobilesafari"


总结:类似下面的方法

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];

[[UIApplication sharedApplicationopenURL:[NSURL URLWithString:@"sms://158********"]];

[[UIApplication sharedApplicationopenURL:[NSURL URLWithString:@"tel://158********"]];

[[UIApplication sharedApplicationopenURL:[NSURL URLWithString:@"mailto://362****@qq.com"]];

我们用过很多,估计也是程序内部设置了类似的url scheme来供其他应用程序操作的


也许我理解的有错误,欢迎大家一起交流分享,不甚感激!


//=================================================================================================================

IOS9中需要添加,否则无法打开指定App

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>mqqOpensdkSSoLogin</string>
    <string>mqzone</string>
    <string>sinaweibo</string>
    <string>alipayauth</string>
    <string>alipay</string>
    <string>safepay</string>
    <string>mqq</string>
    <string>mqqapi</string>
    <string>mqqopensdkapiV3</string>
    <string>mqqopensdkapiV2</string>
    <string>mqqapiwallet</string>
    <string>mqqwpa</string>
    <string>mqqbrowser</string>
    <string>wtloginmqq2</string>
    <string>weixin</string>
    <string>wechat</string>
</array> 


//=================================================================================================================

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值