Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
5cf1dfd
feat(paywalls): public API surface for web_view messages (1/7)
cursoragent Jul 15, 2026
f7f5d16
feat(paywalls): web_view wire envelope + JSON hardening (2/7)
cursoragent Jul 15, 2026
d331b34
feat(paywalls): web_view navigation policy + content-rule isolation (…
cursoragent Jul 15, 2026
8d2452a
feat(paywalls): web_view schema component, not yet registered (4/7)
cursoragent Jul 15, 2026
7664d35
Merge webview/3-isolation-navigation into webview/5-session base
cursoragent Jul 15, 2026
b033efa
feat(paywalls): web_view bridge session with document-reset lifecycle…
cursoragent Jul 15, 2026
6a46822
Merge webview/4-schema into webview/6-view base
cursoragent Jul 15, 2026
37aa810
feat(paywalls): web_view component view + view model, not yet wired (…
cursoragent Jul 15, 2026
c132a31
fix(tests): open session via connect in coordinator lifecycle test
cursoragent Jul 15, 2026
24f352c
fix: remove extra vertical whitespace for SwiftLint
cursoragent Jul 15, 2026
07383d3
fix: SwiftLint whitespace, line length, and force_try
cursoragent Jul 15, 2026
96d24e1
fix: wrap web_view identity string under 120 columns
cursoragent Jul 15, 2026
e671378
refactor(paywalls): keep web view bridge internal
cursoragent Jul 16, 2026
41401ac
refactor(paywalls): keep web view bridge internal
cursoragent Jul 16, 2026
b5caeda
refactor(paywalls): keep web view bridge internal
cursoragent Jul 16, 2026
ad3541f
refactor(paywalls): keep web view bridge internal
cursoragent Jul 16, 2026
d941c01
Merge updated internal web view API base
cursoragent Jul 16, 2026
c9f7142
Merge updated internal web view API stack
cursoragent Jul 16, 2026
111d7ea
Merge updated internal web view API dependencies
cursoragent Jul 16, 2026
850fe90
Merge updated internal web view session stack
cursoragent Jul 16, 2026
cf8b833
Merge updated internal web view session dependencies
cursoragent Jul 16, 2026
ca80c8f
Merge branch 'main' into webview/1-public-api
ajpallares Jul 17, 2026
eba0f50
Merge remote-tracking branch 'origin/main' into webview/6-view
cursoragent Jul 17, 2026
bba700f
feat(paywalls): apply fit defaults to web views
cursoragent Jul 17, 2026
94982da
fix(paywalls): remove client-side web view payload limits
cursoragent Jul 17, 2026
3ac5561
Merge branch 'main' of https://github.com//RevenueCat/purchases-ios i…
JZDesign Jul 17, 2026
ea6d24b
test(paywalls): address web view envelope review
cursoragent Jul 17, 2026
0612595
refactor(paywalls): remove unused web view handler API
cursoragent Jul 17, 2026
d14d63c
fix(paywalls): retain internal bridge dependencies
cursoragent Jul 17, 2026
19cbfe6
refactor(paywalls): remove web view message handler concept
JZDesign Jul 17, 2026
c3d27a9
Merge branch 'webview/1-public-api' into webview/2-envelope
JZDesign Jul 17, 2026
e3ad994
Merge updated internal web view API stack (handler removal)
JZDesign Jul 17, 2026
4671130
Merge updated internal web view API dependencies (handler removal)
JZDesign Jul 17, 2026
226eb28
Merge updated internal web view session stack (handler removal)
JZDesign Jul 17, 2026
3278b60
Merge updated internal web view session dependencies (handler removal)
JZDesign Jul 17, 2026
eb53f30
Adapt web view bridge to trimmed envelope API
ajpallares Jul 20, 2026
66b3e33
Merge branch 'main' into webview/5-session
ajpallares Jul 21, 2026
f984e23
Merge branch 'main' into webview/5-session
ajpallares Jul 21, 2026
86d9c0b
refactor(paywalls): harden web_view session origin gating
ajpallares Jul 21, 2026
ab69065
Merge remote-tracking branch 'origin/webview/5-session' into webview/…
ajpallares Jul 21, 2026
a48f936
Merge branch 'main' into webview/5-session
ajpallares Jul 21, 2026
7940b96
refactor(paywalls): expose web_view origin as URL/WKSecurityOrigin ex…
ajpallares Jul 21, 2026
b266987
Merge webview/5-session into webview/6-view and reconcile the view layer
ajpallares Jul 21, 2026
7280b5d
refactor(paywalls): consolidate web_view imports under single platfor…
ajpallares Jul 21, 2026
0a866f8
refactor(paywalls): add copyright header to web_view files and fix im…
ajpallares Jul 21, 2026
8ceb7d4
Merge branch 'main' into webview/5-session
ajpallares Jul 21, 2026
9c12c2b
Merge branch 'webview/5-session' into webview/6-view
ajpallares Jul 21, 2026
b9078c4
fix(paywalls): adopt web_view origin extensions and add file headers
ajpallares Jul 21, 2026
07ecb4c
fix(paywalls): treat unresolvable web_view origin as inert bridge
ajpallares Jul 21, 2026
8948589
Merge webview/5-session into webview/6-view (inert-bridge origin fix)
ajpallares Jul 21, 2026
2eb1308
refactor(paywalls): tidy web_view session comments and narrow handleC…
ajpallares Jul 21, 2026
cd147f7
Merge branch 'webview/5-session' into webview/6-view
ajpallares Jul 21, 2026
53fde7e
test(paywalls): add missing web_view drop-branch coverage and harden …
ajpallares Jul 21, 2026
7761ba2
Merge branch 'webview/5-session' into webview/6-view
ajpallares Jul 21, 2026
07c6a31
refactor(paywalls): address web_view view review feedback
ajpallares Jul 21, 2026
d7d0c25
Merge branch 'webview/6-view' of https://github.com/RevenueCat/purcha…
ajpallares Jul 21, 2026
bc2ad31
Merge remote-tracking branch 'origin/main' into webview/6-view
ajpallares Jul 21, 2026
2f1d7ca
fix(paywalls): drop WebViewComponentTests from xcodeproj UnitTests ta…
ajpallares Jul 21, 2026
a32d717
refactor(paywalls): split web_view schema and view tests by target
ajpallares Jul 21, 2026
bc74f30
Merge branch 'main' into webview/6-view
ajpallares Jul 21, 2026
408192b
Fix main-actor isolation error in WebViewRepresentable on Xcode 15
ajpallares Jul 21, 2026
1c52e67
refactor(paywalls): tighten web_view platform typealias access
ajpallares Jul 22, 2026
fb5e589
refactor(paywalls): drop redundant macOS web_view data-store cleanup
ajpallares Jul 22, 2026
92d405c
Merge branch 'main' into webview/6-view
ajpallares Jul 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@
16DA8EC92E4EE24100283940 /* JsonLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16DA8EC82E4EE24100283940 /* JsonLoader.swift */; };
16DA8ECD2E4EE33700283940 /* ImageComponentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16DA8ECC2E4EE33700283940 /* ImageComponentTests.swift */; };
16DA8EDF2E4F6A6600283940 /* PaywallVideoComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16DA8EDE2E4F6A6600283940 /* PaywallVideoComponent.swift */; };
66EAEED2B259D54A9E789F4A /* PaywallWebViewComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14C1ACED1916C08B0E8E2776 /* PaywallWebViewComponent.swift */; };
16DA8EF42E4F7A2500283940 /* VideoComponentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16DA8EF32E4F7A2500283940 /* VideoComponentTests.swift */; };
FA4A2DF41FC434A9401F08CC /* PaywallWebViewAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D0C57ECDF0E7F1A5B4418FF /* PaywallWebViewAPI.swift */; };
11CC2EA83C56933E40D8E5D8 /* WebViewEnvelope.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89DD761C9E00E61032AAEB44 /* WebViewEnvelope.swift */; };
1401E951ACE24C7B84F94B7F /* WebViewIsolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 833251CB0C6FE0BA16C2AC41 /* WebViewIsolation.swift */; };
FC5AE63E8F81C3EC2FFDA408 /* WebViewOriginPolicy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28661C6CE3F64078A0138080 /* WebViewOriginPolicy.swift */; };
0F4E490A741F00F18C4985D8 /* WebViewSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72CC1CD0385BF18D1392EB43 /* WebViewSession.swift */; };
D969194D68D6967E81788B1F /* WebViewComponentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A2779D02050D28BF369658 /* WebViewComponentView.swift */; };
B53032B7D38B14FE9850F314 /* WebViewComponentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F6E5F9ABE99E8F2BC1DE237 /* WebViewComponentViewModel.swift */; };
16DA8F1E2E4FB6E200283940 /* ImageComponent.json in Resources */ = {isa = PBXBuildFile; fileRef = 16DA8F1B2E4FB6E200283940 /* ImageComponent.json */; };
16DA8F1F2E4FB6E200283940 /* VideoComponent.json in Resources */ = {isa = PBXBuildFile; fileRef = 16DA8F1C2E4FB6E200283940 /* VideoComponent.json */; };
16E146AD2E99F3480089B609 /* TransactionNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16E146AB2E99F1E20089B609 /* TransactionNotifications.swift */; };
Expand Down Expand Up @@ -1635,7 +1643,21 @@
16DA8EC82E4EE24100283940 /* JsonLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JsonLoader.swift; sourceTree = "<group>"; };
16DA8ECC2E4EE33700283940 /* ImageComponentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageComponentTests.swift; sourceTree = "<group>"; };
16DA8EDE2E4F6A6600283940 /* PaywallVideoComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaywallVideoComponent.swift; sourceTree = "<group>"; };
14C1ACED1916C08B0E8E2776 /* PaywallWebViewComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaywallWebViewComponent.swift; sourceTree = "<group>"; };
E7EB64E9D44783DD16A28B05 /* WebViewComponentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewComponentTests.swift; sourceTree = "<group>"; };
16DA8EF32E4F7A2500283940 /* VideoComponentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoComponentTests.swift; sourceTree = "<group>"; };
9D0C57ECDF0E7F1A5B4418FF /* PaywallWebViewAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaywallWebViewAPI.swift; sourceTree = "<group>"; };
89DD761C9E00E61032AAEB44 /* WebViewEnvelope.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewEnvelope.swift; sourceTree = "<group>"; };
C1B939C7DAAFEA1DE2DE2674 /* WebViewEnvelopeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewEnvelopeTests.swift; sourceTree = "<group>"; };
686562C08D953A27986DB276 /* PaywallWebViewValueTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaywallWebViewValueTests.swift; sourceTree = "<group>"; };
833251CB0C6FE0BA16C2AC41 /* WebViewIsolation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewIsolation.swift; sourceTree = "<group>"; };
28661C6CE3F64078A0138080 /* WebViewOriginPolicy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewOriginPolicy.swift; sourceTree = "<group>"; };
72CC1CD0385BF18D1392EB43 /* WebViewSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewSession.swift; sourceTree = "<group>"; };
D0A2779D02050D28BF369658 /* WebViewComponentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewComponentView.swift; sourceTree = "<group>"; };
6F6E5F9ABE99E8F2BC1DE237 /* WebViewComponentViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewComponentViewModel.swift; sourceTree = "<group>"; };
F75819E258F47D1DB058500D /* WebViewSessionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewSessionTests.swift; sourceTree = "<group>"; };
19B79CAD4C423F576E7DFBA9 /* WebViewIsolationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewIsolationTests.swift; sourceTree = "<group>"; };
9E7AD021B4580265DFDF6A02 /* WebViewNavigationPolicyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewNavigationPolicyTests.swift; sourceTree = "<group>"; };
16DA8F1B2E4FB6E200283940 /* ImageComponent.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = ImageComponent.json; sourceTree = "<group>"; };
16DA8F1C2E4FB6E200283940 /* VideoComponent.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = VideoComponent.json; sourceTree = "<group>"; };
16E146AB2E99F1E20089B609 /* TransactionNotifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionNotifications.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3165,6 +3187,12 @@
030890822D2B77DD0069677B /* PaywallsV2 */ = {
isa = PBXGroup;
children = (
686562C08D953A27986DB276 /* PaywallWebViewValueTests.swift */,
C1B939C7DAAFEA1DE2DE2674 /* WebViewEnvelopeTests.swift */,
19B79CAD4C423F576E7DFBA9 /* WebViewIsolationTests.swift */,
9E7AD021B4580265DFDF6A02 /* WebViewNavigationPolicyTests.swift */,
F75819E258F47D1DB058500D /* WebViewSessionTests.swift */,
E7EB64E9D44783DD16A28B05 /* WebViewComponentTests.swift */,
802593842FE07057005AF6DF /* PaywallStateStoreTests.swift */,
16A9F7B62FAA22D0008E8A4D /* VideoComponentViewTests.swift */,
A9D45E4EAFAD4C0385BEDFB3 /* VideoPlayerViewTests.swift */,
Expand Down Expand Up @@ -3297,6 +3325,20 @@
path = Video;
sourceTree = "<group>";
};
EB23E44596DD9714E81840CF /* WebView */ = {
isa = PBXGroup;
children = (
9D0C57ECDF0E7F1A5B4418FF /* PaywallWebViewAPI.swift */,
89DD761C9E00E61032AAEB44 /* WebViewEnvelope.swift */,
833251CB0C6FE0BA16C2AC41 /* WebViewIsolation.swift */,
28661C6CE3F64078A0138080 /* WebViewOriginPolicy.swift */,
72CC1CD0385BF18D1392EB43 /* WebViewSession.swift */,
D0A2779D02050D28BF369658 /* WebViewComponentView.swift */,
6F6E5F9ABE99E8F2BC1DE237 /* WebViewComponentViewModel.swift */,
);
path = WebView;
sourceTree = "<group>";
};
1699BD5C2EEFBEEC002001FB /* DefaultPaywall */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3387,6 +3429,7 @@
0387D4AD2EC58AA5008E4A6B /* Countdown */,
E1C0A00BBEEF0001CCDD0001 /* Header */,
164681CD2E6B577600854AA5 /* Video */,
EB23E44596DD9714E81840CF /* WebView */,
2C7457472CEA66AB004ACE52 /* ComponentsView.swift */,
7707A94A2CAD936A006E0313 /* Button */,
03C06FC82D479C6300600693 /* Carousel */,
Expand Down Expand Up @@ -5919,6 +5962,7 @@
isa = PBXGroup;
children = (
16DA8EDE2E4F6A6600283940 /* PaywallVideoComponent.swift */,
14C1ACED1916C08B0E8E2776 /* PaywallWebViewComponent.swift */,
16A7F4C02E563B89001F9FC8 /* Transitions */,
03C730F22D35F13F00297FEC /* PaywallV2CacheWarming.swift */,
2CC791612CC0493600FBE120 /* Common */,
Expand Down Expand Up @@ -7436,6 +7480,7 @@
4F8929192A65EF3000A91EA2 /* EnsureNonEmptyCollectionDecodable.swift in Sources */,
757E73CE2F0FE2410066DCDC /* LocalTransactionMetadata.swift in Sources */,
16DA8EDF2E4F6A6600283940 /* PaywallVideoComponent.swift in Sources */,
66EAEED2B259D54A9E789F4A /* PaywallWebViewComponent.swift in Sources */,
35F82BB626A9B8040051DF03 /* AttributionDataMigrator.swift in Sources */,
4F5F47082AA91F8A005649D8 /* HealthOperation.swift in Sources */,
4FEF41AB2B4F2F3400CD699F /* MapAppStoreDetector.swift in Sources */,
Expand Down Expand Up @@ -8233,6 +8278,13 @@
164681D02E6B577600854AA5 /* VideoComponentView.swift in Sources */,
164681D12E6B577600854AA5 /* VideoPlayerViewUIView.swift in Sources */,
164681D62E6B577600854AA5 /* VideoPlayerLayerUIView.swift in Sources */,
FA4A2DF41FC434A9401F08CC /* PaywallWebViewAPI.swift in Sources */,
11CC2EA83C56933E40D8E5D8 /* WebViewEnvelope.swift in Sources */,
1401E951ACE24C7B84F94B7F /* WebViewIsolation.swift in Sources */,
FC5AE63E8F81C3EC2FFDA408 /* WebViewOriginPolicy.swift in Sources */,
0F4E490A741F00F18C4985D8 /* WebViewSession.swift in Sources */,
D969194D68D6967E81788B1F /* WebViewComponentView.swift in Sources */,
B53032B7D38B14FE9850F314 /* WebViewComponentViewModel.swift in Sources */,
DBAA1FC22F8D4ED8000E8C81 /* HeaderNestedHeroZLayerSafeAreaPreview.swift in Sources */,
164681D22E6B577600854AA5 /* VideoComponentViewModel.swift in Sources */,
2C7457422CE81107004ACE52 /* IntroOfferEligibilityContext.swift in Sources */,
Expand Down
24 changes: 24 additions & 0 deletions RevenueCatUI/Data/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ enum Strings {
case video_failed_to_set_audio_session_category(Error)
case video_failed_to_cache(URL, Error)

// WebView
case paywall_web_view_content_rules_failed(String)
case paywall_web_view_message_rejected(reason: String)
case paywall_web_view_post_message_failed(String)
case paywall_web_view_post_message_skipped
case paywall_web_view_reserved_locale_stripped
case paywall_web_view_missing_id
case paywall_web_view_content_process_terminated

// Exit Offers
case errorFetchingOfferings(Error)
case exitOfferNotFound(String)
Expand Down Expand Up @@ -364,6 +373,21 @@ extension Strings: CustomStringConvertible {
case .video_failed_to_cache(let url, let error):
return "Failed to cache video at \(url): \(error)"

case .paywall_web_view_content_rules_failed(let error):
return "Paywalls V2 web_view content rules failed: \(error)"
case .paywall_web_view_message_rejected(let reason):
return "Paywalls V2 web_view message rejected: \(reason)"
case .paywall_web_view_post_message_failed(let error):
return "Paywalls V2 web_view postMessage failed: \(error)"
case .paywall_web_view_post_message_skipped:
return "Paywalls V2 web_view postMessage skipped because the channel is not ready or origin changed."
case .paywall_web_view_reserved_locale_stripped:
return "Paywalls V2 web_view variable 'locale' is SDK-managed and was stripped."
case .paywall_web_view_missing_id:
return "Paywalls V2 web_view component is missing an id. Rendering without native bridge."
case .paywall_web_view_content_process_terminated:
return "Paywalls V2 web_view content process terminated. Removing the web view."

case .errorFetchingOfferings(let error):
return "Error fetching offerings: \(error)"
case .exitOfferNotFound(let offeringId):
Expand Down
219 changes: 219 additions & 0 deletions RevenueCatUI/Templates/V2/Components/WebView/PaywallWebViewAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import Foundation
import SwiftUI
// swiftlint:disable missing_docs

#if !os(tvOS) // For Paywalls V2

/// A validated message sent from a Paywalls V2 `web_view` component to your app.
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public struct PaywallWebViewMessage: Sendable, Equatable {

/// The identifier of the `web_view` component that produced this message.
public let componentID: String

/// The message type, e.g. `"rc:step-complete"`.
public let type: String

/// The responses collected by the web flow. Only populated for `"rc:step-complete"` messages.
public let responses: [String: PaywallWebViewValue]?

/// The error reported by the web content. Only populated for `"rc:error"` messages.
public let error: String?

public init(
componentID: String,
type: String,
responses: [String: PaywallWebViewValue]? = nil,
error: String? = nil
) {
self.componentID = componentID
self.type = type
self.responses = responses
self.error = error
}

}

/// A JSON-compatible value exchanged between a Paywalls V2 `web_view` component and your app.
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public struct PaywallWebViewValue: Sendable, Equatable, Hashable, Codable {

private indirect enum Storage: Sendable, Equatable, Hashable {
case string(String)
case number(Double)
case bool(Bool)
case array([PaywallWebViewValue])
case object([String: PaywallWebViewValue])
case null
}

private let storage: Storage

private init(_ storage: Storage) {
self.storage = storage
}

/// Creates a string value.
public static func string(_ value: String) -> Self { Self(.string(value)) }

/// Creates a numeric value. Non-finite values normalize to ``null`` because JSON cannot encode them.
public static func number(_ value: Double) -> Self { value.isFinite ? Self(.number(value)) : .null }

/// Creates a boolean value.
public static func bool(_ value: Bool) -> Self { Self(.bool(value)) }

/// Creates an array value.
public static func array(_ value: [PaywallWebViewValue]) -> Self { Self(.array(value)) }

/// Creates an object (dictionary) value.
public static func object(_ value: [String: PaywallWebViewValue]) -> Self { Self(.object(value)) }

/// A null value.
public static var null: Self { Self(.null) }

public var stringValue: String? {
if case .string(let value) = self.storage { return value }
return nil
}

public var numberValue: Double? {
if case .number(let value) = self.storage { return value }
return nil
}

public var boolValue: Bool? {
if case .bool(let value) = self.storage { return value }
return nil
}

public var arrayValue: [PaywallWebViewValue]? {
if case .array(let value) = self.storage { return value }
return nil
}

public var objectValue: [String: PaywallWebViewValue]? {
if case .object(let value) = self.storage { return value }
return nil
}

public var isNull: Bool {
if case .null = self.storage { return true }
return false
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

if container.decodeNil() {
self = .null
} else if let value = try? container.decode(Bool.self) {
self = .bool(value)
} else if let value = try? container.decode(Double.self) {
self = .number(value)
} else if let value = try? container.decode(String.self) {
self = .string(value)
} else if let value = try? container.decode([PaywallWebViewValue].self) {
self = .array(value)
} else {
self = .object(try container.decode([String: PaywallWebViewValue].self))
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

switch self.storage {
case .string(let value):
try container.encode(value)
case .number(let value):
try container.encode(value)
case .bool(let value):
try container.encode(value)
case .array(let value):
try container.encode(value)
case .object(let value):
try container.encode(value)
case .null:
try container.encodeNil()
}
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
@MainActor
public struct PaywallWebViewController {

#if canImport(WebKit)
weak var session: WebViewSession?
#endif

init() {}

#if canImport(WebKit)
init(session: WebViewSession?) {
self.session = session
}
#endif

public func postVariables(componentID: String, variables: [String: PaywallWebViewValue]) {
#if canImport(WebKit)
self.session?.postVariables(componentID: componentID, variables: variables)
#endif
}

public func postMessage(componentID: String, type: String, variables: [String: PaywallWebViewValue]) {
#if canImport(WebKit)
self.session?.post(componentID: componentID, type: type, variables: variables)
#endif
}

}

/// A wrapper for the Paywalls V2 `web_view` message handler.
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
public struct PaywallWebViewMessageAction {

private let action: @MainActor (PaywallWebViewMessage, PaywallWebViewController) -> Void

public init(
_ action: @escaping @MainActor (PaywallWebViewMessage, PaywallWebViewController) -> Void
) {
self.action = action
}

@MainActor
public func callAsFunction(_ message: PaywallWebViewMessage, _ controller: PaywallWebViewController) {
self.action(message, controller)
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
struct PaywallWebViewMessageActionKey: EnvironmentKey {
static let defaultValue: PaywallWebViewMessageAction? = nil
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension EnvironmentValues {

var paywallWebViewMessageAction: PaywallWebViewMessageAction? {
get { self[PaywallWebViewMessageActionKey.self] }
set { self[PaywallWebViewMessageActionKey.self] = newValue }
}

}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
extension View {

/// Invokes the given closure when a Paywalls V2 `web_view` component sends a message.
public func onPaywallWebViewMessage(
_ action: @escaping @MainActor (PaywallWebViewMessage, PaywallWebViewController) -> Void
) -> some View {
self.environment(\.paywallWebViewMessageAction, PaywallWebViewMessageAction(action))
}

}

#endif
Loading