Нет описания

SchemaManager.m 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // SchemeManager.m
  3. // FirstLink
  4. //
  5. // Created by unicode on 14-11-13.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "SchemaManager.h"
  9. #import "FLViewController.h"
  10. #import "FirstLinkAppDelegate.h"
  11. #import "FLControllerHelper.h"
  12. #import "WebViewController.h"
  13. @interface SchemaManager ()
  14. @property (nonatomic, strong) NSURL *url;
  15. @end
  16. @implementation SchemaManager
  17. + (SchemaManager *)sharedManager {
  18. static SchemaManager *sharedSchemeManagerInstance = nil;
  19. static dispatch_once_t once_token;
  20. dispatch_once(&once_token, ^{
  21. sharedSchemeManagerInstance = [[self alloc] init];
  22. });
  23. return sharedSchemeManagerInstance;
  24. }
  25. - (BOOL)save:(NSURL *)url {
  26. self.url = [url copy];
  27. return YES;
  28. }
  29. - (void)clearURL {
  30. self.url = nil;
  31. }
  32. - (void)parser {
  33. [self parserURL:self.url shouldCache:NO];
  34. }
  35. - (void)parserURL:(NSURL *)url shouldCache:(BOOL)shouldCache {
  36. if (![url isKindOfClass:[NSURL class]] || !url) {
  37. return;
  38. }
  39. self.url = [url copy];
  40. NSString *host = self.url.host;
  41. NSString *relativePath = self.url.relativePath;
  42. NSMutableDictionary *para = [self parserPara:self.url.query];
  43. // 短信调用H5唤醒App的格式是 firstlinkapp://sms.fine3q.com?url=urlencode
  44. if ([host isEqualToString:SMSHost]) {
  45. NSRange range = [self.url.absoluteString rangeOfString:@"url="];
  46. if (range.length > 0) {
  47. NSString *paraURL = [self.url.absoluteString substringFromIndex:(range.location + range.length)];
  48. if (paraURL.length > 0) {
  49. paraURL = [paraURL stringByRemovingPercentEncoding];
  50. self.url = [NSURL URLWithString:paraURL];
  51. [self parserURL:self.url shouldCache:YES];
  52. return;
  53. }
  54. }
  55. }
  56. if ([self.url.scheme rangeOfString:@"HTTP" options:NSCaseInsensitiveSearch].length > 0) {
  57. WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"];
  58. newViewController.url = self.url.absoluteString;
  59. newViewController.hidesBottomBarWhenPushed = YES;
  60. [[FLControllerHelper currentNavigationController] pushViewController:newViewController animated:YES];
  61. } else {
  62. if (host && relativePath) {
  63. [self pushViewController:host relativePath:relativePath para:para];
  64. }
  65. }
  66. if (!shouldCache) {
  67. [self clearURL];
  68. }
  69. }
  70. - (NSMutableDictionary*)parserPara:(NSString*)query {
  71. query = [query stringByRemovingPercentEncoding];
  72. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  73. for (NSString *param in [query componentsSeparatedByString:@"&"]) {
  74. NSArray *elts = [param componentsSeparatedByString:@"="];
  75. if([elts count] < 2) continue;
  76. [params setObject:[elts objectAtIndex:1] forKey:[elts objectAtIndex:0]];
  77. }
  78. return params;
  79. }
  80. - (void)pushViewController:(NSString*)host
  81. relativePath:(NSString*)relativePath
  82. para:(NSMutableDictionary*)para {
  83. if ([host isEqualToString:LoginHost]) {
  84. if (![FKUserManager isUserLogin]) {
  85. [FLControllerHelper presentLoginControllerAnimated:YES completion:nil];
  86. }
  87. return;
  88. }
  89. NSString *className = nil;
  90. FLViewController *currentController = [self currentController];
  91. if ([host isEqualToString:PostHost]) {
  92. if ([relativePath isEqualToString:DetailPath]) {
  93. className = PindanDetailControllerString;
  94. } else if ([relativePath isEqualToString:TopicPostList]){
  95. className = FKTopicControllerString;
  96. }
  97. }
  98. if ([host isEqualToString:RecoNewsHost]) {
  99. if ([relativePath isEqualToString:RecoNewsList]) {
  100. className = FKRecoNewsControllerString;
  101. }
  102. }
  103. if ([host isEqualToString:PromotionHost]) {
  104. if ([relativePath isEqualToString:PromotionList]) {
  105. className = FKPromotionControllerString;
  106. }
  107. }
  108. if ([host isEqualToString:HotSaleHost]) {
  109. if ([relativePath isEqualToString:HotSaleList]) {
  110. className = FKHotSaleControllerString;
  111. }
  112. }
  113. if ([host isEqualToString:ProductHost]) {
  114. if ([relativePath isEqualToString:DetailPath]) {
  115. className = ProductDetailControllerString;
  116. }
  117. }
  118. if ([host isEqualToString:FAQHost]) {
  119. if ([relativePath isEqualToString:AuthorityListPath]) {
  120. className = AdviseDetailControllerString;
  121. }
  122. }
  123. if ([host isEqualToString:GrouponHost]) {
  124. if ([relativePath isEqualToString:DetailPath]) {
  125. className = FKGroupDetailControllerString;
  126. }
  127. }
  128. if ([host isEqualToString:PersonHost]) {
  129. if ([FKUserManager isUserLogin]) {
  130. if ([relativePath isEqualToString:OrderPath]) {
  131. if (para.allKeys.count > 0) {
  132. className = OrderDetailControllerString;
  133. } else {
  134. className = FKPersonOrderControllerString;
  135. }
  136. }
  137. if ([relativePath isEqualToString:CashCouponPath]) {
  138. className = CashCouponControllerString;
  139. }
  140. if ([relativePath isEqualToString:PointPath]) {
  141. className = PointControllerString;
  142. }
  143. if ([relativePath isEqualToString:InviteCodePath]){
  144. className = FKInviteCodeControllerString;
  145. }
  146. if ([relativePath isEqualToString:BuyVipPath]) {
  147. className = FKVipControllerString;
  148. }
  149. if ([relativePath isEqualToString:BackPath]) {
  150. [[FLControllerHelper currentNavigationController] popViewControllerAnimated:YES];
  151. }
  152. if ([relativePath isEqualToString:DownloadPath]) {
  153. NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/剁手帮海淘-新用户专享225元优惠券/id950199368?mt=8"];
  154. [[UIApplication sharedApplication] openURL:url];
  155. }
  156. }
  157. }
  158. if ([host isEqualToString:IndexHost]) {
  159. [FLControllerHelper backToMainController];
  160. }
  161. if ([host isEqualToString:SearchHost]) {
  162. className = FKExploreListControllerString;
  163. }
  164. if ([host isEqualToString:ShopCartHost]) {
  165. if ([FKUserManager isUserLogin]) {
  166. className = FKBasketControllerString;
  167. }
  168. }
  169. if ([host isEqualToString:LogisticsHost]) {
  170. if ([FKUserManager isUserLogin]) {
  171. if ([relativePath isEqualToString:LogisticsDetailPath]) {
  172. className = LogisticsViewControllerString;
  173. }
  174. }
  175. }
  176. if ([host isEqualToString:ShareHost]) {
  177. if ([FKUserManager isUserLogin]) {
  178. className = FKCircleDetailControllerString;
  179. }
  180. }
  181. if ([host isEqualToString:ScoreHost]) {
  182. if ([FKUserManager isUserLogin]) {
  183. className = FKPointDetailControllerString;
  184. }
  185. }
  186. if ([host isEqualToString:WantBuyHost]) {
  187. if ([FKUserManager isUserLogin]) {
  188. className = FKWantBuyControllerString;
  189. }
  190. }
  191. /**
  192. * generate instance from class name and push it to view controllers stack
  193. */
  194. if (className) {
  195. if ([NSStringFromClass(currentController.class) isEqualToString:className]) {
  196. [currentController forceRefresh];
  197. } else {
  198. Class instanceClass = NSClassFromString(className);
  199. FLViewController *newViewController = [[instanceClass alloc] init];
  200. newViewController.openURLPara = para;
  201. newViewController.hidesBottomBarWhenPushed = YES;
  202. [currentController.navigationController pushViewController:newViewController animated:YES];
  203. }
  204. }
  205. }
  206. - (FLViewController*)currentController {
  207. UITabBarController *tabbarController = [FLControllerHelper currentTabBarController];
  208. if (!tabbarController) return nil;
  209. UINavigationController *curNaviController = (UINavigationController*)tabbarController.selectedViewController;
  210. if ([curNaviController isKindOfClass:[UINavigationController class]]) {
  211. FLViewController *currentController = (FLViewController*)curNaviController.topViewController;
  212. return currentController;
  213. }
  214. return nil;
  215. }
  216. - (void)pushViewControllerFromClassName:(NSString*)className
  217. para:(NSDictionary*)para
  218. toViewController:(FLViewController*)viewController{
  219. if (viewController) {
  220. FLViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:className];
  221. newViewController.openURLPara = para;
  222. [viewController.navigationController pushViewController:newViewController animated:YES];
  223. }
  224. return;
  225. }
  226. @end