iOS 微信分享通用链接跳到指定页面

43 阅读1分钟

接入微信api后在这个方法中设置跳入对应界面

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler {
    // 检查是否是通过Universal Link启动的
    if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSURL *webpageURL = userActivity.webpageURL;
        NSString *urlString = [webpageURL absoluteString];
        
        // 解析URL来看是否含有roomId参数
        NSURLComponents *components = [NSURLComponents componentsWithURL:webpageURL resolvingAgainstBaseURL:NO];
        NSString *roomId = nil;
        
        for (NSURLQueryItem *item in components.queryItems) {
            if ([item.name isEqualToString:@"roomId"]) {
                roomId = item.value;
                break;
            }
        }
        
        // 如果URL中有roomId参数,则尝试导航到VC页面
        if (roomId) {
            [self navigateToVCWithRoomId:roomId];
            return YES;
        }
    }
    
    // 微信SDK处理Universal Link
    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}


- (void)navigateToVCWithRoomId:(NSString *)roomId {
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navController = (UINavigationController *)tabBarController.viewControllers[1];
    
    UIViewController *VC = [[UIViewController alloc] init];
    videoListJoinVC.hidesBottomBarWhenPushed = YES;  // 在push前设置
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navController.viewControllers];
    [viewControllers addObject:VC];  
    [navController setViewControllers:viewControllers animated:NO];

    tabBarController.selectedIndex = 1;  // 切换到第二个tab

    videoListJoinVC.hidesBottomBarWhenPushed = NO;  // Push后重置
}

VC写你自己要调到指定位置的VC