Skip to content
Open

NO #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Classes/IMYWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
- (void)webViewDidFinishLoad:(IMYWebView*)webView;
- (void)webView:(IMYWebView*)webView didFailLoadWithError:(NSError*)error;
- (BOOL)webView:(IMYWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)wkWebViewShowAlterInfo:(NSString *)message;

- (void)wkWebViewShowConformInfo:(NSString *)message block:(void (^_Nonnull)(BOOL result))completionHandler;

- (void)wkWebViewShowInputPanelInfo:(nullable NSString *)prompt defaultText:(nullable NSString *)defaultText block:(void (^_Nonnull)(NSString * _Nullable result))completionHandler;

@end

Expand All @@ -44,6 +49,9 @@
///WKWebView 跟网页进行交互的方法。
- (void)addScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler name:(NSString*)name;

- (void)removeScriptMessageHandlerName:(NSString*)name;


///back 层数
- (NSInteger)countOfHistory;
- (void)gobackWithStep:(NSInteger)step;
Expand All @@ -53,6 +61,7 @@

- (id)loadRequest:(NSURLRequest*)request;
- (id)loadHTMLString:(NSString*)string baseURL:(NSURL*)baseURL;
- (id)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL;

@property (nonatomic, readonly, copy) NSString* title;
@property (nonatomic, readonly) NSURLRequest* currentRequest;
Expand Down
55 changes: 55 additions & 0 deletions Classes/IMYWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ - (void)addScriptMessageHandler:(id<WKScriptMessageHandler>)scriptMessageHandler
[configuration.userContentController addScriptMessageHandler:scriptMessageHandler name:name];
}
}

- (void)removeScriptMessageHandlerName:(NSString*)name {
if (!_usingUIWebView) {
WKWebViewConfiguration* configuration = [(WKWebView*)self.realWebView configuration];
[configuration.userContentController removeScriptMessageHandlerForName:name];
}
}


- (JSContext *)jsContext
{
if (_usingUIWebView) {
Expand Down Expand Up @@ -221,6 +230,39 @@ - (void)webView:(WKWebView*)webView didFailNavigation:(WKNavigation*)navigation
[self callback_webViewDidFailLoadWithError:error];
}
#pragma mark - WKUIDelegate
//wkwebview默认不响应js的alert,设置代理,并且写alert的回调即可
-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{

NSLog(@"%@js想要alert",message);
if ([self.delegate respondsToSelector:@selector(wkWebViewShowAlterInfo:)]) {
[self.delegate wkWebViewShowAlterInfo:message];
}
//一定要写这一句,否则会崩溃
completionHandler();

}


- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
if (self.delegate && [self.delegate respondsToSelector:@selector(wkWebViewShowConformInfo:block:)]) {
[self.delegate wkWebViewShowConformInfo:message block:^(BOOL result) {
completionHandler(result);
}];
}
}


- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler {
if (self.delegate && [self.delegate respondsToSelector:@selector(wkWebViewShowInputPanelInfo:defaultText:block:)]) {
[self.delegate wkWebViewShowInputPanelInfo:prompt defaultText:defaultText block:^(NSString * _Nullable result) {
completionHandler(result);
}];
}
}




///-- 还没用到
#pragma mark - CALLBACK IMYVKWebView Delegate

Expand Down Expand Up @@ -300,6 +342,19 @@ - (id)loadHTMLString:(NSString*)string baseURL:(NSURL*)baseURL
return [(WKWebView*)self.realWebView loadHTMLString:string baseURL:baseURL];
}
}
- (id)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL {
if (data) {
if (_usingUIWebView) {
[(UIWebView*)self.realWebView loadData:data MIMEType:MIMEType textEncodingName:characterEncodingName baseURL:baseURL];
return nil;
}
else {
return [(WKWebView*)self.realWebView loadData:data MIMEType:MIMEType characterEncodingName:characterEncodingName baseURL:baseURL];
}
}else {
return nil;
}
}
- (NSURLRequest*)currentRequest
{
if (_usingUIWebView) {
Expand Down