terry 5 years ago
commit
22e704a2c7
4 changed files with 248 additions and 0 deletions
  1. 54 0
      HybridBridgeImpl.m
  2. 16 0
      HybridBridgeProtocol.h
  3. 21 0
      JumpManager.h
  4. 157 0
      JumpManager.m

+ 54 - 0
HybridBridgeImpl.m

@@ -0,0 +1,54 @@
+//
+//  HybridBridgeImpl.m
+//  tk_hybrid_demo
+//
+//  Created by terry.guo on 2018/6/19.
+//  Copyright © 2018年 Terry. All rights reserved.
+//
+
+#import "NSJSONSerialization+Shortcuts.h"
+
+#import "JumpManager.h"
+#import "HybridBridgeImpl.h"
+
+@interface HybridBridgeImpl ()
+
+@property (weak, nonatomic) UIViewController *viewController;
+
+@end
+
+@implementation HybridBridgeImpl
+
+- (instancetype)initWithViewController:(UIViewController *)viewController {
+    if (self = [super init]) {
+        _viewController = viewController;
+    }
+    return self;
+}
+
+- (void)goTo:(NSString *)jsonString {
+    dispatch_async(dispatch_get_main_queue(), ^{
+        NSDictionary *params = [self convertToDictionary:jsonString];
+        NSString *file = params[@"file"];
+        NSString *webview = @"ui";
+        if ([file isEqualToString:@"b.html"])
+            webview = @"wk";
+        
+        NSURL *url = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"data/%@",file] withExtension:nil];
+        NSString *filePath = [url absoluteString];
+        filePath = [filePath stringByAppendingString:[NSString stringWithFormat:@"?webview=%@",webview]];
+        if ([file isEqualToString:@"c.html"])
+            filePath = [filePath stringByAppendingString:@"&page=tk_page_first"];
+        [JumpManager openPageWithLink:filePath withObject:nil];
+    });
+}
+
+- (NSDictionary *)convertToDictionary:(NSString *)jsonString {
+    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithString:jsonString options:kNilOptions error:nil];
+    if (dictionary) {
+        return dictionary;
+    }
+    return nil;
+}
+
+@end

+ 16 - 0
HybridBridgeProtocol.h

@@ -0,0 +1,16 @@
+//
+//  HybridBridgeProtocol.h
+//  tk_hybrid_demo
+//
+//  Created by terry.guo on 2018/6/19.
+//  Copyright © 2018年 Terry. All rights reserved.
+//
+
+#import <JavaScriptCore/JavaScriptCore.h>
+#import <Foundation/Foundation.h>
+
+@protocol HybridBridgeProtocol <JSExport>
+
+- (void)goTo:(NSString *)jsonString;
+
+@end

+ 21 - 0
JumpManager.h

@@ -0,0 +1,21 @@
+//
+//  JumpManager.h
+//  tk_hybrid_demo
+//
+//  Created by terry.guo on 2018/6/19.
+//  Copyright © 2018年 Terry. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface JumpManager : NSObject
+
++ (BOOL)canAndGoOutWithLink:(NSString *)link;
+
++ (BOOL)openPageWithLink:(NSString *)link;
+
++ (BOOL)openPageWithLink:(NSString *)link withObject:(id)anObject;
+
++ (NSMutableDictionary *)convertLinkURLtoParam:(NSString *)linkURL;
+
+@end

+ 157 - 0
JumpManager.m

