Skip to content

Commit

Permalink
Add webview inspection flag (#3093)
Browse files Browse the repository at this point in the history
* Add webview inspection flag

* Enable web inspector on thomas
  • Loading branch information
rlepinski authored May 7, 2024
1 parent fc31d9c commit bcb1d33
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ struct WKWebViewRepresentable: UIViewRepresentable {
webView.isOpaque = false
webView.backgroundColor = .clear
webView.scrollView.backgroundColor = .clear

webView.navigationDelegate = context.coordinator.nativeBridge

webView.isAccessibilityElement = true
webView.accessibilityLabel = accessibilityLabel

if #available(iOS 16.4, *) {
webView.isInspectable = Airship.isFlying && Airship.config.isWebViewInspectionEnabled
}

if let url = URL(string: self.url) {
updateLoading(true)
webView.load(URLRequest(url: url))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ struct InAppMessageMediaWebView: UIViewRepresentable {
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.scrollView.isScrollEnabled = false

if #available(iOS 16.4, *) {
webView.isInspectable = Airship.isFlying && Airship.config.isWebViewInspectionEnabled
}

return webView
}

Expand Down
4 changes: 4 additions & 0 deletions Airship/AirshipCore/Source/AirshipWebview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ struct WebViewView: UIViewRepresentable {
let webView = WKWebView()
webView.navigationDelegate = context.coordinator.nativeBridge

if #available(iOS 16.4, *) {
webView.isInspectable = Airship.isFlying && Airship.config.isWebViewInspectionEnabled
}

if let url = URL(string: self.url) {
updateLoading(true)
webView.load(URLRequest(url: url))
Expand Down
12 changes: 10 additions & 2 deletions Airship/AirshipCore/Source/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class AirshipConfig: NSObject, NSCopying {
@objc
public var autoPauseInAppAutomationOnLaunch: Bool = false

/// Flag to enable or disable web view inspection on Airship created web views. Applies only to iOS 16.4+.
/// Defaults to `false`
@objc
public var isWebViewInspectionEnabled: Bool = false

/// The airship cloud site. Defaults to `us`.
@objc
public var site: CloudSite = .us
Expand Down Expand Up @@ -453,6 +458,7 @@ public class AirshipConfig: NSObject, NSCopying {
autoPauseInAppAutomationOnLaunch = config.autoPauseInAppAutomationOnLaunch
useUserPreferredLocale = config.useUserPreferredLocale
restoreMessageCenterOnReinstall = config.restoreMessageCenterOnReinstall
isWebViewInspectionEnabled = config.isWebViewInspectionEnabled
}

public func copy(with zone: NSZone? = nil) -> Any {
Expand Down Expand Up @@ -498,7 +504,8 @@ public class AirshipConfig: NSObject, NSCopying {
Enabled features: %ld\n\
Reset enabled features: %ld\n\
Use user preferred locale: %d\n\
Restore Message Center on reinstall: %d\n
Restore Message Center on reinstall: %d\n\
Web view insepection enabled: %d\n
""",
inProduction,
inProduction,
Expand Down Expand Up @@ -536,7 +543,8 @@ public class AirshipConfig: NSObject, NSCopying {
enabledFeatures.rawValue,
resetEnabledFeatures ? "YES" : "NO",
useUserPreferredLocale,
restoreMessageCenterOnReinstall ? "YES" : "NO"
restoreMessageCenterOnReinstall ? "YES" : "NO",
isWebViewInspectionEnabled ? "YES" : "NO"
)
}

Expand Down
5 changes: 5 additions & 0 deletions Airship/AirshipCore/Source/MediaWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ struct MediaWebView: UIViewRepresentable {
webView.backgroundColor = UIColor.black
webView.scrollView.backgroundColor = UIColor.black
webView.navigationDelegate = context.coordinator

if #available(iOS 16.4, *) {
webView.isInspectable = Airship.isFlying && Airship.config.isWebViewInspectionEnabled
}

if type == .video {
let html = String(
format: """
Expand Down
6 changes: 6 additions & 0 deletions Airship/AirshipCore/Source/RuntimeConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public final class RuntimeConfig: NSObject, @unchecked Sendable {
@objc
public var restoreMessageCenterOnReinstall: Bool

/// Flag to enable or disable web view inspection on Airship created web views. Applies only to iOS 16.4+.
/// Defaults to `false`.
@objc
public var isWebViewInspectionEnabled: Bool

private let site: CloudSite
private let remoteConfigCache: RemoteConfigCache
private let notificationCenter: NotificationCenter
Expand Down Expand Up @@ -327,6 +332,7 @@ public final class RuntimeConfig: NSObject, @unchecked Sendable {
self.restoreMessageCenterOnReinstall = config.restoreMessageCenterOnReinstall
self.remoteConfigCache = RemoteConfigCache(dataStore: dataStore)
self.notificationCenter = notificationCenter
self.isWebViewInspectionEnabled = config.isWebViewInspectionEnabled
super.init()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ struct MessageCenterWebView: UIViewRepresentable {
)
webView.allowsLinkPreview = false
webView.navigationDelegate = context.coordinator.nativeBridge

if #available(iOS 16.4, *) {
webView.isInspectable = Airship.isFlying && Airship.config.isWebViewInspectionEnabled
}

return webView
}

Expand Down
3 changes: 2 additions & 1 deletion Thomas/Thomas/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
// Populate AirshipConfig.plist with your app's info from https://go.urbanairship.com
// or set runtime properties here.
let config = AirshipConfig.default()
config.isWebViewInspectionEnabled = true

if config.validate() != true {
showInvalidConfigAlert()
Expand All @@ -32,7 +33,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
// etc.

// Call takeOff (which creates the UAirship singleton)
Airship.takeOff(launchOptions: launchOptions)
Airship.takeOff(config, launchOptions: launchOptions)

// Print out the application configuration for debugging (optional)
print("Config:\n \(config)")
Expand Down

0 comments on commit bcb1d33

Please sign in to comment.