1.新建Project One:ShangHai 并自定义URL Schemes
工程项目的Info.plist -> Information Property List -> URL type 选项中添加以下信息:生成的源码:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>ShangHaiOne</string>
<string>shanghaitwo</string>
</array>
<key>CFBundleURLName</key>
<string>com.shanghai.www</string>
</dict>
</array>
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<UIApplicationOpenURLOptionsKey,id> *)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" = "<BSProcessHandle: 0x600000275ec0; Beijing:4581; valid: YES>";
}--ShangHaiOne name=aries&passoword=123456
Beijing工程输出:
Beijing[4581:537158] 跳转到上海成功