Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lookup API to return data based on the country #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,42 @@ extension PrinceOfVersions {

If parameter `notificationFrequency` is set to `.always` and latest version of the app is bigger than installed version, method will always return `.newUpdateAvailable`. However, if the`notificationFrequency` is set to `.once`, only first time this method is called for the same latest app version, it will return `.newUpdateAvailable`, each subsequent call, it will return `.noUpdateAvailable`.

If the optional `country` parameter is provided, the API request will target the specified App Store region (e.g., `"mk"` for Macedonia). If `country` is not provided, the API will fallback to its default behavior, typically fetching data from the U.S. App Store.

- Parameters:
* bundle: Bundle where .plist file is stored in which app identifier and app versions should be checked.
* trackPhaseRelease: Boolean that indicates whether PoV should notify about new version after 7 days when app is fully rolled out or immediately. Default value is `YES`.
* callbackQueue: The queue on which the completion handler is dispatched. By default, `main` queue is used.
* notificationFrequency: Determines update status appearance frequency.
* country: Optional parameter to specify the App Store region to target (e.g., `"mk"` for Macedonia). Defaults to `nil`.
* completion: The completion handler to call when the load request is complete. It returns result that contains UpdatInfo data or PoVError error

- Returns:
* Discardable `URLSessionDataTask`
*/
@available(swift, obsoleted: 1.0)
@objc(checkForUpdateFromAppStoreWithTrackPhasedRelease:callbackQueue:bundle:notificationFrequency:completion:error:)
@objc(checkForUpdateFromAppStoreWithTrackPhasedRelease:callbackQueue:bundle:notificationFrequency:country:completion:error:)
@discardableResult
public static func checkForUpdateFromAppStore(
trackPhaseRelease: Bool,
callbackQueue: DispatchQueue,
bundle: Bundle,
notificationFrequency: UpdateNotificationType,
country: String?,
completion: @escaping AppStoreObjectCompletionBlock,
error: @escaping ObjectErrorBlock
) -> URLSessionDataTask? {

let frequency: NotificationType = notificationFrequency == .always ? .always : .once

return internalyCheckAndPrepareForUpdateAppStore(bundle: bundle, trackPhaseRelease: trackPhaseRelease, callbackQueue: callbackQueue, notificationFrequency: frequency, completion: completion, error: error)
return internalyCheckAndPrepareForUpdateAppStore(
bundle: bundle,
trackPhaseRelease: trackPhaseRelease,
callbackQueue: callbackQueue,
notificationFrequency: frequency,
country: country,
completion: completion,
error: error
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,24 @@ internal extension PrinceOfVersions {
trackPhaseRelease: Bool,
callbackQueue: DispatchQueue,
notificationFrequency: NotificationType = .always,
country: String? = nil,
completion: @escaping AppStoreObjectCompletionBlock,
error: @escaping ObjectErrorBlock
) -> URLSessionDataTask? {
return PrinceOfVersions.checkForUpdateFromAppStore(trackPhaseRelease: trackPhaseRelease, bundle: bundle, callbackQueue: callbackQueue, notificationFrequency: notificationFrequency, completion: { result in
return PrinceOfVersions.checkForUpdateFromAppStore(
trackPhaseRelease: trackPhaseRelease,
bundle: bundle,
callbackQueue: callbackQueue,
notificationFrequency: notificationFrequency,
country: country,
completion: {
result in
switch result {
case .success(let appStoreInfo):
completion(__ObjCAppStoreResult(from: appStoreInfo))
case .failure(let (errorResponse as NSError)):
error(errorResponse)
}
})
})
}
}
28 changes: 22 additions & 6 deletions Sources/PrinceOfVersions/PrinceOfVersions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ public extension PrinceOfVersions {

If parameter `notificationFrequency` is set to `.always` and latest version of the app is bigger than installed version, method will always return `.newUpdateAvailable`. However, if the`notificationFrequency` is set to `.once`, only first time this method is called for the same latest app version, it will return `.newUpdateAvailable`, each subsequent call, it will return `.noUpdateAvailable`.

If the optional `country` parameter is provided, the API request will target the specified App Store region (e.g., `"mk"` for Macedonia). If `country` is not provided, the API will fallback to its default behavior, typically fetching data from the U.S. App Store.

- parameter trackPhaseRelease: Boolean that indicates whether PoV should notify about new version after 7 days when app is fully rolled out or immediately. Default value is `true`.
- parameter bundle: Bundle where .plist file is stored in which app identifier and app versions should be checked.
- parameter callbackQueue: The queue on which the completion handler is dispatched. By default, `main` queue is used.
- parameter notificationFrequency: Determines update status appearance frequency.
- parameter cachePolicy: Determines which URLRequest cache policy should be used. Defaults to `.reloadIgnoringLocalCacheData` which will call the API every time.
- parameter country: Optional parameter to specify the App Store region to target (e.g., `"mk"` for Macedonia). Defaults to `nil`.
- parameter completion: The completion handler to call when the load request is complete. It returns result that contains UpdatInfo data or PoVError error.

- returns: Discardable `URLSessionDataTask`
Expand All @@ -127,17 +130,30 @@ public extension PrinceOfVersions {
callbackQueue: DispatchQueue = .main,
notificationFrequency: NotificationType = .always,
cachePolicy: NSURLRequest.CachePolicy = .reloadIgnoringLocalCacheData,
country: String? = nil,
completion: @escaping AppStoreCompletionBlock
) -> URLSessionDataTask? {

guard
let bundleIdentifier = bundle.bundleIdentifier,
let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(bundleIdentifier)")
let bundleIdentifier = bundle.bundleIdentifier,
var url = URL(string: "https://itunes.apple.com/lookup")
else {
callbackQueue.async {
completion(Result.failure(PoVError.invalidJsonData))
callbackQueue.async {
completion(Result.failure(PoVError.invalidJsonData))
}
return nil
}

let queryItems: [URLQueryItem] = [
URLQueryItem(name: "bundleId", value: bundleIdentifier),
URLQueryItem(name: "country", value: country)
].filter { $0.value != nil }

if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) {
urlComponents.queryItems = queryItems
if let updatedURL = urlComponents.url {
url = updatedURL
}
return nil
}

return internalyGetDataFromAppStore(
Expand Down