Swiftpack.co - eaceto/WKWebView_WarmUp as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by eaceto.
eaceto/WKWebView_WarmUp 1.1.0
A library that speeds up the loading of Web Pages when using WKWebView
⭐️ 6
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/eaceto/WKWebView_WarmUp.git", from: "1.1.0")

WKWebView WarmUp

Swift Platforms

Swifth Package Manager Cocoapods

Swift CI Swift Docs

Latest release

A library that speeds up the loading of Web Pages when using WKWebView.

Table of Contents

  1. References
  2. Requirements
  3. Installation
  4. Usage
  5. Notes
  6. Author

Requirements

Platform Minimun Swift Version Installation Status
iOS 9.0+ 5.3 Cocoapods, Swift Package Manager Fully Tested
macOS 10.12+ 5.3 Cocoapods, Swift Package Manager Fully Tested

Installation

Cocoapods

pod 'WKWebView_WarmUp', '~> <latest version>'

Swift Package Manager

// Inside Package definition
dependencies: [
    .package(url: "https://github.com/eaceto/WKWebView_WarmUp.git", .upToNextMajor(from: "<latest version>"))
]

// Inside Target definition
dependencies: [
    "WKWebView_WarmUp"
]

Usage

note: warmUp should always be called in the Main Thread.

When you want to speed up the loading of a WebView, perform the following request with the required URL / URLRequest to load.

let url = URL(string: "https://duckduckgo.com")!
WKWebViewHeater.shared.warmUp(with: url)

This call should be done as soon as possible, and before your app makes all the critical calls, so the calls performed by the WKWebViewHeater.

Then, when you want to retrieve the warmed-up WebView, just call

let webView = WKWebViewHeater.shared.dequeue(with: url)!

Lifespan

Warmed-up WebView may have a lifespan. In case the end of life of a WebView happens when it's inside the pool (not yet dequeued), the pool will reload it by calling the warmUp method over the WebView automatically.

let url = URL(string: "https://duckduckgo.com")!
WKWebViewHeater.shared.warmUp(with: url, lifespan: 30.0)  //WebView will be automatically reloaded every 30 seconds.

Notes

Remember that this WebView's size is Zero! In order to added to your ViewController, use AutoLayout as follows:

// Declare a variable that holds the WebView
private lazy var webView: WKWebView = {
    let webView: WKWebView!
    webView = WKWebViewHeater.shared.dequeue(url: urlString)
    
    // Set other properties of the WebView
    webView.configuration.allowsInlineMediaPlayback = true
    
    webView.translatesAutoresizingMaskIntoConstraints = false
    webView.scrollView.alwaysBounceHorizontal = false
    return webView
}()

override func viewDidLoad() {
    super.viewDidLoad()

    // Add the WebView
    view.addSubview(webView)

    // Set WebView's delegate and props
    webView.navigationDelegate = self
    
    // User AutoLayout to set its constraints
    let webViewConstraints = [
        webView.topAnchor.constraint(equalTo: view.topAnchor),
        webView.leftAnchor.constraint(equalTo: view.leftAnchor),
        webView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        webView.rightAnchor.constraint(equalTo: view.rightAnchor)
    ]
    NSLayoutConstraint.activate(webViewConstraints)
}

Author

© 2020 Ezequiel (Kimi) Aceto

This project is distributed under MIT License.

GitHub

link
Stars: 6
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v.1.1.0
1 year ago

Added a lifespan to WebViews. When a web view lifespan reaches its end, the pool automatically reloads it. So the information is always up-to-date.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics