// 1.获取到对应应用程序的URL
NSURL *wechatURL = [NSURL URLWithString:urlString];
// 2.判断手机中是否安装了对应的应用程序
if ([[UIApplication sharedApplication] canOpenURL:wechatURL]) {
// 3.打开应用程序
[[UIApplication sharedApplication] openURL:wechatURL];
}
// 被打开app会调用一下方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options; (iOS9.0 更新)
// 1. 获取来源url
NSString *sourceUrl = url.absoluteString;
// 2.1 获取主页控制器
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
// 2.2 把所有的控制器从栈里抛出,返回到主界面
[rootNav popToRootViewControllerAnimated:YES];
// 2.3 将来源URL传给主界面控制器
ViewController *rootVC = [rootNav.childViewControllers firstObject];
rootVC.url = sourceUrl;
// 3. 判断跳转到朋友圈还是微信好友列表;
if ([sourceUrl containsString:@"timeline"])
{
[rootVC performSegueWithIdentifier:@"homeToTimeline" sender:nil];
}else if ([sourceUrl containsString:@"session"]){
[rootVC performSegueWithIdentifier:@"homeToSession" sender:nil];
}
// 在主界面控制器中传递url
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"homeToTimeline"]) {
TimelineViewController *timeVC = segue.destinationViewController;
timeVC.url = self.url;
}
}
// 在目标控制器上调用url
NSString *destinationURL = [[self.url componentsSeparatedByString:@"?"] lastObject];
NSLog(@"%@",self.url);
NSString *urlString = [destinationURL stringByAppendingString:@"://"];
NSURL *url = [NSURL URLWithString:urlString];
// 判断能否跳转
if ([[UIApplication sharedApplication] canOpenURL:url]) {
NSLog(@"%@",url);
[[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
}