iOS_経験(4)jsとocインタラクション(UI&WKWebView)
4026 ワード
一丶说明
二丶iOS呼び出しjs
WKWebView
UIWebView
三丶js呼び出しiOS
WKWebView
UIWebView
四丶注意
五丶github
https://github.com/k373379320/OCAndJs
成熟したサードパーティ:https://github.com/marcuswestin/WebViewJavascriptBridge
WKWebView
、
WKWebView HTML5
WKWebView , UIWebView 1/3 ~ 1/4
WKWebView 60fps
WKWebView Safari JavaScript
WKWebView
UIWebViewDelegate UIWebView 14 3
WKWebView :http://www.jianshu.com/p/7bb5f15f1daa
二丶iOS呼び出しjs
WKWebView
:
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;
: js hello() , " iOS "
iOS ;
[self.webView evaluateJavaScript:@"hello(' iOS ')" completionHandler:nil];
js
function hello(msg)
{
document.write(msg);
}
UIWebView
iOS
[self.webView stringByEvaluatingJavaScriptFromString:@"hello(' iOS ')"];
js:
function hello(msg) {
document.write(msg)
}
三丶js呼び出しiOS
WKWebView
:
- (void)addScriptMessageHandler:(id )scriptMessageHandler name:(NSString *)name;
:js , iOS hello_OC ; :{hello:1};
js :
oc :
1.
2. /
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
// :
// [config.userContentController addScriptMessageHandler:self name:@"hello_OC"];
(WeakScriptMessageDelegate , )
[config.userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"hello_OC"];
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero
configuration:config];
3.
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
NSLog(@"", message.name, message.body);
}
4.WeakScriptMessageDelegate
#import
#import
@interface WeakScriptMessageDelegate : NSObject
@property (nonatomic, weak) id scriptDelegate;
- (instancetype)initWithDelegate:(id)scriptDelegate;
@end
@implementation WeakScriptMessageDelegate
- (instancetype)initWithDelegate:(id)scriptDelegate
{
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
@end
UIWebView
iOS :
#pragma mark - delegete
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"__>%s",__func__);
NSLog(@"__>%@",request.URL.absoluteString);
NSString *parameterStr = [[request.URL.absoluteString componentsSeparatedByString:@"parm:"] lastObject];
NSDictionary *dict = [parameterStr ZB_dictionaryWithURLString];
NSLog(@"js oc, :%@",dict);
return YES;
}
js :
function toOC(argument) {
/*UIWebVIew*/
window.location.href = "parm:hello iOS"
}
function iOSShareApp(argument) {
/*UIWebView*/
window.location.href = "parm:shareTitle=hyd&shareDesc=' '&shareUrl=' '&sharePic='www.hyd.com'"
}
四丶注意
五丶github
https://github.com/k373379320/OCAndJs
成熟したサードパーティ:https://github.com/marcuswestin/WebViewJavascriptBridge