Short step, a thousand miles; not small streams, into jianghai
In the current era of mixed-use development, the pure virgin increasingly unrealistic, basically every app will intersect and h5, are familiar with the interaction between native and h5, rapid implementation is also an essential skill needed for each coder needs. (This article webView
is specific to WKWebView
)
First, the first record and the interaction of some h5 iOS does not involve rn and flutter. h5 and native through js构建
the bridge
1, iOS call h5 by js
- (void)evaluateJavaScript:(NSString *)javaScriptString
completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
js webView provides a method of execution, I usually perform some js after finished loading html, such as access to the page height
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
NSString *doc = @"document.body.outerHTML";
[webView evaluateJavaScript:doc
completionHandler:^(id _Nullable htmlStr, NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"html:%@",htmlStr);
}] ;
}
2, h5 callback by native js
iOS: webView need to initialize the configuration
configuration
, to add the appropriate configurationuserContentController
Description: There is no direct useWKScriptMessageHandler
, but it's a custom agents, direct use of strong references, causing a circular reference, this page will not be released
BelowPayParam
an example:
@interface WeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
@end
@implementation WeakScriptMessageDelegate
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate
{
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
webView initialization
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"PayParam"];
cfg.userContentController = userContentController;
WKWebView *webView = [[WKWebView alloc] initWithFrame:(CGRectMake(0, NavH, ScreenWidth, ScreenHeight-NavH)) configuration:cfg];
Method of processing agents
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
WS(ws)
if ([message.name isEqualToString:@"PayParam"]) {
//和h5约定好数据结构
NSDictionary *params = message.body;
//后面是相应的处理
}
}
h5: h5 wording here is relatively simple, just phone models to determine if Apple's mobile phone call the appropriate method on it window.webkit.messageHandlers <object name> .postMessage (<data>).
window.webkit.messageHandlers.PayParam.postMessage({})
Second, the internal webView page hits jump
There can be many click events, but not all events can all be identified within webView page, here are a few I have come across
1, when the promotion page webView do hope from our webView can jump to the store, or jump to promote the app (business package) download address
2, webView telephone, hoping to click on the pop-up prompts call
3, internal h5 there may be a link scheme Jump
code show as below:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSURL *url = navigationAction.request.URL;
NSString *scheme = [url scheme];
UIApplication *app = [UIApplication sharedApplication];
// 打电话
if ([scheme isEqualToString:@"tel"]) {
if ([app canOpenURL:url]) {
[app openURL:url];
// 一定要加上这句,否则会打开新页面
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
// 打开appstore
if ([url.absoluteString containsString:@"itunes.apple.com"]) {
if ([app canOpenURL:url]) {
[app openURL:url];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
//下载企业包
if ([url.absoluteString containsString:@"itms-services://"]) {
if (@available(iOS 10.0, *)) {
[app openURL:url options:@{} completionHandler:^(BOOL success) {
}];
} else {
[app openURL:url];
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
webView whether to open a new url is through this method to judge - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
if it is not legitimate url default returns WKNavigationActionPolicyCancel
, so the need to manually handle
Three, webView play background music does not sound default
webView default is not open to play a sound, you need to manually open
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
cfg.userContentController = userContentController;
cfg.allowsInlineMediaPlayback = YES;
if (@available(iOS 10.0, *)) {
cfg.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
}else {
cfg.mediaPlaybackRequiresUserAction = NO;
}
These are some of the problems encountered and interactive h5, after the discovery of new problems to add ~
git Address:https://github.com/famile/YXBaseVCTool
Slow runner, heard condemning; run fast, just to hear the sound of the wind
Reproduced in: https: //www.jianshu.com/p/6bcdc1096806