- 首先创建A,B两个的demo.我打算点击B中按钮跳转到A,即(B --> A).
1.在A的 'TARGETS' 的 'Info' 中找到 'URL Types',设置 'URL Shemes'如下,名字自己随意好啦
或者直接在info.plist中设置,如下:
- 接下来在B的跳转按钮中设置跳转代码:
无参数
NSString *paramStr = [NSString stringWithFormat:@"myAppTest:"];
NSURL *url = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
有参数
NSString *paramStr = [NSString stringWithFormat:@"myAppTest://username=%@&age=%@&address=%@", @"test123", @"100", @"上海市"];
NSURL *url = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
此刻已经实现跳转!!!
- 当A在后台运行的时候,B对A进行跳转会执行的方法
//当一个应用程序被其他程序打开的时候会调用这个方法,在该方法中可以实现两个应用程序间的数据局传递
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSString *urlStr = [url absoluteString];
if ([urlStr hasPrefix:@"myAppTest://"]) {
NSLog(@"TestAppDemo1 request params: %@", urlStr);
urlStr = [urlStr stringByReplacingOccurrencesOfString:@"myAppTest://" withString:@""];
NSArray *paramArray = [urlStr componentsSeparatedByString:@"&"];
NSLog(@"paramArray: %@", paramArray);
NSMutableDictionary *paramsDic = [[NSMutableDictionary alloc] initWithCapacity:0];
for (int i = 0; i < paramArray.count; i++) {
NSString *str = paramArray[i];
NSArray *keyArray = [str componentsSeparatedByString:@"="];
NSString *key = keyArray[0];
NSString *value = keyArray[1];
[paramsDic setObject:value forKey:key];
NSLog(@"key:%@ ==== value:%@", key, value);
}
}
return NO;
}