【问题标题】:iPhone 7 not displaying webviewiPhone 7 不显示网页视图
【发布时间】:2019-01-23 06:32:31
【问题描述】:

我正在 ios 中设计一个 webview 应用程序。我开始更改我的代码。代码更改完全没问题。该应用程序在模拟器(iPhone 7)中响应,因为它应该响应。但是当我将应用程序部署到设备(iPhone 7)时,应用程序只显示空白页。

这很奇怪。我读了很多关于网络运营商的文章。目前它显示No Service。我尝试重新插入 SIM 卡。但这没有用。不知道有没有用。

如果有人能告诉我应该尝试调试什么。

问题更新

我如何加载 webview

案例 1:当我从项目目录中的软链接中获取 index.html 时,我可以在我的设备中进行操作

func getLandingPage() -> URLRequest {
    let path = Bundle.main.path(forResource: "www", ofType: nil)!
    let url = URL(fileURLWithPath: "\(path)/index.html")
    return URLRequest(url: url)
}

已创建网址-"file:///var/containers/Bundle/Application/9890F84F-4CF4-48AB-8874-AC1BC0B77C55/ios.app/www/index.html"

案例 2:当我将所有文件从我的软链接目录复制到设备的 documentDirectory 内的目录中,然后如果我尝试从那里加载 index.html 则它显示空白页。我希望这能正确发生。

func getLandingPage() -> URLRequest {
    let docURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let pathData = "\(docURL[0].path)/\(CODE_FOLDER)/index.html"
    let url     = URL(fileURLWithPath: pathData)

    return URLRequest(url: url)
}

网址创建-"file:///var/mobile/Containers/Data/Application/427943C3-2238-49A3-AFCC-5561062B7CDA/Documents/CODE/index.html"

【问题讨论】:

  • 您是否连接到互联网?
  • 确保您可以在该设备上浏览互联网,即使它是模拟器或真实设备
  • 我已连接到互联网。我也可以浏览它
  • 似乎您的文件未从路径中获取。您能否尝试调试该文件是否存在于路径中。这可以帮助你stackoverflow.com/a/29005938/2323806
  • 检查你的链接路径是否写得正确,或者从wi-fi连接试试看它是否有效?打印你的路径数据并检查它是否正确打印或有错误。

标签: ios iphone swift wkwebview


【解决方案1】:

尝试通过它检查该 URL,如果它打开或没有点击按钮来调试天气,它在您的条件下工作正常或不在设备中。

guard let url = URL(string: "your url string/ path") // or directly your URL
            else { return }
 if UIApplication.shared.canOpenURL(url)
 {
     if #available(iOS 10.0, *) {
            UIApplication.shared.open(url)
     } else {
            UIApplication.shared.openURL(url)
     }
 }
else
{
  print("This doesn't seem to be a valid URL")
}

模拟器上的文档目录与真机不同,你也可以试试:

let documentDirUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)  // give your url here 
let indexFileUrl = documentDirUrl.appendingPathComponent("index.html") // then append the path component of your html file
webView.loadFileURL(indexFileUrl, allowingReadAccessTo: documentDirUrl)    // load the webview

【讨论】:

  • 第一次测试时,我得到了这个日志“这个应用程序不允许查询方案文件”。我正在尝试第二个
  • @ShubhamAgarwalBhewanewala 是的,因为没有在 info.plist 文件中注册 URL 方案。这无济于事,我认为 URL 方案用于检查天气应用程序是否会打开外部链接。我的错。
  • 第二部分解决了我的问题。现在你能解释一下为什么会这样吗?
  • @ShubhamAgarwalBhewanewala 因为模拟器上的 Documents 目录与真机不同,所以和往常一样。一定是这个原因。
  • 理由暂时令人满意。感谢您的帮助
【解决方案2】:

也许试试这个用于本地文件夹。您可以检查您是否是文件退出。

func getLandingPage() -> URLRequest {
let docURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let path = "\(CODE_FOLDER)/index.html"
let url = documentURL.appendingPathComponent(path)
if FileManager.default.fileExists(atPath: (url?.path)!) {
return URLRequest(url: url)
} else {
 print("File doesn't exists")
}
}

【讨论】:

  • 它显示文件存在但 webview 没有加载
  • @ShubhamAgarwalBhewanewala 尝试将您的 html 文件的路径组件附加到目录 url。有关更多信息,请查看我编辑的答案。希望对您有所帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
相关资源
最近更新 更多