Skip to content

How to use NUGUServiceKit for custom webview with javascript

jin-marino edited this page May 17, 2021 · 8 revisions

Custom webview with javascript

Wrote based on v1.1.3 (updated 2021.05.17)

NuguServiceKit

NuguServiceWebView

NuguServiceWebView is a customized webview inherited from WKWebView.

NuguServiceWebView + Convenience

Cookie should be properly injected to NuguServiceWebView. NuguClientKit provides function for user's cookie setting convenience. Use setCookie() method instead if you want your own cookie to be set.

    /// Add cookie to `NuguServiceWebView` which already has default cookie
    /// - Parameter cookie: Custom dictionary for user specific cookie setting.
    ///                     Value will be overwrited when the key is same with default cookie.
    ///                     Few keys are provided as `NuguServiceCookieKey` and for more custom keys, extend `NuguServiceCookieKey`
    func addCookie(_ cookie: [NuguServiceCookieKey.RawValue: Any]?) {
        ...
        // default cookie provided by `NuguClientKit`
        let defaultCookieDictionary: [String: Any] = [
            "Authorization": token,
            "Os-Type-Code": "MBL_IOS",
            "App-Version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
            "Sdk-Version": Bundle(identifier: "com.sktelecom.romaine.NuguServiceKit")?.infoDictionary?["CFBundleShortVersionString"] as? String ?? "",
            "Poc-Id": configuration.pocId,
            "Phone-Model-Name": UIDevice.current.model,
            "Theme": "LIGHT",
            "Oauth-Redirect-Uri": configuration.serviceWebRedirectUri
        ]
        ...
    }

NuguServiceCookieKey

NuguServiceCookieKey can be extended and used for custom key/value injection.

ex>
    public extension NuguServiceCookieKey {
        static let theme = NuguServiceCookieKey("Theme")
        static let deviceUniqueId = NuguServiceCookieKey("Device-Unique-Id")
    }

Public Methods

// Set custom cookie by promised variables as NuguServiceCookie
// - Parameter cookie: custom dictionary for user specific cookie setting
public func setCookie(_ cookie: [String: Any])

// load url request with url string and queries
public func loadUrlString(_ urlString: String?, with queries: [URLQueryItem]? = nil)

NuguServiceWebJavascriptDelegate

NuguServiceWebJavascriptDelegate passes promised javascript method used in NUGU services webview with delegate methods

public func openExternalApp(openExternalAppItem: WebOpenExternalApp)

public func openInAppBrowser(url: String)

// Use reason parameter for custom action
public func closeWindow(reason: String)

ex>
    func closeWindow(reason: String) {
        if reason == "WITHDRAWN_USER" {
            navigationController?.dismiss(animated: true, completion: {
                NuguCentralManager.shared.clearSampleApp()
            })
        } else {
            navigationController?.popViewController(animated: true)
        }
    }