【问题标题】:UIWebView displaying white screen for locally loaded imagesUIWebView 为本地加载的图像显示白屏
【发布时间】:2016-08-18 21:42:38
【问题描述】:

我正在使用Swift 编写我的第一个 iOS 应用程序(仅适用于 iPad),其中,应用程序将根据应用程序上存在的资源和用户选择的值加载 html。我正在使用以下函数加载数据UIWebView。所有资源都存在于项目中。

        let url =  NSBundle.mainBundle().URLForResource(updatedResource, withExtension: "html")
        let requestObj = NSURLRequest(URL: url!);
        htmlWebView.loadRequest(requestObj);

url是需要加载的资源名称。

print("This is the request \(htmlWebView.request)") 给了我价值

This is the request Optional(<NSMutableURLRequest: 0x7fc180589a00> { URL: file:///Users/ios/Library/Developer/CoreSimulator/Devices/CB5ACC92-856E-4CBE-B4D4-03B0C1774765/data/Containers/Bundle/Application/C588D7E2-15F0-406A-8B09-227E2284459E/DemoHTML5.app/file3.html })

之前,我在 iPad Mini 模拟器上部署了该应用,但是在 iPad 2/iPad Air 模拟器上部署时它显示白屏

P.S:我正在使用 XCode 6.1 并在 iOS 8.1 上运行该应用程序(OS 10.11 升级正在进行中)。所有资源都存在于应用程序中

【问题讨论】:

  • 白屏会永久保留还是会保留一段时间?

标签: ios swift ipad uiwebview


【解决方案1】:

这段代码对我来说很好用。我的猜测是你在 iPad 上的故事板有问题。这是一个演示解决方案的示例项目:

https://www.dropbox.com/s/0xekspq2otqoq3l/SimpleWebViewLoader.zip?dl=0

里面的代码很简单:

class ViewController: UIViewController {
    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url =  NSBundle.mainBundle().URLForResource("test", withExtension: "html")
        let requestObj = NSURLRequest(URL: url!)
        webView.loadRequest(requestObj) 
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我包含了 zip,因此您可以运行它,看看它对您的工作方式是否不同。注意我使用的是 Xcode 7.3。通常建议您查看 Xcode 版本的最新版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-01
    • 2017-01-13
    • 2015-01-22
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多