您的位置:首页 > 移动开发

IOS9中打开另一个App

2015-11-24 11:40 537 查看
//=================================================================================================================

最近要在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
plaincopy

NSURL * myURL_APP_A = [NSURL URLWithString:@"testHello://"];  

if ([[UIApplication sharedApplication] canOpenURL:myURL_APP_A]) {  

    NSLog(@"canOpenURL");  

    [[UIApplication sharedApplication] openURL:myURL_APP_A];  

}  

自定义处理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
plaincopy

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

{  

    if (!url) {  return NO; }  

    NSString *URLString = [url absoluteString];  

    NSLog(@"%@",URLString);  

    //[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"];  

    //[[NSUserDefaults standardUserDefaults] synchronize];  

    return YES;  

}  

其他

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

[csharp] view
plaincopy

// 检查用户是否配置了AppId  

// 有没有准确配置Info的CFBundleURLSchemes字段  

// 是不是可以正确打开  

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

    }  

}  

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

[csharp] view
plaincopy

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

}  

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

  


IOS开发~两个App互相调用

分类: IOS2013-07-01
20:27 273人阅读 评论(1) 收藏 举报

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

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

AppOwn:



AppTwo:



3、分别实现代码:

AppOwn的 viewController.m 中添加代码:

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

{

    

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

    if ([[UIApplicationsharedApplication] canOpenURL:url])

    {

        NSLog(@"canOpenURL");

        [[UIApplication sharedApplication] openURL:url];

    } else

    {

        NSLog(@"can not OpenURL");

    }

}

AppTwn的 viewController.m 中添加代码:

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

{

    

    NSLog(@"touchesBegan");

    

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

    

    if ([[UIApplicationsharedApplication] canOpenURL:url])

    {

        NSLog(@"canOpenURL");

        [[UIApplication sharedApplication] openURL:url];

    } else

    {

        NSLog(@"can not OpenURL");

    }

}

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

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

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

测试工程操作如下

1.新建一个app1,在Info.plist文件的信息属性列表里新建一组,类型是URL types    设置如下





这里最关键的部分在于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被启动后,进入了





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

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

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

{

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

    {

        NSLog(@"%@",
sourceApplication);    //来源于哪个app(Bundle
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 sharedApplication] openURL:[NSURL URLWithString:@"sms://158********"]];

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

[[UIApplication sharedApplication] openURL:[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> 

//=================================================================================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: