-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from klaviyo/ab/CHNL-16220/present-webview-fr…
…om-host-app [CHNL-16220] present webview from host app
- Loading branch information
Showing
9 changed files
with
196 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// PreloadError.swift | ||
// klaviyo-swift-sdk | ||
// | ||
// Created by Andrew Balmer on 1/21/25. | ||
// | ||
|
||
enum PreloadError: Error { | ||
case timeout | ||
case navigationFailed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// KlaviyoWebViewOverlayManager.swift | ||
// klaviyo-swift-sdk | ||
// | ||
// Created by Andrew Balmer on 1/15/25. | ||
// | ||
|
||
import SwiftUI | ||
import UIKit | ||
|
||
@_spi(KlaviyoPrivate) | ||
public class KlaviyoWebViewOverlayManager { | ||
public static let shared = KlaviyoWebViewOverlayManager() | ||
private var isLoading: Bool = false | ||
|
||
/// Presents a view controller on the top-most view controller | ||
/// - Parameters: | ||
/// - viewController: A `UIViewController` instance to present. | ||
/// - modalPresentationStyle: The modal presentation style to use (default is `.overCurrentContext`). | ||
/// | ||
/// - warning: For internal use only. The host app should not manually call this method, as | ||
/// the logic for fetching and displaying forms will be handled internally within the SDK. | ||
@_spi(KlaviyoPrivate) | ||
@MainActor public func preloadAndShow( | ||
viewModel: KlaviyoWebViewModeling, | ||
modalPresentationStyle: UIModalPresentationStyle = .overCurrentContext) { | ||
guard !isLoading else { | ||
return | ||
} | ||
|
||
isLoading = true | ||
|
||
let viewController = KlaviyoWebViewController(viewModel: viewModel) | ||
viewController.modalPresentationStyle = modalPresentationStyle | ||
|
||
Task { | ||
defer { isLoading = false } | ||
|
||
try await viewModel.preloadWebsite(timeout: 8_000_000_000) | ||
|
||
guard let topController = UIApplication.shared.topMostViewController else { | ||
return | ||
} | ||
topController.present(viewController, animated: true, completion: nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// UIApplication+Ext.swift | ||
// klaviyo-swift-sdk | ||
// | ||
// Created by Andrew Balmer on 12/4/24. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
extension UIApplication { | ||
var topMostViewController: UIViewController? { | ||
guard let keyWindow = getKeyWindow() else { return nil } | ||
var topController = keyWindow.rootViewController | ||
while let presentedController = topController?.presentedViewController { | ||
if let navigationController = presentedController as? UINavigationController { | ||
topController = navigationController.visibleViewController | ||
} else if let tabBarController = presentedController as? UITabBarController { | ||
topController = tabBarController.selectedViewController | ||
} else { | ||
topController = presentedController | ||
} | ||
} | ||
return topController | ||
} | ||
|
||
private func getKeyWindow() -> UIWindow? { | ||
connectedScenes | ||
.filter { $0 is UIWindowScene } | ||
.compactMap { $0 as? UIWindowScene } | ||
.flatMap(\.windows) | ||
.first(where: { $0.isKeyWindow }) | ||
} | ||
} |
Oops, something went wrong.