You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Swift中通过UIWebView加载docx等微软格式文件

Fixing UIWebView White Screen When Loading Local Office Files (docx/xls) on iOS Devices

Hey there, let's work through this frustrating white screen issue you're seeing when trying to load docx/xls files in UIWebView on real iOS devices. Here's what's going on and how to fix it:

Why This Happens

First off, UIWebView is deprecated (Apple stopped supporting it officially after iOS 12, and it's been phased out in newer iOS versions). It has very limited native support for Office file formats, which is why you're getting a white screen on real devices (simulators might behave differently because of macOS's underlying rendering engine). Even if it works occasionally, it's not reliable for production apps.

Step-by-Step Solutions

WKWebView is Apple's modern, supported web view component with better performance, security, and format support. Here's how to switch:

Update Your Storyboard/Code

  • In your storyboard, delete the UIWebView and drag in a WKWebView instead.
  • Connect it to your view controller with an IBOutlet:
    @IBOutlet weak var fileServerWebView: WKWebView!
    

Load the Local File Correctly

Use loadFileURL instead of loadRequest—this method is designed for local file access and handles permissions better:

override func viewDidLoad() {
    super.viewDidLoad()
    
    guard let filePath = Bundle.main.path(forResource: "easychair", ofType: "docx") else {
        print("Cannot find file path")
        return
    }
    
    let fileURL = URL(fileURLWithPath: filePath)
    // Allow read access to the entire bundle directory
    let directoryURL = fileURL.deletingLastPathComponent()
    
    fileServerWebView.loadFileURL(fileURL, allowingReadAccessTo: directoryURL)
}

2. Verify Your File is Properly Bundled

Even if your code finds the path, make sure the file is actually included in your app's bundle:

  • Go to your Xcode project > Select your target > Navigate to Build Phases > Copy Bundle Resources
  • Check if easychair.docx is in the list. If not, click the + button and add it.

3. Fallback: Use Apple's Native QLPreviewController

If WKWebView still doesn't render the file correctly (some complex Office files might have issues), use QLPreviewController—this is Apple's built-in preview system that natively supports all Office formats:

Implement the Preview Controller

First, import the QuickLook framework:

import QuickLook

Make your view controller conform to QLPreviewControllerDataSource:

class YourViewController: UIViewController, QLPreviewControllerDataSource {
    // ... your existing code ...
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let filePath = Bundle.main.path(forResource: "easychair", ofType: "docx") else {
            print("Cannot find file path")
            return
        }
        
        let fileURL = URL(fileURLWithPath: filePath)
        let previewController = QLPreviewController()
        previewController.dataSource = self
        previewController.currentPreviewItemIndex = 0
        present(previewController, animated: true)
    }
    
    // MARK: - QLPreviewControllerDataSource
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        guard let filePath = Bundle.main.path(forResource: "easychair", ofType: "docx") else {
            fatalError("File not found")
        }
        return URL(fileURLWithPath: filePath) as QLPreviewItem
    }
}

Final Notes

  • Avoid UIWebView entirely: It's no longer supported, and using it might lead to app rejection in the App Store.
  • QLPreviewController is the most reliable way to preview Office files on iOS—it handles formatting, scrolling, and even editing (if enabled) seamlessly.

内容的提问来源于stack exchange,提问作者Visal Sambo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:51:32