All Downloads are FREE. Search and download functionalities are using the official Maven repository.

iosMain.com.multiplatform.webview.web.WKWebViewExt.kt Maven / Gradle / Ivy

There is a newer version: 1.9.40-alpha04
Show newest version
package com.multiplatform.webview.web

import kotlinx.cinterop.ExperimentalForeignApi
import platform.Foundation.addObserver
import platform.Foundation.removeObserver
import platform.WebKit.WKWebView
import platform.darwin.NSObject

/**
 * Created By Kevin Zou On 2023/9/13
 */
val observedProgressList =
    listOf(
        "estimatedProgress",
        "title",
        "URL",
        "canGoBack",
        "canGoForward",
    )

/**
 * Adds observers for the given properties
 */
@OptIn(ExperimentalForeignApi::class)
fun WKWebView.addObservers(
    observer: NSObject,
    properties: List,
) {
    properties.forEach {
        this.addObserver(
            observer,
            forKeyPath = it,
            options = platform.Foundation.NSKeyValueObservingOptionNew,
            context = null,
        )
    }
}

/**
 * Removes observers for the given properties
 */
fun WKWebView.removeObservers(
    observer: NSObject,
    properties: List,
) {
    properties.forEach {
        this.removeObserver(observer, forKeyPath = it)
    }
}

@OptIn(ExperimentalForeignApi::class)
fun WKWebView.addProgressObservers(observer: NSObject) {
    this.addObservers(
        observer = observer,
        properties = observedProgressList,
    )
}

/**
 * Removes observers for the given properties
 */
fun WKWebView.removeProgressObservers(observer: NSObject) {
    this.removeObservers(
        observer = observer,
        properties = observedProgressList,
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy