#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <WebKit/WebKit.h>
@interface ViewController ()<UIWebViewDelegate, WKNavigationDelegate, WKUIDelegate>
@property (strong, nonatomic) WKWebView *wkWebView;
@end
/**
*
* JSContext:给JavaScript提供运行的上下文环境
* JSValue:JavaScript和Objective-C数据和方法的桥梁
* JSManagedValue:管理数据和方法的类
* JSVirtualMachine:处理线程相关,使用较少
* JSExport:这是一个协议,如果采用协议的方法交互,自己定义的协议必须遵守此协议
*/
@implementation ViewController
#pragma mark ==================== 懒加载 ====================
- (WKWebView *)wkWebView
{
if (!_wkWebView)
{
//创建偏好设置对象
WKPreferences *preferences = [[WKPreferences alloc] init];
//最小字体
preferences.minimumFontSize = 0;
//是否支出JavaScript
preferences.javaScriptEnabled = YES;
//是否允许不经过用户交互,由JavaScript自动打开窗口
preferences.javaScriptCanOpenWindowsAutomatically = NO;
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.preferences = preferences;
_wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
}
return _wkWebView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.wkWebView];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
[_wkWebView loadRequest:request];
}
#pragma mark ==================== WKNavigationDelegate ====================
////决定是否响应网页的某个动作,例如加载,回退,前进,刷新等
//- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
//{
//
//}
//
////接收到数据后是否允许跳转
//- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
//{
//
//}
//
////页面开始加载时调用
//- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
//{
// NSLog(@"%s", __func__);
//}
//
////接收到服务器跳转请求之后调用
//- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation
//{
//
//}
//
////主页数据加载发生错误时调用
//- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
//{
//
//}
//
////当内容提交到主机时调用
//- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation
//{
//
//}
//
////主页加载完成时调用
//- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
//{
//
//}
//
////当提交发生错误时调用
//- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
//{
//
//}
//
//
////需要响应身份验证时调用,在block中传入用户身份凭证
//- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
//{
//
//}
//
////进程被终止时调用
//- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView API_AVAILABLE(macosx(10.11), ios(9.0))
//{
//
//}
#pragma mark ==================== WKUIDelegate ====================
//如果我们新打开了一个tab,就会调用此方法.创建新的WKWebView, 可以指定配置对象、导航动作对象、window特性。如果没有实现这个方法,不会加载链接,如果返回的是原webview会崩溃。
//- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
//{
//
//}
//
////关闭WKWebView时调用
//- (void)webViewDidClose:(WKWebView *)webView API_AVAILABLE(macosx(10.11), ios(9.0))
//{
//
//}
//
//
////JavaScript调用alert方法后回调的方法 message中为alert提示的信息 必须要在其中调用completionHandler()
//- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
//{
//
//}
//
//
////JavaScript调用confirm方法后回调的方法 confirm是js中的确定框,需要在block中把用户选择的情况传递进去
//- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler
//{
//
//}
//
//
////JavaScript调用prompt方法后回调的方法 prompt是js中的输入框 需要在block中把用户输入的信息传入
//- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler
//{
//
//}
//- (BOOL)webView:(WKWebView *)webView shouldPreviewElement:(WKPreviewElementInfo *)elementInfo API_AVAILABLE(ios(10.0))
//{
//
//}
//
//- (nullable UIViewController *)webView:(WKWebView *)webView previewingViewControllerForElement:(WKPreviewElementInfo *)elementInfo defaultActions:(NSArray<id <WKPreviewActionItem>> *)previewActions API_AVAILABLE(ios(10.0))
//{
//
//}
//
//
//- (void)webView:(WKWebView *)webView commitPreviewingViewController:(UIViewController *)previewingViewController API_AVAILABLE(ios(10.0))
//{
//
//}
//
//
//- (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray<NSURL *> * _Nullable URLs))completionHandler API_AVAILABLE(macosx(10.12))
//{
//
//}
@end
网友评论