2015-08-21 13:02:36 +00:00
|
|
|
//
|
|
|
|
// Downloader.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Andrew Naylor on 21/08/2015.
|
|
|
|
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-10-14 19:41:19 +00:00
|
|
|
import CommerceKit
|
|
|
|
import StoreFoundation
|
2015-08-21 13:02:36 +00:00
|
|
|
|
2019-01-12 01:06:02 +00:00
|
|
|
/// Monitors app download progress.
|
|
|
|
///
|
|
|
|
/// - Parameter adamId: An app ID?
|
2020-05-15 03:24:27 +00:00
|
|
|
/// - Parameter purchase: Flag indicating whether the app needs to be purchased.
|
|
|
|
/// Only works for free apps. Defaults to false.
|
2019-01-12 01:06:02 +00:00
|
|
|
/// - Returns: An error, if one occurred.
|
2020-05-15 03:24:27 +00:00
|
|
|
func download(_ adamId: UInt64, purchase: Bool = false) -> MASError? {
|
2016-02-14 01:24:30 +00:00
|
|
|
guard let account = ISStoreAccount.primaryAccount else {
|
2016-09-25 21:13:23 +00:00
|
|
|
return .notSignedIn
|
2016-02-14 01:24:30 +00:00
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
|
|
|
guard let storeAccount = account as? ISStoreAccount
|
2021-03-22 05:25:18 +00:00
|
|
|
else { fatalError("Unable to cast StoreAccount to ISStoreAccount") }
|
2020-05-15 03:24:27 +00:00
|
|
|
let purchase = SSPurchase(adamId: adamId, account: storeAccount, purchase: purchase)
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2016-02-14 01:24:30 +00:00
|
|
|
var purchaseError: MASError?
|
2019-01-12 01:06:02 +00:00
|
|
|
var observerIdentifier: CKDownloadQueueObserver?
|
|
|
|
|
|
|
|
let group = DispatchGroup()
|
2016-09-17 12:58:38 +00:00
|
|
|
group.enter()
|
|
|
|
purchase.perform { purchase, _, error, response in
|
2016-02-14 01:24:30 +00:00
|
|
|
if let error = error {
|
2016-09-25 21:13:23 +00:00
|
|
|
purchaseError = .purchaseFailed(error: error as NSError?)
|
2016-09-17 12:58:38 +00:00
|
|
|
group.leave()
|
2016-02-14 01:24:30 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
if let downloads = response?.downloads, downloads.count > 0, let purchase = purchase {
|
2016-02-14 01:24:30 +00:00
|
|
|
let observer = PurchaseDownloadObserver(purchase: purchase)
|
2016-09-06 13:37:12 +00:00
|
|
|
|
2016-02-14 01:24:30 +00:00
|
|
|
observer.errorHandler = { error in
|
|
|
|
purchaseError = error
|
2016-09-17 12:58:38 +00:00
|
|
|
group.leave()
|
2015-09-03 23:13:47 +00:00
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
2016-02-14 01:24:30 +00:00
|
|
|
observer.completionHandler = {
|
2016-09-17 12:58:38 +00:00
|
|
|
group.leave()
|
2015-08-24 18:45:54 +00:00
|
|
|
}
|
2016-09-06 13:37:12 +00:00
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
let downloadQueue = CKDownloadQueue.shared()
|
2018-04-06 03:29:15 +00:00
|
|
|
observerIdentifier = downloadQueue.add(observer)
|
2019-01-12 01:06:02 +00:00
|
|
|
} else {
|
2016-02-14 01:24:30 +00:00
|
|
|
print("No downloads")
|
2016-09-25 21:13:23 +00:00
|
|
|
purchaseError = .noDownloads
|
2016-09-17 12:58:38 +00:00
|
|
|
group.leave()
|
2015-08-24 18:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 01:06:02 +00:00
|
|
|
|
|
|
|
_ = group.wait(timeout: .distantFuture)
|
|
|
|
|
2016-09-06 13:37:12 +00:00
|
|
|
if let observerIdentifier = observerIdentifier {
|
2018-04-06 03:29:15 +00:00
|
|
|
CKDownloadQueue.shared().remove(observerIdentifier)
|
2016-09-06 13:37:12 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 01:24:30 +00:00
|
|
|
return purchaseError
|
2015-08-24 18:45:54 +00:00
|
|
|
}
|