// // SchemeManager.m // FirstLink // // Created by unicode on 14-11-13. // Copyright (c) 2014年 FirstLink. All rights reserved. // #import "SchemaManager.h" #import "FLViewController.h" #import "FirstLinkAppDelegate.h" #import "FLControllerHelper.h" #import "WebViewController.h" @interface SchemaManager () @property (nonatomic, strong) NSURL *url; @end @implementation SchemaManager + (SchemaManager *)sharedManager { static SchemaManager *sharedSchemeManagerInstance = nil; static dispatch_once_t once_token; dispatch_once(&once_token, ^{ sharedSchemeManagerInstance = [[self alloc] init]; }); return sharedSchemeManagerInstance; } - (BOOL)save:(NSURL *)url { self.url = [url copy]; return YES; } - (void)clearURL { self.url = nil; } - (void)parser { [self parserURL:self.url shouldCache:NO]; } - (void)parserURL:(NSURL *)url shouldCache:(BOOL)shouldCache { if (![url isKindOfClass:[NSURL class]] || !url) { return; } self.url = [url copy]; NSString *host = self.url.host; NSString *relativePath = self.url.relativePath; NSMutableDictionary *para = [self parserPara:self.url.query]; // 短信调用H5唤醒App的格式是 firstlinkapp://sms.fine3q.com?url=urlencode if ([host isEqualToString:SMSHost]) { NSRange range = [self.url.absoluteString rangeOfString:@"url="]; if (range.length > 0) { NSString *paraURL = [self.url.absoluteString substringFromIndex:(range.location + range.length)]; if (paraURL.length > 0) { paraURL = [paraURL stringByRemovingPercentEncoding]; self.url = [NSURL URLWithString:paraURL]; [self parserURL:self.url shouldCache:YES]; return; } } } if ([self.url.scheme rangeOfString:@"HTTP" options:NSCaseInsensitiveSearch].length > 0) { WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"]; newViewController.url = self.url.absoluteString; newViewController.hidesBottomBarWhenPushed = YES; [[FLControllerHelper currentNavigationController] pushViewController:newViewController animated:YES]; } else { if (host && relativePath) { [self pushViewController:host relativePath:relativePath para:para]; } } if (!shouldCache) { [self clearURL]; } } - (NSMutableDictionary*)parserPara:(NSString*)query { query = [query stringByRemovingPercentEncoding]; NSMutableDictionary *params = [NSMutableDictionary dictionary]; for (NSString *param in [query componentsSeparatedByString:@"&"]) { NSArray *elts = [param componentsSeparatedByString:@"="]; if([elts count] < 2) continue; [params setObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]]; } return params; } - (void)pushViewController:(NSString*)host relativePath:(NSString*)relativePath para:(NSMutableDictionary*)para { if ([host isEqualToString:LoginHost]) { if (![FKUserManager isUserLogin]) { [FLControllerHelper presentLoginControllerAnimated:YES completion:nil]; } return; } NSString *className = nil; FLViewController *currentController = [self currentController]; if ([host isEqualToString:PostHost]) { if ([relativePath isEqualToString:DetailPath]) { className = PindanDetailControllerString; } else if ([relativePath isEqualToString:TopicPostList]){ className = FKTopicControllerString; } } if ([host isEqualToString:RecoNewsHost]) { if ([relativePath isEqualToString:RecoNewsList]) { className = FKRecoNewsControllerString; } } if ([host isEqualToString:PromotionHost]) { if ([relativePath isEqualToString:PromotionList]) { className = FKPromotionControllerString; } } if ([host isEqualToString:HotSaleHost]) { if ([relativePath isEqualToString:HotSaleList]) { className = FKHotSaleControllerString; } } if ([host isEqualToString:ProductHost]) { if ([relativePath isEqualToString:DetailPath]) { className = ProductDetailControllerString; } } if ([host isEqualToString:FAQHost]) { if ([relativePath isEqualToString:AuthorityListPath]) { className = AdviseDetailControllerString; } } if ([host isEqualToString:GrouponHost]) { if ([relativePath isEqualToString:DetailPath]) { className = FKGroupDetailControllerString; } } if ([host isEqualToString:PersonHost]) { if ([FKUserManager isUserLogin]) { if ([relativePath isEqualToString:OrderPath]) { if (para.allKeys.count > 0) { className = OrderDetailControllerString; } else { className = FKPersonOrderControllerString; } } if ([relativePath isEqualToString:CashCouponPath]) { className = CashCouponControllerString; } if ([relativePath isEqualToString:PointPath]) { className = PointControllerString; } if ([relativePath isEqualToString:InviteCodePath]){ className = FKInviteCodeControllerString; } if ([relativePath isEqualToString:BuyVipPath]) { className = FKVipControllerString; } if ([relativePath isEqualToString:BackPath]) { [[FLControllerHelper currentNavigationController] popViewControllerAnimated:YES]; } if ([relativePath isEqualToString:DownloadPath]) { NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/剁手帮海淘-新用户专享225元优惠券/id950199368?mt=8"]; [[UIApplication sharedApplication] openURL:url]; } } } if ([host isEqualToString:IndexHost]) { [FLControllerHelper backToMainController]; } if ([host isEqualToString:SearchHost]) { className = FKExploreListControllerString; } if ([host isEqualToString:ShopCartHost]) { if ([FKUserManager isUserLogin]) { className = FKBasketControllerString; } } if ([host isEqualToString:LogisticsHost]) { if ([FKUserManager isUserLogin]) { if ([relativePath isEqualToString:LogisticsDetailPath]) { className = LogisticsViewControllerString; } } } if ([host isEqualToString:ShareHost]) { if ([FKUserManager isUserLogin]) { className = FKCircleDetailControllerString; } } if ([host isEqualToString:ScoreHost]) { if ([FKUserManager isUserLogin]) { className = FKPointDetailControllerString; } } if ([host isEqualToString:WantBuyHost]) { if ([FKUserManager isUserLogin]) { className = FKWantBuyControllerString; } } /** * generate instance from class name and push it to view controllers stack */ if (className) { if ([NSStringFromClass(currentController.class) isEqualToString:className]) { [currentController forceRefresh]; } else { Class instanceClass = NSClassFromString(className); FLViewController *newViewController = [[instanceClass alloc] init]; newViewController.openURLPara = para; newViewController.hidesBottomBarWhenPushed = YES; [currentController.navigationController pushViewController:newViewController animated:YES]; } } } - (FLViewController*)currentController { UITabBarController *tabbarController = [FLControllerHelper currentTabBarController]; if (!tabbarController) return nil; UINavigationController *curNaviController = (UINavigationController*)tabbarController.selectedViewController; if ([curNaviController isKindOfClass:[UINavigationController class]]) { FLViewController *currentController = (FLViewController*)curNaviController.topViewController; return currentController; } return nil; } - (void)pushViewControllerFromClassName:(NSString*)className para:(NSDictionary*)para toViewController:(FLViewController*)viewController{ if (viewController) { FLViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:className]; newViewController.openURLPara = para; [viewController.navigationController pushViewController:newViewController animated:YES]; } return; } @end