在做开发时,有时候可能会用到两个app之间切换传值之类的,主要用的是:
[[UIApplication sharedApplication] openURL:url];
下面就写一下具体的操作步骤:
1.在工程一中:
2.在需要跳转的地方写下面的代码:
NSString *urlStr = [NSString stringWithFormat:@"TestSwitchTwo://%@",@"测试一下"];
// 转码
// iOS9以下用
// stringByAddingPercentEscapesUsingEncoding
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlStr]]];
[[UIApplication sharedApplication] openURL:url];
这个地方需要注意一下,如果需要传入汉字需要转码,不然会出现
LaunchServices: ERROR: There is no registered handler for URL scheme (null);
iOS9以下用:
stringByAddingPercentEscapesUsingEncoding;
iOS9以上用:
stringByAddingPercentEncodingWithAllowedCharacters;
3.在工程二中:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
NSString *str111 = [[url host] stringByRemovingPercentEncoding];
return YES;
}
iOS9以下用
stringByReplacingPercentEscapesUsingEncoding解码;
这样,就能跳转到第二个工程并传值过去。
注:如果是用模拟器,要将要跳转到的那个工程运行起来,然后才进行从第一个工程切换到另一个工程。