An iOS/OSX bridge for sending messages between Obj-C and JavaScript in UIWebViews/WebViews
14.29k
stars
2.99k
forks
source link
After presentViewController wkwebview not receive WebViewJavascriptBridge send message? #340
Open
sujiewen opened 6 years ago
@interface SSGWebViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate,WKUIDelegate,WKScriptMessageHandler,WKNavigationDelegate>
@property (nonatomic, strong) WKWebView sWebView; @property WebViewJavascriptBridge bridge;
@end
@implementation SSGWebViewController
(void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; // 设置导航控制器的代理为self self.navigationController.delegate = self;
// 禁止选择CSS NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";
// CSS选中样式取消 NSMutableString *javascript = [NSMutableString string]; [javascript appendString:@"var style = document.createElement('style');"]; [javascript appendString:@"style.type = 'text/css';"]; [javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css]; [javascript appendString:@"style.appendChild(cssContent);"]; [javascript appendString:@"document.body.appendChild(style);"]; [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止长按 [javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];//禁止选择
// javascript注入 WKUserScript noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; WKUserContentController userContentController = [[WKUserContentController alloc] init]; [userContentController addUserScript:noneSelectScript];
WKWebViewConfiguration *config = [WKWebViewConfiguration new]; config.userContentController = userContentController; //初始化偏好设置属性:preferences config.preferences = [WKPreferences new]; config.preferences.minimumFontSize = 10; config.preferences.javaScriptEnabled = YES; config.preferences.javaScriptCanOpenWindowsAutomatically =YES;
WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds configuration:config]; webView.navigationDelegate = self; webView.scrollView.scrollEnabled = NO; // webView.scrollView.delegate = self; webView.UIDelegate =self; [self.view addSubview:webView];
webView.scrollView.bounces = NO; _sWebView = webView; //增加背景 webView.backgroundColor = [UIColor clearColor]; webView.opaque = NO; [webView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"images/map/bg_login.png"]]];
if (_bridge) { return; }
[WebViewJavascriptBridge enableLogging];
_bridge = [WebViewJavascriptBridge bridgeForWebView:_sWebView]; [_bridge setWebViewDelegate:self];
[_bridge registerHandler:@"webjsblog" handler:^(id data, WVJBResponseCallback responseCallback) { NSLog(@"WEBJSBLog"); responseCallback(@"{}"); }];
[_bridge registerHandler:@"colseLiveVHandler" handler:^(id data, WVJBResponseCallback responseCallback) { NSLog(@"colseLiveVHandler called: %@", data);
}
(void)openLiveV:(NSDictionary )data{ SGLiveViewController sgLVC = [[SGLiveViewController alloc] init]; sgLVC.webViewController = self; sgLVC.bridge = self.bridge; sgLVC.dataDict = data; [self presentViewController:sgLVC animated:YES completion:nil]; } @end
After presentViewController wkwebview not receive WebViewJavascriptBridge send message?