ios 应用进程间通信

ios应用本身是沙盒机制,但是苹果公司考虑到两个应用之间会存在通信,就在这方面留出过一些接口,来实现两个进程之间进行通信。


1. 从一个App中打开另一个App

先创建一个App叫App1,然后设置在info.plist设置URL types,我们简单来解释下,URL identifier这个值是个字符串,是什么都没关系,但是大家都提倡用 反域名 来作为它的值。重点是URL Schemes,这是一个数组,数组里边的item值是一个String类型,用来表示本身这个应用可以用自定义协议xxx://的url方式来打开,可以包含多个item值, 比如这里我们可以用app1://的url来打开这个应用。

App1AppDelegate协议中实现下面其中一个方法,来对以后其他App激活App1后,App1要做的操作。注意下面第二个方法实现了的话,会覆盖掉第一个方法。


那么另一个App我们叫做App2,如何打开我们刚才创建的第一个App1啦?

首先要在App2的info.plist文件中设置LSApplicationQueriesSchemes这个数组,然后它的item的值,就是我们将要在这个App2中应用打开的App1的URL Schemes数组的一个值,就想我们要在App2中允许打开App1,那么我们要在App2中将App1先加入白名单。


那么在App2中的配置就完成了,我们只需要简单的调用一下openUrl消息就可以了。



2.从一个App打开激活另一个App我们做到了,但是从App2中如何传值给App1啦?

我们在App2使用openUrl的时候给让参数url来携带参数,传递给app1,这样在app1中就可以通过url来接受传递过来的参数




补充:网友写的blog中有提到代码健壮性,使用下列代码,很容易理解,稍微看看吧!

// 检查用户是否配置了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];  
    }  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值