UIWebView显示本地加载图像的白色屏幕

时间:2016-04-25 13:11:19

标签: ios swift ipad uiwebview

我正在使用Swift编写我的第一个iOS应用程序(仅适用于iPad),其中app会根据应用程序上的资源和用户选择的值加载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升级)。所有资源都存在于应用程序中

1 个答案:

答案 0 :(得分:0)

这段代码对我来说很好。我的猜测是你在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.
    }
}

我包含了拉链,因此您可以运行它,看看它是否适用于您。注意我使用的是Xcode 7.3。通常建议您查看Xcode版本的最新信息。

相关问题