N
N
Nicholas2020-10-31 15:54:06
Apple Xcode
Nicholas, 2020-10-31 15:54:06

How to create WKWebView application on Xcode for ios with offline html page?

Greetings. I am a beginner developer. Knowledge can be said to be minimal. I have a small project - a free classifieds board. To begin with, I created for it, in addition to the web version, a simple WebView application for android. But immediately I ran into the need to create an application for ios. I tried to somehow master j2objc, but refused because of the complexity. Recently I found a simple ready-made WKWebView project on Swift. Is it possible to open an offline site in it from html files embedded in the application? In the android application, I did the same for the home page to speed up the work. Do I need to enable cookies, caching, java script? If so, what commands should be entered?
I will be infinitely grateful for the pieces of code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NSA-bot, 2020-10-31
@NSA-bot

Controller view file with WKWebView placed on it in the storyboard.

import UIKit
import WebKit

class ViewController: UIViewController {
  
  @IBOutlet weak var webView: WKWebView! // Аутлет на ваше WKWebView
  
  override func viewDidLoad() {
    super.viewDidLoad()
    show()
  }
  
  // Метод для вывода страницы из файла page.html, который находится в любом месте вашего проекта.
  private func show() {
    let path = Bundle.main.path(forResource: "page", ofType: "html")!
    print(path)
    do {
      let contents = try String(contentsOfFile: path, encoding: .utf8)
      webView.loadHTMLString(contents as String, baseURL: nil)
      webView.sizeToFit()
    } catch let error {
      print("Error: ", error.localizedDescription)
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question