关于IOS UIWebView内存消耗过高的问题

1 篇文章 0 订阅
1 篇文章 0 订阅

最近发现app在打开一款H5游戏玩的时候经常莫名其妙就崩溃了,然后各种找原因发现在玩的时候memory消耗达将近200M,蒙了。这么大,而且退出后内存消耗也没怎么降下来。debug模式下玩了几次游戏崩溃,报错确实是内存问题。网上一查发现不是个例,基本来说是UIWebView本身的问题,大家都建议使用WKWebView。由于WKWebView是IOS8才开始的,所以在使用的时候我先判断IOS版本号,IOS8以上用WKWebview.经测试发现内存消耗大大减少,在打开同样的H5游戏的时候内存消耗降到了60M左右,加载速度似乎也快了很多,退出的时候内存消耗有释放掉,不会因为不断打开而持续升高。

具体使用方法:1.引入WebKit.framework.

2.在要用到的地方加入头文件 #import <WebKit/WebKit.h>

3.然后就是常规的用法了,要说的是弹窗问题,记得设置WKUIDelegate的2个方法,不然网页的alert弹不出来


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

{   

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message

                                                                             message:nil

                                                                      preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"确定"

                                                        style:UIAlertActionStyleCancel

                                                      handler:^(UIAlertAction *action) {

                                                          completionHandler();

                                                      }]];

    

    [self presentViewController:alertController animated:YES completion:^{}];

    

}


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

    

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message

                                                                             message:nil

                                                                      preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"确定"

                                                        style:UIAlertActionStyleDefault

                                                      handler:^(UIAlertAction *action) {

                                                          completionHandler(YES);

                                                      }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"取消"

                                                        style:UIAlertActionStyleCancel

                                                      handler:^(UIAlertAction *action){

                                                          completionHandler(NO);

                                                      }]];

    

    [self presentViewController:alertController animated:YES completion:^{}];

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值