mas/mas-cli/AppStore/Downloader.swift

52 lines
1.5 KiB
Swift
Raw Normal View History

//
// Downloader.swift
// mas-cli
//
// Created by Andrew Naylor on 21/08/2015.
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
//
2015-08-24 18:45:54 +00:00
func download(adamId: UInt64) -> MASError? {
2016-02-14 01:24:30 +00:00
guard let account = ISStoreAccount.primaryAccount else {
return MASError(code: .NotSignedIn)
}
let group = dispatch_group_create()
let purchase = SSPurchase(adamId: adamId, account: account)
var purchaseError: MASError?
purchase.perform { purchase, unused, error, response in
if let error = error {
purchaseError = MASError(code: .PurchaseError, sourceError: error)
dispatch_group_leave(group)
return
}
2015-08-24 18:45:54 +00:00
2016-02-14 01:24:30 +00:00
if let downloads = response.downloads where downloads.count > 0 {
let observer = PurchaseDownloadObserver(purchase: purchase)
observer.errorHandler = { error in
purchaseError = error
dispatch_group_leave(group)
}
2016-02-14 01:24:30 +00:00
observer.completionHandler = {
2015-08-24 18:45:54 +00:00
dispatch_group_leave(group)
}
2016-02-14 01:24:30 +00:00
CKDownloadQueue.sharedDownloadQueue().addObserver(observer)
}
else {
print("No downloads")
purchaseError = MASError(code: .NoDownloads)
dispatch_group_leave(group)
2015-08-24 18:45:54 +00:00
}
}
2016-02-14 01:24:30 +00:00
dispatch_group_enter(group)
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
return purchaseError
2015-08-24 18:45:54 +00:00
}