Skip to content

Commit

Permalink
Added new promptToUpdate method
Browse files Browse the repository at this point in the history
  • Loading branch information
rwbutler committed Oct 10, 2021
1 parent deef679 commit ce6690d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.6.0] - 2021-10-10
### Added
- Added another `promptToUpdate` function which does not require an `UpdatesResult` object which means that the function can be used without having to pass an `UpdatesResult` object around the calling app.
- `UpdatesResult` now has the App Store URL for the calling app if it is possible to form it from the required parameters.

## [1.5.0] - 2021-10-05
### Added
- Added properties `minOptionalAppVersion` and `minRequiredAppVersion` with the latter taking precedence if both are set to a version string. If the former is set then the update type value will be `.soft` i.e. a soft update whereas if the latter is set then the update type will be `.hard` indicating that a different UI should be displayed to the user.
Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Updates.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Updates/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 1.5.0;
MARKETING_VERSION = 1.6.0;
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.updates;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -509,7 +509,7 @@
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Updates/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 1.5.0;
MARKETING_VERSION = 1.6.0;
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = com.rwbutler.updates;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
4 changes: 2 additions & 2 deletions Updates/Classes/Core/Updates+Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ extension Updates {
/// - appStoreId: The app store identifier specified as a String.
/// - Returns: The URL required to launch the App Store page for the specified app,
/// provided a valid identifier is provided.
static func appStoreURL(appStoreId: String, countryCode: String? = nil, productName: String) -> URL? {
guard let countryCode = countryCode ?? Updates.countryCode else {
static func appStoreURL(appStoreId: String, countryCode: String? = nil, productName: String? = nil) -> URL? {
guard let countryCode = countryCode ?? Updates.countryCode, let productName = productName else {
return nil
}
let lowercasedCountryCode = countryCode.lowercased()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum UpdateType: String, Codable {

public struct Update {
public let appStoreId: String?
public let appStoreURL: URL?
public let isUpdated: AppUpdatedResult // whether or not this launch is a new install or update.
public let newVersionString: String
public let releaseNotes: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ struct UpdatesResultFactory: Factory {
comparator: configuration.comparator,
minRequiredOSVersion: minRequiredOSVersion
)
let appStoreURL = configuration.appStoreId.flatMap { appStoreId in
Updates.appStoreURL(
appStoreId: appStoreId,
countryCode: Updates.countryCode,
productName: Updates.productName
)
}
let update = Update(
appStoreId: configuration.appStoreId,
appStoreURL: appStoreURL,
isUpdated: isAppUpdated,
newVersionString: appStoreVersion,
releaseNotes: configuration.releaseNotes,
Expand Down Expand Up @@ -129,8 +137,16 @@ private extension UpdatesResultFactory {
return nil
}
let isAppUpdated = self.isAppUpdated(bundleVersion: bundleVersion, configuration: configuration)
let appStoreURL = configuration.appStoreId.flatMap { appStoreId in
Updates.appStoreURL(
appStoreId: appStoreId,
countryCode: Updates.countryCode,
productName: Updates.productName
)
}
let update = Update(
appStoreId: configuration.appStoreId,
appStoreURL: appStoreURL,
isUpdated: isAppUpdated,
newVersionString: minAppVersion,
releaseNotes: configuration.releaseNotes,
Expand Down
49 changes: 49 additions & 0 deletions Updates/Classes/UI/UpdatesUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,55 @@ public class UpdatesUI: NSObject {
presentingViewController.present(alert, animated: animated, completion: nil)
}

/// Prompt the user to update to the latest version
public static func promptToUpdate(
newVersionString: String?,
appStoreId: String?,
animated: Bool = true,
completion: (() -> Void)? = nil,
presentingViewController: UIViewController,
title: String? = nil,
message: String,
updateType: UpdateType
) {
let alertTitle: String
if let title = title {
alertTitle = title
} else if let productName = Updates.productName, let newVersionString = newVersionString {
alertTitle = "\(productName) v\(newVersionString) Available"
} else if let newVersionString = newVersionString {
alertTitle = "App Version \(newVersionString) Available"
} else {
alertTitle = "App Update Available"
}
let alert = UIAlertController(title: alertTitle, message: message, preferredStyle: .alert)
let updateButtonTitle = buttonTitle("Update")
let updateAction = UIAlertAction(title: updateButtonTitle, style: .default) { _ in
if updateType != .hard {
alert.dismiss(animated: animated, completion: completion)
}
guard let appStoreId = appStoreId else {
return
}
self.presentAppStore(
animated: animated,
appStoreId: appStoreId,
appStoreURL: Updates.appStoreURL(for: appStoreId),
completion: completion,
presentingViewController: presentingViewController
)
}
let cancelButtonTitle = buttonTitle("Cancel")
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .cancel) { _ in
alert.dismiss(animated: animated, completion: completion)
}
alert.addAction(updateAction)
if updateType != .hard {
alert.addAction(cancelAction)
}
presentingViewController.present(alert, animated: animated, completion: nil)
}

/// Presents SKStoreProductViewController modally.
/// - Parameters:
/// - animated: Whether or not the modal presentation is animated.
Expand Down

0 comments on commit ce6690d

Please sign in to comment.