首先要在跳转应用程序中button的连线事件中:
- (IBAction)openMyApp:(id)sender {
NSURL *url = [NSURL URLWithString:@"speechWords://"];
// 不需要做判断
[[UIApplication sharedApplication] openURL:url];
}
其中speechWords://
是即将跳转应用程序的URL Types:

同时也可以在应用之间利用跳转的URL进行传参:
将URL改为speechWords://name='jay'&age=18
可以进行传值,在被跳转的应用程序中可以对该URL进行解析:
// 当其他程序通过 openURL 跳转到本程序的时候触发
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
NSLog(@"%@%@%@", app, url, options);
// 将url转Jason, componentsSeparatedByString方法会把字符串通过‘//’分割lg两个成数组.
NSString *lastStr = [url.absoluteString componentsSeparatedByString:@"//"].lastObject;
NSArray *tmpArr = [lastStr componentsSeparatedByString:@"&"];
NSMutableDictionary *dic = [NSMutableDictionary new];
for (NSString *tmpStr in tmpArr) {
NSArray *keyValues = [tmpStr componentsSeparatedByString:@"="];
[dic setValue:keyValues.lastObject forKey:keyValues.firstObject];
}
NSLog(@"dic %@", dic);
return YES;
}
网友评论