Nếu đó là một tệp lớn, bạn không thể/không nên sử dụng UIWebView
.
Tại sao? Tôi đã cố gắng để hiển thị một tập tin tài liệu (docx) với một vài hình ảnh và ứng dụng của tôi đã bị rơi, sau khi ném một cảnh báo bộ nhớ. Lý do rất đơn giản. Mặc dù kích thước tệp là ~ 2,5 MB, thiết bị không có đủ bộ nhớ RAM/bộ nhớ để hiển thị tất cả hình ảnh bitmap (được nhúng trong tài liệu). Gỡ lỗi vấn đề bằng cách sử dụng Instruments cho thấy bộ nhớ ứng dụng đã tăng từ 30 MB lên 230 MB. Tôi tưởng tượng bạn đang trải qua một cái gì đó tương tự.
Giải pháp có thể:
Không cho phép người dùng mở các file lớn trên các thiết bị di động của họ. Hoặc là, hoặc tạm dừng/tạm dừng quá trình tải UIWebView
khi bạn nhận được cảnh báo bộ nhớ.
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if ([self.webView isLoading]) {
[self.webView stopLoading];
}
}
Hãy thử sử dụng phương pháp [UIApplication sharedApplication] openURL:]
để thay thế.
Hãy thử sử dụng UIDocumentInteractionController
để thay thế.
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
documentInteractionController.delegate = self;
BOOL present = [documentInteractionController presentPreviewAnimated:YES];
if (!present) {
// Allow user to open the file in external editor
CGRect rect = CGRectMake(0.0, 0.0, self.view.frame.size.width, 10.0f);
present = [documentInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES];
if (!present) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Cannot preview or open the selected file"
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil, nil];
[alertView show];
}
}
Lưu ý: Tôi đã không cố gắng mở một tập tin chủ đạo bằng cách sử dụng phương pháp nêu trên. Để sử dụng UIDocumentInteractionController
, trước tiên bạn sẽ phải tải tệp xuống.
Hy vọng điều này sẽ hữu ích.
Xin chào, tôi cũng đang gặp phải vấn đề tương tự. Bạn có tìm thấy giải pháp nào cho điều này không? – Ilanchezhian