应用跳转是根据协议头跳转
A跳转B,需要B增加URL Types ,A应用根据URL的协议头跳转
iOS8之前的跳转方法:
//适配iOS9 需要配置plist文件 -->添加应用白名单 -->info.plist添加LSApplicationQueriesSchemes数组
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]];
}
//iOS9可以使用 不需要配置plist文件
if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://"]]) {
NSLog(@"打开失败");
}
分别跳转朋友圈和好友列表 给好友列表页面一个"唯一"的URL scheme以供跳转
在A中将scheme和要跳转B的协议头处理拼接
//headerStr是协议头 scheme是A的标识
urlStr = [NSString stringWithFormat:@"%@?%@",headerStr,scheme];
在B中处理传来的url
//lastUrl是截取出来的scheme
NSString *urlStr = [lastUrl stringByAppendingString:@"://"];
处理url得到scheme,并返回
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]]
A方
// 朋友圈
- (IBAction)timeline:(id)sender
{
if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://timeline"]]) {
NSLog(@"不能跳转到微信");
}
}
// 好友列表
- (IBAction)session:(id)sender
{
// if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"weixin://session"]]) {
// NSLog(@"不能跳转到微信");
// }
// 获取info.plist内容
NSDictionary *info = [NSBundle mainBundle].infoDictionary;
// NSLog(@"%@",info);
// 获取scheme
NSArray *urlTypes = info[@"CFBundleURLTypes"];
// NSLog(@"%@",urlTypes);
NSDictionary *dict = urlTypes[0];
NSArray *urlSchemes = dict[@"CFBundleURLSchemes"];
NSString *scheme = urlSchemes[0];
NSLog(@"%@",scheme);
// 拼接@"weixin://session" 和scheme
NSString *headerStr = @"weixin://session";
NSString *urlStr = [NSString stringWithFormat:@"%@?%@",headerStr,scheme];
NSLog(@"%@",urlStr);
// 跳转到好友列表
if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]]) {
NSLog(@"不能跳转到微信");
}
}
B方
AppDelegate
@property (nonatomic, copy) NSString *lastStr;
#pragma mark 作用完全一样-->接收第三方app传递的信息
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
// weixin://session
NSLog(@"%@",url);
NSString *urlStr = url.absoluteString;
// 获取 // 的范围
NSRange range = [urlStr rangeOfString:@"//"];
// 截取 // 后面的所有字符串
// session?ert84tfv
NSString *lastStr = [urlStr substringFromIndex:range.location + range.length];
NSLog(@"%@",lastStr);
self.lastStr = lastStr;
// 获取根控制器
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
// 获取viewController
ViewController *vc = nav.childViewControllers[0];
// 回到根控制器
[nav popToRootViewControllerAnimated:YES];
// 跳转不同界面
if ([lastStr containsString:@"session"]) {
[vc performSegueWithIdentifier:@"session" sender:nil];
}else if([lastStr containsString:@"timeline"]){
[vc performSegueWithIdentifier:@"timeline" sender:nil];
}
return YES;
}
ViewController
点击返回到之前的项目
- (IBAction)backToApp:(id)sender
{
// 获取应用程序的代理
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// 获取//以后的字符串
// lastStr: session?ert84tfv
NSString *lastStr = delegate.lastStr;
// 获取?的范围
NSRange range = [lastStr rangeOfString:@"?"];
// 从?后面开始截取字符串
NSString *scheme = [lastStr substringFromIndex:range.location + range.length];
NSLog(@"%@",scheme);
// 跳转
if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:[scheme stringByAppendingString:@"://"]]]) {
NSLog(@"不能跳转");
}
}