1. 程式人生 > >通過制定連線開啟APP中指定的頁面處理方法

通過制定連線開啟APP中指定的頁面處理方法

在專案中經常需要使用到掃描二維碼後根據是否安裝app進行不同的操作.有的是跳轉到指定的頁面,有的是需要去下載app的頁面.如何進行目的性跳轉:

//在AppDelegate.m中的openURL方法中進行操作,這個方法是通過url與應用進行互動時會呼叫的方法

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
 //這裡邊的 ycxdrive是我前端h5工程師約定的URL Schemes,是一個約定好的名字
if ([[url absoluteString] rangeOfString:@"ycxdrive"].location == 0) { //type userId都是我們前段及移動端約定好url上鎖攜帶的資訊欄位 NSRange range = [string rangeOfString:@"type"];//匹配得到的下標 string = [string substringFromIndex:range.location];//擷取掉下標7之後的字串 NSArray *array = [string componentsSeparatedByString:@"&"
]; //從符號&中分隔成2個元素的陣列 NSMutableDictionary *APPInfoDicAboutFriend = [NSMutableDictionary dictionary]; [array enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj containsString:@"type"]) { NSRange typeRange = [obj rangeOfString:@"="
];//匹配得到的下標 NSString *typeString = [obj substringFromIndex:typeRange.location+1];//擷取掉下標7之後的字串 [APPInfoDicAboutFriend setObject:typeString forKey:@"type"]; }else if ([obj containsString:@"userId"]){ NSRange userIDRange = [obj rangeOfString:@"="];//匹配得到的下標 NSString *userIDString = [obj substringFromIndex:userIDRange.location+1];//擷取掉下標7之後的字串 [APPInfoDicAboutFriend setObject:userIDString forKey:@"userId"]; } }]; //根據上述條件進行制定頁面的跳轉 // 跳轉使用者主頁,本人使用的tabbarVC控制器作為主控制器所以這麼操作 YCXRootTabBarController *rootTabVC = (YCXRootTabBarController *)self.window.rootViewController; rootTabVC.selectedIndex = 3; [rootTabVC.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { YCXBaseNavigationController *baseVC = obj; if ([baseVC.viewControllers[0] isKindOfClass:[YCXProfileController class]]) { YCXProfileController *profileVC = baseVC.viewControllers[0]; //需要跳轉到 ProfileController下的PersonalProfileController中 PersonalProfileController *vc = [[PersonalProfileController alloc] initWithNibName:@"PersonalProfileController" bundle:nil]; [profileVC.navigationController pushViewController:vc animated:YES]; } }]; } } return YES; }