重要:iOS 12+ 棄用 `UIWebView`,新專案**必須使用 `WKWebView`**。本文針對存量專案維護場景,提供 3 種 Loading 實現;新專案請參考 `WKNavigationDelegate` 的 `estimatedProgress` 實現進度條。
原理:蓋一層半透明 View + 菊花轉,`webViewDidStartLoad` 顯示,`webViewDidFinishLoad`/`didFailLoadWithError` 隱藏。
@interface ViewController ()
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIView *loadingContainer;
@property (nonatomic, strong) UIActivityIndicatorView *spinner;
@end
- (void)setupWebView {
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, 320, 400)];
self.webView.delegate = self;
self.webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.webView];
// Loading 容器
self.loadingContainer = [[UIView alloc] initWithFrame:self.view.bounds];
self.loadingContainer.backgroundColor = [UIColor blackColor];
self.loadingContainer.alpha = 0.7;
self.loadingContainer.tag = 103;
[self.view addSubview:self.loadingContainer];
self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.spinner.center = self.loadingContainer.center;
[self.loadingContainer addSubview:self.spinner];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.example.com"]]];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self.spinner startAnimating];
self.loadingContainer.hidden = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.spinner stopAnimating];
self.loadingContainer.hidden = YES;
// 或移除:[self.view viewWithTag:103] removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[self.spinner stopAnimating];
self.loadingContainer.hidden = YES;
// 可選:顯示錯誤提示
}
特點:模態彈窗,用戶無法操作其他介面,適合關鍵頁面強制等待。
@property (nonatomic, strong) UIAlertView *loadingAlert;
- (void)webViewDidStartLoad:(UIWebView *)webView {
if (!self.loadingAlert) {
self.loadingAlert = [[UIAlertView alloc] initWithTitle:nil
message:@"載入中..."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame = CGRectMake(120, 48, 38, 38);
[self.loadingAlert addSubview:spinner];
[spinner startAnimating];
}
[self.loadingAlert show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.loadingAlert dismissWithClickedButtonIndex:0 animated:YES];
self.loadingAlert = nil;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[self.loadingAlert dismissWithClickedButtonIndex:0 animated:YES];
self.loadingAlert = nil;
// 顯示錯誤 Alert
}
⚠️ `UIAlertView` iOS 9+ 棄用,建議改用 `UIAlertController` + 自定義 View。
原理:`UIWebView` 支援 GIF 動畫,載入本地 GIF 檔作為 Loading 圖。
NSData *gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"]];
UIWebView *gifView = [[UIWebView alloc] initWithFrame:CGRectMake(100, 100, 70, 70)];
gifView.userInteractionEnabled = NO;
gifView.backgroundColor = [UIColor clearColor];
[gifView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
[self.view addSubview:gifView];
缺點:WebView 重、記憶體高、GIF 效能差、難以控制顯示/隱藏時機。**僅適合極簡原型**。
// WKWebView 進度條實現
@property (nonatomic, strong) WKWebView *wkWebView;
@property (nonatomic, strong) UIProgressView *progressView;
- (void)setupWKWebView {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
self.wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
self.wkWebView.navigationDelegate = self;
self.wkWebView.UIDelegate = self;
[self.view addSubview:self.wkWebView];
// 進度條
self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 2)];
self.progressView.trackTintColor = [UIColor clearColor];
self.progressView.progressTintColor = [UIColor blueColor];
[self.view addSubview:self.progressView];
// KVO 觀察 estimatedProgress
[self.wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[self.wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.example.com"]]];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.progress = self.wkWebView.estimatedProgress;
self.progressView.hidden = (self.wkWebView.estimatedProgress >= 1.0);
}
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[UIView animateWithDuration:0.3 animations:^{
self.progressView.alpha = 0;
} completion:^(BOOL finished) {
self.progressView.progress = 0;
self.progressView.alpha = 1;
}];
}
- (void)dealloc {
[self.wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}
| 方案 | 優點 | 缺點 | 適用場景 |
|---|---|---|---|
| UIView + Spinner | 可自訂樣式、非阻塞、支援進度文字 | 需自行管理顯示/隱藏邏輯 | 通用頁面、存量 UIWebView 專案 |
| UIAlertView + Spinner | 模態強制等待、代碼極簡 | 阻塞交互、AlertView 棄用、難自訂 | 關鍵流程(支付、登入)強制等待 |
| UIWebView 載入 GIF | 可顯示複雜動畫 | 效能差、記憶體高、控制困難 | 極簡原型、特殊動畫需求 |
| WKWebView + Progress (推薦) | 真實進度、效能優、官方維護 | 需遷移至 WKWebView | **所有新專案、重構專案** |