iPhone 7 不显示webview
iPhone 7 not displaying webview
我正在 ios 中设计一个 webview 应用程序。我开始更改我的代码。代码更改完全没问题。该应用程序在模拟器(iPhone 7)中响应,因为它应该响应。但是当我将应用程序部署到设备(iPhone 7)时,应用程序只显示空白页。
这很奇怪。我读了很多文章,他们在谈论网络运营商。目前显示 No Service
。我尝试重新插入 SIM 卡。但这没有用。不知道有没有用
如果有人能告诉我应该尝试调试什么。
Question Update
我如何加载 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)
}
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)
}
URL 创建- "file:///var/mobile/Containers/Data/Application/427943C3-2238-49A3-AFCC-5561062B7CDA/Documents/CODE/index.html"
也许可以为本地文件夹尝试这个。您可以检查您的文件是否退出。
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")
}
}
尝试通过此检查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
我正在 ios 中设计一个 webview 应用程序。我开始更改我的代码。代码更改完全没问题。该应用程序在模拟器(iPhone 7)中响应,因为它应该响应。但是当我将应用程序部署到设备(iPhone 7)时,应用程序只显示空白页。
这很奇怪。我读了很多文章,他们在谈论网络运营商。目前显示 No Service
。我尝试重新插入 SIM 卡。但这没有用。不知道有没有用
如果有人能告诉我应该尝试调试什么。
Question Update
我如何加载 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)
}
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)
}
URL 创建- "file:///var/mobile/Containers/Data/Application/427943C3-2238-49A3-AFCC-5561062B7CDA/Documents/CODE/index.html"
也许可以为本地文件夹尝试这个。您可以检查您的文件是否退出。
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")
}
}
尝试通过此检查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