Skip to content

Commit

Permalink
Jetpack Focus: Show Jetpack app installation within the app using Sto…
Browse files Browse the repository at this point in the history
…reKit (#21896)
  • Loading branch information
staskus authored Nov 22, 2023
2 parents 6dd5cdc + cd83651 commit e7a35f1
Showing 1 changed file with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import StoreKit

class JetpackRedirector {

Expand Down Expand Up @@ -30,8 +31,63 @@ class JetpackRedirector {

// First, check if the WordPress app can open Jetpack by testing its URL scheme.
// if we can potentially open Jetpack app, let's open it through universal link to avoid scheme conflicts (e.g., a certain game :-).
// finally, if the user might not have Jetpack installed, direct them to App Store page.
let urlToOpen = UIApplication.shared.canOpenURL(jetpackDeepLinkURL) ? jetpackUniversalLinkURL : jetpackAppStoreURL
UIApplication.shared.open(urlToOpen)
// finally, if the user might not have Jetpack installed, open App Store view controller through StoreKit.
if UIApplication.shared.canOpenURL(jetpackDeepLinkURL) {
UIApplication.shared.open(jetpackUniversalLinkURL)
} else {
showJetpackAppInstallation(fallbackURL: jetpackAppStoreURL)
}
}

private static func showJetpackAppInstallation(fallbackURL: URL) {
let viewController = RootViewCoordinator.sharedPresenter.rootViewController.topmostPresentedViewController
let storeProductVC = SKStoreProductViewController()
let appID = [SKStoreProductParameterITunesItemIdentifier: "1565481562"]

configureNavigationBarAppearance(storeProductVC)

storeProductVC.loadProduct(withParameters: appID) { (result, error) in
if result {
viewController.present(storeProductVC, animated: true)
} else if let error = error {
DDLogError("Failed loading Jetpack App product: \(error.localizedDescription)")
UIApplication.shared.open(fallbackURL)
}
}
}

// MARK: - SKStoreProductViewController navigation bar appearance

/// Sets SKStoreProductViewController navigation bar translucent
///
/// Application's global navigation appearance settings interferes with SKStoreProductViewController
/// which requires for this temporary workaround
private static func configureNavigationBarAppearance(_ controller: SKStoreProductViewController) {
let previousisTranslucentValue = UINavigationBar.appearance().isTranslucent
UINavigationBar.appearance().isTranslucent = true

/// Reset to default translucent value
storeProductViewControllerObserver = StoreProductViewControllerObserver(onDismiss: {
UINavigationBar.appearance().isTranslucent = previousisTranslucentValue
storeProductViewControllerObserver = nil
})

controller.delegate = storeProductViewControllerObserver
}

/// Observe product view controller dismissal
class StoreProductViewControllerObserver: NSObject, SKStoreProductViewControllerDelegate {
private let onDismiss: () -> ()

init(onDismiss: @escaping () -> ()) {
self.onDismiss = onDismiss
super.init()
}

func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
onDismiss()
}
}

private static var storeProductViewControllerObserver: StoreProductViewControllerObserver?
}

0 comments on commit e7a35f1

Please sign in to comment.