美文网首页
WKWebView使用中遇到的坑

WKWebView使用中遇到的坑

作者: zhuzhilong | 来源:发表于2019-05-10 17:17 被阅读0次

    WKWebView使用中遇到的坑

    1.在调试中,H5内置弹出框, 在原生中运行不弹出

    解决方案:加上下面几行代码

    - (void)webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(void))completionHandler{

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];

        [alertControlleraddAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            completionHandler();

        }])];

        [self presentViewController:alertController animated:YES completion:nil];

    }

    - (void)webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(NSString*)message initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(BOOL))completionHandler{

        //    DLOG(@"msg = %@ frmae = %@",message,frame);

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];

        [alertControlleraddAction:([UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

            completionHandler(NO);

        }])];

        [alertControlleraddAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            completionHandler(YES);

        }])];

        [self presentViewController:alertController animated:YES completion:nil];

    }

    - (void)webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt defaultText:(NSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void(^)(NSString*_Nullable))completionHandler{

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];

        [alertControlleraddTextFieldWithConfigurationHandler:^(UITextField*_NonnulltextField) {

            textField.text= defaultText;

        }];

        [alertControlleraddAction:([UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            completionHandler(alertController.textFields[0].text?:@"");

        }])];

        [self presentViewController:alertController animated:YES completion:nil];

    }

    2.设置cookie ,有时候H5会获取不到

    解决办法:

    -(void)setTheCookieOfWebView

    {

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.baoFu_token,@"token", nil];

        NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

        //先删除Userid和isbind的cookie

        [_webView evaluateJavaScript:[NSString stringWithFormat:@"document.cookie ='json=%@'",[UIViewController convertToJsonData:dic]] completionHandler:nil];

        NSArray*cookieArray = [NSArrayarrayWithArray:[cookieStoragecookies]];

        for(idobjincookieArray) {

            [cookieStoragedeleteCookie:obj];

        }

    }

    +(NSString*)convertToJsonData:(NSDictionary*)dict{

        NSError*error;

        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions  error:&error];

        NSString*jsonString;

        if(!jsonData) {

            NSLog(@"%@",error);

        }else{

            jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

        }

        NSMutableString*mutStr = [NSMutableStringstringWithString:jsonString];

        NSRangerange = {0,jsonString.length};

        //去掉字符串中的空格

        [mutStrreplaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

        NSRangerange2 = {0,mutStr.length};

        //去掉字符串中的换行符

        [mutStrreplaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

        returnmutStr;

    }

    相关文章

      网友评论

          本文标题:WKWebView使用中遇到的坑

          本文链接:https://www.haomeiwen.com/subject/jpzaoqtx.html