WebView一些细节

webview很容易造成内存泄漏, 所以非得用的话要尽可能进行优化, 不过话说回来, 要是webview很适合ios, 那还要ios开发干啥呢

@interface NARegisterServiceDelegateViewController ()<UIWebViewDelegate>
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, assign) BOOL isSuccessfullyLoad;
@end

#pragma mark - 创建webview
- (void)createWebview{
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_webView];
     _webView.delegate = self;
    //适配屏幕
    [_webView setScalesPageToFit:YES];

    [self requestNetwork];
}

#pragma mark - 网络请求
- (void)requestNetwork{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:SERVICE_DELEGATE_URL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData  timeoutInterval:AF_REQUEST_TIMEOUT];
    //    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:SERVICE_DELEGATE_URL]];
    [_webView loadRequest:request];
}

#pragma mark - webview开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView{
    _isSuccessfullyLoad = NO;
}

#pragma mark - webview加载结束
- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [self memoryoptimalize];
    [SVProgressHUD showSuccessWithStatus:@"加载成功"];
    [SVProgressHUD dismissWithDelay:1.5];
    _isSuccessfullyLoad = YES;
}

#pragma mark - webview加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"服务条款加载失败, 错误原因: %@", error);
    if(error.code == -1009){
        [self showLoadFailedAlertWithMessage:@"您似乎断开了网络连接"];
}
    else if (error.code == -1001){
        [self showLoadFailedAlertWithMessage:@"请求超时, 请重试"];
    }
    else{
        [self showLoadFailedAlertWithMessage:@"抱歉, 加载失败"];
    }

    _isSuccessfullyLoad = NO;
}

#pragma mark - 展示加载失败的alert
- (void)showLoadFailedAlertWithMessage:(NSString *)message{
    [SVProgressHUD showErrorWithStatus:message];
    [SVProgressHUD dismissWithDelay:1.5];
}

#pragma mark - 内存优化
- (void)memoryoptimalize{
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (!_isSuccessfullyLoad) {
        [SVProgressHUD showWithStatus:@"正在拼命加载中, 请稍候..."];
    }
}

#pragma mark - 视图将要消失
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
//    [_webView loadHTMLString:@"" baseURL:nil];
    [_webView stopLoading];
}

#pragma mark - dealloc
- (void)dealloc{
    _webView.delegate = nil;
}

感谢http://blog.csdn.net/primer_programer/article/details/24855329

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容