WebView一些細節(jié)

webview很容易造成內(nèi)存泄漏, 所以非得用的話要盡可能進行優(yōu)化, 不過話說回來, 要是webview很適合ios, 那還要ios開發(fā)干啥呢

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

#pragma mark - 創(chuàng)建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 - 網(wǎng)絡請求
- (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:@"您似乎斷開了網(wǎng)絡連接"];
}
    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 - 內(nèi)存優(yōu)化
- (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 - 視圖將要出現(xiàn)
- (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

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容