一、头文件
// webview 配置,具体看下面
@property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration;
// 导航代理
@property (nullable, nonatomic, weak) id navigationDelegate;
// 用户交互代理
@property (nullable, nonatomic, weak) id UIDelegate;
// 页面前进、后退列表
@property (nonatomic, readonly, strong) WKBackForwardList *backForwardList;
// 默认构造器
- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
// 已不再使用
- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;
// 与UIWebView一样的加载请求API
- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
// 加载URL
- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL NS_AVAILABLE(10_11, 9_0);
// 直接加载HTML
- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
// 直接加载data
- (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString*)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL NS_AVAILABLE(10_11, 9_0);
// 前进或者后退到某一页面
- (nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem*)item;
// 页面的标题,这昆支持KVO的
@property (nullable, nonatomic, readonly, copy) NSString *title;
// 当前请求的URL,它是支持KVO的
@property (nullable, nonatomic, readonly, copy) NSURL *URL;
// 标识当前是否正在加载内容中,它是支持KVO的
@property (nonatomic, readonly, getter=isLoading) BOOL loading;
// 当前加载的进度,范围为[0, 1]
@property (nonatomic, readonly) double estimatedProgress;
// 标识页面中的所有资源是否通过安全加密连接来加载,它是支持KVO的
@property (nonatomic, readonly) BOOL hasOnlySecureContent;
// 当前导航的证书链,支持KVO
@property (nonatomic, readonly, copy) NSArray *certificateChainNS_AVAILABLE(10_11, 9_0);
// 是否可以招待goback操作,它是支持KVO的
@property (nonatomic, readonly) BOOL canGoBack;
// 是否可以执行gofarward操作,支持KVO
@property (nonatomic, readonly) BOOL canGoForward;
// 返回上一页面,如果不能返回,则什么也不干
- (nullable WKNavigation *)goBack;
// 进入下一页面,如果不能前进,则什么也不干
- (nullable WKNavigation *)goForward;
// 重新载入页面
- (nullable WKNavigation *)reload;
// 重新从原始URL载入
- (nullable WKNavigation *)reloadFromOrigin;
// 停止加载数据
- (void)stopLoading;
// 执行JS代码
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;
// 标识是否支持左、右swipe手势是否可以前进、后退
@property (nonatomic) BOOL allowsBackForwardNavigationGestures;
// 自定义user agent,如果没有则为nil
@property (nullable, nonatomic, copy) NSString *customUserAgentNS_AVAILABLE(10_11, 9_0);
// 在iOS上默认为NO,标识不允许链接预览
@property (nonatomic) BOOL allowsLinkPreview NS_AVAILABLE(10_11, 9_0);
#if TARGET_OS_IPHONE
/*! @abstract The scroll view associated with the web view.
*/
@property (nonatomic, readonly, strong) UIScrollView *scrollView;
#endif
#if !TARGET_OS_IPHONE
// 标识是否支持放大手势,默认为NO
@property (nonatomic) BOOL allowsMagnification;
// 放大因子,默认为1
@property (nonatomic) CGFloat magnification;
// 根据设置的缩放因子来缩放页面,并居中显示结果在指定的点
- (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;
#endif
二、枚举类型
1. WKUserScriptInjectionTime
它是一个枚举类型,只有在文档开始加载时注入和加载结束时注⼊。
typedef NS_ENUM(NSInteger, WKUserScriptInjectionTime) {
WKUserScriptInjectionTimeAtDocumentStart, // 开始加载时注入
WKUserScriptInjectionTimeAtDocumentEnd
} NS_ENUM_AVAILABLE(10_10, 8_0); // 加载结束时注⼊
2. WKSelectionGranularity
typedef NS_ENUM(NSInteger, WKSelectionGranularity) {
WKSelectionGranularityDynamic,//用户长按复制文字的选择区域是用户自定义
WKSelectionGranularityCharacter,
} API_AVAILABLE(ios(8.0));// 用户长按复制文字的选择区域是非用户自定义
3. WKNavigationActionPolicy
导航动作决定策略:
typedef NS_ENUM(NSInteger, WKNavigationActionPolicy) {
WKNavigationActionPolicyCancel, // 不允许导航,不会跳转链接
WKNavigationActionPolicyAllow,// 允许导航
} NS_ENUM_AVAILABLE(10_10, 8_0);
4. WKNavigationType
typedef NS_ENUM(NSInteger, WKNavigationType) {
WKNavigationTypeLinkActivated, // 链接已经点击
WKNavigationTypeFormSubmitted, // 表单提交
WKNavigationTypeBackForward, // 前进、后退
WKNavigationTypeReload, // 重新载入
WKNavigationTypeFormResubmitted, //表单重新提交
WKNavigationTypeOther = -1, // 其它
} API_AVAILABLE(macosx(10.10), ios(8.0));
5. WKNavigationResponsePolicy
typedef NS_ENUM(NSInteger, WKNavigationResponsePolicy) {
WKNavigationResponsePolicyCancel, // 不接收响应
WKNavigationResponsePolicyAllow, // 接收响应
} API_AVAILABLE(macosx(10.10), ios(8.0));
6. NSURLSessionAuthChallengeDisposition
typedef NS_ENUM(NSInteger, NSURLSessionAuthChallengeDisposition) {
NSURLSessionAuthChallengeUseCredential = 0, // 使用(信任)证书
NSURLSessionAuthChallengePerformDefaultHandling = 1, // 默认,忽略
NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2, // 取消
NSURLSessionAuthChallengeRejectProtectionSpace = 3, // 这次取消,下载次还来问
} NS_ENUM_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0);
7. NSURLCredentialPersistence
程序可以保留证书, 并有以下几种保留模式
typedef NS_ENUM(NSUInteger, NSURLCredentialPersistence) {
NSURLCredentialPersistenceNone, // 要求 URL 载入系统 “在用完相应的认证信息后立刻丢弃”。
NSURLCredentialPersistenceForSession, // 要求 URL 载入系统 “在应用终止时,丢弃相应的 credential ”。
NSURLCredentialPersistencePermanent, // 要求 URL 载入系统 "将相应的认证信息存入钥匙串(keychain),以便其他应用也能使用。
NSURLCredentialPersistenceSynchronizable API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0)) // 此凭据将永久存储。此外,该凭证将基于拥有的AppleID分发给其他设备。注意:在Mac OS X中,只要用户给予许可,任何应用程序都可以访问任何凭证,而在iOS中,应用程序可以只访问它自己的凭证。
};
三、 相关类
1. WKWebViewConfiguration配置
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
2. WKPreferences偏好设置
// 设置偏好设置
config.preferences = [[WKPreferences alloc] init];
// 默认为0
config.preferences.minimumFontSize = 10;
// 默认认为YES
config.preferences.javaScriptEnabled = YES;
// 在iOS上默认为NO,表示不不能⾃自动通过窗⼝口打开
config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
3. WKProcessPool内容处理理池
WKProcessPool并没有公开任何的属性或者⽅方法,不不需要配置:
config.processPool = [[WKProcessPool alloc] init];
4. WKUserContentController内容交互控制器器
我们要通过JS与webview内容交互,就需要到这个类了了,它的所有属 性及⽅方法说明如下:
// 只读属性,所有添加的WKUserScript都在这⾥里里可以获取到
@property (nonatomic, readonly, copy) NSArray *userScripts;
// 注⼊入JS
- (void)addUserScript:(WKUserScript *)userScript;
// 移除所有注⼊入的JS
- (void)removeAllUserScripts;
// 添加scriptMessageHandler到所有的frames中,则都可以通过
window.webkit.messageHandlers..postMessage()
// 发送消息
// ⽐比如,JS要调⽤用我们原⽣生的⽅方法,就可以通过这种⽅方式了了
- (void)addScriptMessageHandler:(id )scriptMessageHandler name: (NSString *)name;
// 根据name移除所注⼊入的scriptMessageHandler
- (void)removeScriptMessageHandlerForName:(NSString *)name;
5. WKUserScript
在WKUserContentController中,所有使⽤用到WKUserScript。 WKUserContentController是⽤用于与JS交互的类,⽽而所注⼊入的JS是 WKUserScript对象。它的所有属性和⽅方法如下:
// JS源代码
@property (nonatomic, readonly, copy) NSString *source;
// JS注⼊入时间
@property (nonatomic, readonly) WKUserScriptInjectionTime injectionTime;
// 只读属性,表示JS是否应该注⼊入到所有的frames中还是只有main frame.
@property (nonatomic, readonly, getter=isForMainFrameOnly) BOOL forMainFrameOnly;
// 初始化⽅方法,⽤用于创建WKUserScript对象
// source:JS源代码
// injectionTime:JS注⼊入的时间
// forMainFrameOnly:是否只注⼊入main frame
- (instancetype)initWithSource:(NSString *)source injectionTime (WKUserScriptInjectionTime)injectionTime forMainFrameOnly:(BOOL)forMainFrameOnly;
6. WKWebsiteDataStore存储的Web内容
iOS9.0以后才能使⽤用这个类。它是代表webview不不同的数据类型,包 括cookies、disk、memory caches、WebSQL、IndexedDB数据库 和本地存储。 要求iOS9.0 以上才能使用。 WKProcessPool并没有公开任何的属性或者⽅方法,不不需要配置:
// 默认数据存储
+ (WKWebsiteDataStore *)defaultDataStore;
// 返回⾮非持久化存储,数据不不会写⼊入⽂文件系统
+ (WKWebsiteDataStore *)nonPersistentDataStore;
// 已经不不可⽤用
- (instancetype)init NS_UNAVAILABLE;
// 只读属性,表示是否是持久化存储
@property (nonatomic, readonly, getter=isPersistent) BOOL persistent;
// 获取所有web内容的数据存储类型集,⽐比如cookies、disk等
+ (NSSet *)allWebsiteDataTypes;
// 获取某些指定数据存储类型的数据
- (void)fetchDataRecordsOfTypes:(NSSet*)dataTypes completionHandler:(void (^) (NSArray *))completionHandler;
// 删除某些指定类型的数据
- (void)removeDataOfTypes:(NSSet *)dataTypes forDataRecords:(NSArray*)dataRecords completionHandler:(void (^) (void))completionHandler;
// 删除某些指定类型的数据且修改⽇日期是指定的⽇日期
- (void)removeDataOfTypes:(NSSet *)websiteDataTypes modifiedSince:(NSDate *)date completionHandler:(void (^)(void))completionHandler;
所有的dataTypes是下⾯面这些系统所定义的:
WK_EXTERN NSString * const WKWebsiteDataTypeDiskCache NS_AVAILABLE(10_11, 9_0);
WK_EXTERN NSString * const WKWebsiteDataTypeMemoryCache NS_AVAILABLE(10_11, 9_0);
WK_EXTERN NSString * const WKWebsiteDataTypeOfflineWebApplicationCache NS_AVAILABLE(10_11, 9_0);
WK_EXTERN NSString * const WKWebsiteDataTypeCookies NS_AVAILABLE(10_11, 9_0);
WK_EXTERN NSString * const WKWebsiteDataTypeSessionStorage NS_AVAILABLE(10_11, 9_0);
WK_EXTERN NSString * const WKWebsiteDataTypeLocalStorage NS_AVAILABLE(10_11, 9_0);
WK_EXTERN NSString * const WKWebsiteDataTypeWebSQLDatabases NS_AVAILABLE(10_11, 9_0);
WK_EXTERN NSString * const WKWebsiteDataTypeIndexedDBDatabases NS_AVAILABLE(10_11, 9_0);
7. WKWebsiteDataRecord
iOS9.0以后才可⽤用。 website的数据存储记录类型,它只有两个属性:
// 通常是域名
@property (nonatomic, readonly, copy) NSString
*displayName;
// 存储的数据类型集
@property (nonatomic, readonly, copy) NSSet
*dataTypes;
8. WKNavigationResponse
WKNavigationResponse是导航响应类,通过它可以获取相关响应的 信息:
// 是否是main frame
@property (nonatomic, readonly, getter=isForMainFrame) BOOL forMainFrame;
// 获取响应response
@property (nonatomic, readonly, copy) NSURLResponse *response;
// 是否显示MIMEType
@property (nonatomic, readonly) BOOL canShowMIMEType;
9. WKNavigationAction
// 正在请求的导航的frame
@property (nonatomic, readonly, copy) WKFrameInfo *sourceFrame;
// 目标frame,如果这是新的window,它会是nil
@property (nullable, nonatomic, readonly, copy) WKFrameInfo *targetFrame;
// 导航类型,如下面的小标题WKNavigationType
@property (nonatomic, readonly) WKNavigationType navigationType;
// 导航的请求
@property (nonatomic, readonly, copy) NSURLRequest *request;
10. WKBackForwardList
WKBackForwardList表示webview中可以前进或者后退的⻚⾯列表。
// 当前正在显示的item(页面)
@property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *currentItem;
// 后一页,如果没有就是nil
@property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *backItem;
// 前一页,如果没有就是nil
@property (nullable, nonatomic, readonly, strong) WKBackForwardListItem *forwardItem;
// 可以进行goback操作的页面列表
@property (nonatomic, readonly, copy) NSArray *backList;
// 可以进行goforward操作的页面列表
@property (nonatomic, readonly, copy) NSArray *forwardList;
// 根据下标获取某一个页面的item
- (nullable WKBackForwardListItem *)itemAtIndex: (NSInteger)index;
11. WKBackForwardListItem
⻚面导航前进、后退列列表项:
// 该页面的URL
@property (readonly, copy) NSURL *URL;
// 该页面的title
@property (nullable, readonly, copy) NSString *title;
// 初始请求该item的请求的URL
@property (readonly, copy) NSURL *initialURL;
四、 WKNavigationDelegate
// 决定导航的动作,通常用于处理跨域的链接能否导航。WebKit对跨域进⾏了安全检查限制,不允许跨域,因此我们要对不能跨域的链接单独处理。但是,对于Safari是允许跨域的,不⽤这么处理。 这个是决定是否Request
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
// 决定是否接收响应,这个是决定是否接收response, 要获取response,通过WKNavigationResponse对象获取
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
// 当main frame的导航开始请求时,会调⽤用此⽅方法
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation;
// 当main frame接收到服务重定向时,会回调此⽅方法
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation: (null_unspecified WKNavigation *)navigation;
// 当main frame开始加载数据失败时,会回调
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
// 当main frame的web内容开始到达时,会回调
- (void)webView:(WKWebView *)webView didCommitNavigation: (null_unspecified WKNavigation *)navigation;
// 当main frame导航完成时,会回调
- (void)webView:(WKWebView *)webView didFinishNavigation: (null_unspecified WKNavigation *)navigation;
// 当main frame最后下载数据失败时,会回调
- (void)webView:(WKWebView *)webView didFailNavigation: (null_unspecified WKNavigation *)navigation withError: (NSError *)error;
// 这与用于授权验证的API,与AFN、UIWebView的授权验证API是一样的
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler;
// 当web content处理理完成时,会回调
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);
五、WKUIDelegate
// 创建新的webview,可以指定配置对象、导航动作对象、window特性
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;
// webview关闭时回调
- (void)webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);
// 调⽤用JS的alert()⽅方法
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;
// 调⽤用JS的confirm()⽅方法
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;
// 调⽤用JS的prompt()⽅方法
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler: (void (^)(NSString * __nullable result))completionHandler;
六、WKWebView与JS交互
1.WKScriptMessageHandler 协议:能让网页通过JS把消息发送给OC
协议方法
WKUserContentController可以理解为调度器,WKScriptMessage则是携带的数据。
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;
WKUserContentController
核心功能:
// js注入,即向网页中注入我们的js方法
- (void)addUserScript:(WKUserScript *)userScript;
//添加供js调用oc的桥梁。这里的name对应WKScriptMessage中的name,多数情况下我们认为它就是方法名。
- (void)addScriptMessageHandler:(id )scriptMessageHandler name:(NSString *)name;
WKScriptMessage
WKScriptMessage就是js通知oc的数据。
//对应- (void)addScriptMessageHandler:(id )scriptMessageHandler name:(NSString *)name;添加的name。
@property (nonatomic, readonly, copy) NSString *name;
// 携带的核心数据。
@property (nonatomic, readonly, copy) id body;
js调用时只需
window.webkit.messageHandlers..postMessage()
JS调用OC
1.配置WKUserContentController
/** WKWebView设置 */
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
WKUserContentController *usercontentController = [[WKUserContentController alloc]init];
// 添加要处理的方法名
[usercontentController addScriptMessageHandler:self name:@"ScanAction"];
[usercontentController addScriptMessageHandler:self name:@"Location"];
[usercontentController addScriptMessageHandler:self name:@"Share"];
[usercontentController addScriptMessageHandler:self name:@"Color"];
[usercontentController addScriptMessageHandler:self name:@"Pay"];
[usercontentController addScriptMessageHandler:self name:@"Shake"];
[usercontentController addScriptMessageHandler:self name:@"GoBack"];
config.userContentController = usercontentController;
_wkWebView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:config];
2.在handle代理方法中判断message为哪个方法名调用不同的方法
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
NSLog(@"方法名:%@", message.name);
NSLog(@"参数:%@", message.body);
// 方法名
NSString *methods = [NSString stringWithFormat:@"%@:", message.name];
SEL selector = NSSelectorFromString(methods);
// 调用方法
if ([self respondsToSelector:selector])
{
[self performSelector:selector withObject:message.body];
} else {
NSLog(@"未实行方法:%@", methods);
}
}
3.在dealloc中移除Handler,防止循引用
-(void)dealloc
{
[_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"ScanAction"];
[_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"Location"];
[_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"Share"];
[_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"Color"];
[_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"Pay"];
[_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"Shake"];
[_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"GoBack"];
}
OC调用JS
/** 将结果返回给js (OC调用JS) 方法名(参数1,参数2,... ,参数n) */
NSString *jsStr = [NSString stringWithFormat:@"setLocation('%@')",@"北辰世纪中心"];
[self.wkWebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"getLocation:%@----%@",result, error);
}];
UIDelegate
/** 显示一个JS的Alert(与JS交互)*/
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提醒" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler();
}];
[alertVc addAction:action];
[self presentViewController:alertVc animated:YES completion:nil];
}
/** 弹出一个输入框(与JS交互)*/
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler
{
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:prompt message:defaultText preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(alertVc.textFields[0].text);
}];
/** 定义第一个输入框 */
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
{
textField.placeholder = defaultText;
}];
[alertVc addAction:action];
[self presentViewController:alertVc animated:YES completion:nil];
}
/** 显示一个确认框 */
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler
{
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"确认框" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(1);
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(0);
}];
[alertVc addAction:okAction];
[alertVc addAction:cancelAction];
[self presentViewController:alertVc animated:YES completion:nil];
}