// 1.獲取到對(duì)應(yīng)應(yīng)用程序的URL
NSURL *wechatURL = [NSURL URLWithString:urlString];
// 2.判斷手機(jī)中是否安裝了對(duì)應(yīng)的應(yīng)用程序
if ([[UIApplication sharedApplication] canOpenURL:wechatURL]) {
// 3.打開(kāi)應(yīng)用程序
[[UIApplication sharedApplication] openURL:wechatURL];
}
// 被打開(kāi)app會(huì)調(diào)用一下方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options绳姨; (iOS9.0 更新)
// 1. 獲取來(lái)源url
NSString *sourceUrl = url.absoluteString;
// 2.1 獲取主頁(yè)控制器
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
// 2.2 把所有的控制器從棧里拋出登澜,返回到主界面
[rootNav popToRootViewControllerAnimated:YES];
// 2.3 將來(lái)源URL傳給主界面控制器
ViewController *rootVC = [rootNav.childViewControllers firstObject];
rootVC.url = sourceUrl;
// 3. 判斷跳轉(zhuǎn)到朋友圈還是微信好友列表;
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;
}
}
// 在目標(biāo)控制器上調(diào)用url
NSString *destinationURL = [[self.url componentsSeparatedByString:@"?"] lastObject];
NSLog(@"%@",self.url);
NSString *urlString = [destinationURL stringByAppendingString:@"://"];
NSURL *url = [NSURL URLWithString:urlString];
// 判斷能否跳轉(zhuǎn)
if ([[UIApplication sharedApplication] canOpenURL:url]) {
NSLog(@"%@",url);
[[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
}