iOS使用URL Scheme实现App的进程间通信

1.新建Project One:ShangHai 并自定义URL Schemes

工程项目的Info.plist -> Information Property List -> URL type 选项中添加以下信息:
iOS使用URL Scheme实现App的进程间通信_第1张图片
InfoPlist.jpg.png

生成的源码:

    CFBundleURLTypes
    
        
            CFBundleURLSchemes
            
                ShangHaiOne
                shanghaitwo
            
            CFBundleURLName
            com.shanghai.www
        
    

2.新建Project Two: Beijing 并在界面创建一个button 并在方法中添加如下代码:

- (IBAction)switchToShangHai:(id)sender {
    
    NSURL *url=[NSURL URLWithString:@"ShangHaiOne://?name=aries&passoword=123456"];
    
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        
        NSDictionary *dict=@{
                            @"city":@"Beijing"
                            };
        [[UIApplication sharedApplication] openURL:url options:dict completionHandler:^(BOOL success) {
 
            NSLog(@"跳转到上海%@",success?@"成功":@"失败");
        }];
        
    }else{
        NSLog(@"URL Not Exist");
    }
}

3.进程间数据传输
在Project One: ShangHai中的APPDelegate.m文件中添加此方法:

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
    if ([[options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey] isEqualToString:@"com.apple.Beijing"]) {
        
        NSLog(@"options=%@--%@ %@",options,[url scheme],[url query]);
    }
    return YES;
}

4.ShangHai工程在启动的情况下,启动Beijing工程并点击跳转button:

ShanHai工程输出:

    ShangHai[4569:536608] oprtions={
    UIApplicationOpenURLOptionsOpenInPlaceKey = 0;
    UIApplicationOpenURLOptionsSourceApplicationKey = "com.apple.Beijing";
    "_UIApplicationOpenURLOptionsSourceProcessHandleKey" = "";
}--ShangHaiOne name=aries&passoword=123456

Beijing工程输出:

    Beijing[4581:537158] 跳转到上海成功

你可能感兴趣的:(iOS使用URL Scheme实现App的进程间通信)