Good day,
I have found an interesting behaviour that I can not understand.
I have an app that has a WKWebView. I use locally saved html files to present in the webview. To present each view I have a button for each html file. In the simulator I can go between the views as I tap each button. But in the actual device, I can see both views once and then it is stuck on the second view and will not change back to the first view.
I have moved this issue into a new project where I can play with it with out messing with the rest of the app. I am getting the same issue in the test project.
@IBOutlet weak var wec: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
wec.uiDelegate = self
// Do any additional setup after loading the view.
}
@IBAction func one() {
show_web(str: "one")
}
@IBAction func two() {
show_web(str: "two")
}
func show_web(str: String) {
var dir_name: String!
switch str {
case "one":
dir_name = "one"
case "two":
dir_name = "two"
default:
return
}
let documentDirectory = create_directory_if_needed(directory: dir_name)
let index_html_url = documentDirectory!.appendingPathComponent("index.html")
print("--------------- LOADING WEB VIEW ------------------/n",
index_html_url.path,
"--------------- LOADING WEB VIEW END ------------------/n")
let request = URLRequest(url: index_html_url)
print("???????", request)
self.wec.load(request)
}
func create_directory_if_needed(directory: String) -> URL? {
let fm = FileManager.default
let documentDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask)[0]
let new_folder = documentDirectory.appendingPathComponent(directory)
if !fm.fileExists(atPath: new_folder.path) {
do {
try fm.createDirectory(at: new_folder, withIntermediateDirectories: true, attributes: nil)
}catch {
print("*********** ERROR create directory ***************")
print(error)
print("*********** ERROR End ***************")
return nil
}
}
return new_folder
}
When I print the request it always shows the correct path that I want to be presented but it does not load the correct path.
--------------- LOADING WEB VIEW ------------------/n /Users/jonasrafnsson/Library/Developer/CoreSimulator/Devices/189C901B-CD20-4636-B176-0DCE7AED456D/data/Containers/Data/Application/CB8A2C28-3406-4162-9A6E-6C0B924F6874/Documents/two/index.html --------------- LOADING WEB VIEW END ------------------/n
??????? file:///Users/jonasrafnsson/Library/Developer/CoreSimulator/Devices/189C901B-CD20-4636-B176-0DCE7AED456D/data/Containers/Data/Application/CB8A2C28-3406-4162-9A6E-6C0B924F6874/Documents/two/index.html
--------------- LOADING WEB VIEW ------------------/n /Users/jonasrafnsson/Library/Developer/CoreSimulator/Devices/189C901B-CD20-4636-B176-0DCE7AED456D/data/Containers/Data/Application/CB8A2C28-3406-4162-9A6E-6C0B924F6874/Documents/one/index.html --------------- LOADING WEB VIEW END ------------------/n
??????? file:///Users/jonasrafnsson/Library/Developer/CoreSimulator/Devices/189C901B-CD20-4636-B176-0DCE7AED456D/data/Containers/Data/Application/CB8A2C28-3406-4162-9A6E-6C0B924F6874/Documents/one/index.html
Anyone seen this behaviour before?
Regards, Jonas
The solution I came down to was to add the web view programmatically and create a new web view every time I changed the view.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
@IBOutlet weak var web_view: UIView!
var web: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setup_web()
}
@IBAction func copyD() {
let fm = FileManager.default
let urls = [create_directory_if_needed(directory: "one"), create_directory_if_needed(directory: "two")]
let files = [Bundle.main.url(forResource: "index_1", withExtension: "html"), Bundle.main.url(forResource: "index_2", withExtension: "html")]
for f in 0..<files.count {
do {
try fm.copyItem(atPath: files[f]!.path,
toPath: urls[f]!.appendingPathComponent("index.html").path)
}catch {
print("*********** ERROR Copying Single file **************")
print(error)
print("*********** ERROR End **************")
}
}
}
@IBAction func one() {
reset_webview()
show_web(str: "one")
}
@IBAction func two() {
reset_webview()
show_web(str: "two")
}
func setup_web() {
web = WKWebView(frame: CGRect(x: 0, y: 0, width: web_view.frame.width, height: web_view.frame.height))
web.uiDelegate = self
web_view.addSubview(web)
}
func reset_webview() {
web.removeFromSuperview()
web = nil
setup_web()
}
func show_web(str: String) {
var dir_name: String!
switch str {
case "one":
dir_name = "one"
case "two":
dir_name = "two"
default:
return
}
let documentDirectory = create_directory_if_needed(directory: dir_name)
let index_html_url = documentDirectory!.appendingPathComponent("index.html")
print("--------------- LOADING WEB VIEW ------------------/n",
index_html_url.path,
"--------------- LOADING WEB VIEW END ------------------/n")
let request = URLRequest(url: index_html_url)
print("???????", request)
self.web.load(request)
}
func create_directory_if_needed(directory: String) -> URL? {
let fm = FileManager.default
let documentDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask)[0]
let new_folder = documentDirectory.appendingPathComponent(directory)
if !fm.fileExists(atPath: new_folder.path) {
do {
try fm.createDirectory(at: new_folder, withIntermediateDirectories: true, attributes: nil)
}catch {
print("*********** ERROR create directory ***************")
print(error)
print("*********** ERROR End ***************")
return nil
}
}
return new_folder
}
func check_if_file_exists(url: URL) -> URL? {
let fm = FileManager.default
// Check if the file exists, in case it doesnt we stop here.
let path = url.path
guard fm.fileExists(atPath: path) else {
return nil
}
return url
}
}
These works as intended. Maybe not the easiest way but the only one I managed.
Jonas
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.