@@ -0,0 +1,157 @@
+//
+//  JumpManager.m
+//  tk_hybrid_demo
+//
+//  Created by terry.guo on 2018/6/19.
+//  Copyright © 2018年 Terry. All rights reserved.
+//
+#import "WebViewController.h"
+#import "WKWebViewController.h"
+#import "BaseViewController.h"
+
+#import "JumpManager.h"
+
+static NSString * const kWebviewIdentifier = @"webview";
+static NSString * const kWKWebviewIdentifier = @"wk";
+static NSString * const kUIWebviewIdentifier = @"ui";
+
+@interface JumpManager ()
+
+@property (strong, nonatomic) NSDictionary *mappings;
+
+@end
+
+@implementation JumpManager
+
++ (instancetype)sharedInstance {
+    static dispatch_once_t onceToken;
+    static JumpManager *instance;
+    dispatch_once(&onceToken, ^{
+        instance = [[self alloc] init];
+    });
+    return instance;
+}
+
+- (id)init {
+    self = [super init];
+    if (self) {
+        [self setupMappings];
+    }
+    return self;
+}
+
+- (void)setupMappings {
+    [self registerPage:@"IndexViewController"    identifier:@"tk_page_index"];
+    [self registerPage:@"FirstViewController"    identifier:@"tk_page_first"];
+    [self registerPage:@"SecondViewController"   identifier:@"tk_page_second"];
+}
+
+#pragma mark --是否拦截
++ (BOOL)canAndGoOutWithLink:(NSString *)link {
+    Class class = [[JumpManager sharedInstance]classFromLink:link];
+    return class != nil;
+}
+
+#pragma mark --跳转
++ (BOOL)openPageWithLink:(NSString *)link {
+    return [JumpManager openPageWithLink:link withObject:nil];
+}
+
++ (BOOL)openPageWithLink:(NSString *)link withObject:(id)anObject {
+    Class class = [[JumpManager sharedInstance]classFromLink:link];
+    if (class) {
+        BaseViewController *viewController = [[class alloc]initWithPageLink:link withObject:anObject];
+        return [[JumpManager sharedInstance]pushViewController:viewController];
+    }else {
+        return [JumpManager openH5PageWithLink:link];
+    }
+}
+
++ (BOOL)openH5PageWithLink:(NSString *)link {
+    NSDictionary *urlParams = [self convertLinkURLtoParam:link];
+    NSString *useWhichWebView = urlParams[kWebviewIdentifier];
+    if ([useWhichWebView isEqualToString:kWKWebviewIdentifier]) {
+        WKWebViewController *wkWebViewController = [WKWebViewController webVCWithURLString:link];
+        return [[JumpManager sharedInstance]pushViewController:wkWebViewController];
+    } else {
+        WebViewController *webViewController = [WebViewController webVCWithURLString:link];
+        return [[JumpManager sharedInstance]pushViewController:webViewController];
+    }
+}
+
++ (NSMutableDictionary *)convertLinkURLtoParam:(NSString *)linkURL {
+    NSRange range = [linkURL rangeOfString:@"?"];
+    if (range.location != NSNotFound) {
+        NSString *paramStr = [linkURL substringFromIndex:range.location+1];
+        
+        NSArray *arrParam = [paramStr componentsSeparatedByString:@"&"];
+        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
+        
+        for (NSString *subParam in arrParam) {
+            NSArray *arrsub = [subParam componentsSeparatedByString:@"="];
+            NSString *value = [[arrsub objectAtIndex:1] stringByRemovingPercentEncoding];
+            [dic setObject:value forKey:[arrsub objectAtIndex:0]];
+        }
+        
+        return dic;
+    }
+    return nil;
+}
+
+#pragma mark --私有
+- (Class)classFromLink:(NSString *)aLink {
+    __block  BOOL match;
+   __block NSString *key = nil;
+    [[self.mappings allKeys]enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+       match = [aLink rangeOfString:obj].location != NSNotFound;
+        if (match) {
+            key = obj;
+            *stop = YES;
+        }
+    }];
+    if (key) return NSClassFromString(self.mappings[key]);
+    return nil;
+}
+
+- (void)registerPage:(NSString *)pageName
+          identifier:(NSString *)identifier {
+    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:self.mappings];
+    if (pageName.length > 0 && identifier.length > 0)
+        [dict setObject:pageName forKey:identifier];
+    self.mappings = [NSDictionary dictionaryWithDictionary:dict];
+}
+
+- (BOOL)pushViewController:(UIViewController *)vc {
+    if (vc == nil) {
+        return NO;
+    }
+    
+    UIApplication *app = [UIApplication sharedApplication];
+    UIViewController *rootVC = [app valueForKeyPath:@"delegate.window.rootViewController"];
+    if ([rootVC isKindOfClass:[UITabBarController class]]) {
+        UIViewController *nVC = [(UITabBarController *)rootVC selectedViewController];
+        
+        if ([nVC isKindOfClass:[UINavigationController class]]) {
+            vc.hidesBottomBarWhenPushed = YES;
+            [(UINavigationController *)nVC pushViewController:vc animated:YES];
+            
+            return YES;
+        }
+    } else if ([rootVC isKindOfClass:[UINavigationController class]]) {
+        [(UINavigationController *)rootVC pushViewController:vc animated:YES];
+        return YES;
+    }else {
+        if ([rootVC respondsToSelector:@selector(selectedViewController)]) {
+            UIViewController *nVC = [(UITabBarController *)rootVC selectedViewController];
+            if ([nVC isKindOfClass:[UINavigationController class]]) {
+                vc.hidesBottomBarWhenPushed = YES;
+                [(UINavigationController *)nVC pushViewController:vc animated:YES];
+                
+                return YES;
+            }
+        }
+    }
+    return NO;
+}
+
+@end