2015-08-24 18:44:47 +00:00
|
|
|
//
|
|
|
|
// ISStoreAccount.swift
|
|
|
|
// mas-cli
|
|
|
|
//
|
|
|
|
// Created by Andrew Naylor on 22/08/2015.
|
|
|
|
// Copyright (c) 2015 Andrew Naylor. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
extension ISStoreAccount {
|
|
|
|
static var primaryAccountIsPresentAndSignedIn: Bool {
|
2016-09-17 12:58:38 +00:00
|
|
|
return CKAccountStore.shared().primaryAccountIsPresentAndSignedIn
|
2015-08-24 18:44:47 +00:00
|
|
|
}
|
2018-04-15 03:03:19 +00:00
|
|
|
|
2015-08-24 18:44:47 +00:00
|
|
|
static var primaryAccount: ISStoreAccount? {
|
2016-09-17 12:58:38 +00:00
|
|
|
return CKAccountStore.shared().primaryAccount
|
2015-08-24 18:44:47 +00:00
|
|
|
}
|
2018-04-15 03:03:19 +00:00
|
|
|
|
2016-09-17 16:30:58 +00:00
|
|
|
static func signIn(username: String? = nil, password: String? = nil, systemDialog: Bool = false) throws -> ISStoreAccount {
|
2018-04-15 03:03:19 +00:00
|
|
|
guard let username = username, let password = password else { fatalError() }
|
|
|
|
|
2016-02-14 01:22:33 +00:00
|
|
|
var account: ISStoreAccount? = nil
|
2018-08-11 02:31:25 +00:00
|
|
|
let error: MASError? = nil
|
2018-04-15 03:03:19 +00:00
|
|
|
|
2018-04-06 03:29:15 +00:00
|
|
|
let accountService: ISAccountService = ISServiceProxy.genericShared().accountService
|
2016-02-14 01:22:33 +00:00
|
|
|
let client = ISStoreClient(storeClientType: 0)
|
|
|
|
accountService.setStoreClient(client)
|
|
|
|
|
2018-02-21 16:18:47 +00:00
|
|
|
// let context = ISAuthenticationContext(accountID: 0)!
|
|
|
|
// context.appleIDOverride = username
|
|
|
|
//
|
|
|
|
// if systemDialog {
|
|
|
|
// context.appleIDOverride = username
|
|
|
|
// } else {
|
|
|
|
// context.demoMode = true
|
|
|
|
// context.demoAccountName = username
|
|
|
|
// context.demoAccountPassword = password
|
|
|
|
// context.demoAutologinMode = true
|
|
|
|
// }
|
|
|
|
|
2016-09-17 12:58:38 +00:00
|
|
|
let group = DispatchGroup()
|
|
|
|
group.enter()
|
2018-02-21 16:18:47 +00:00
|
|
|
|
|
|
|
accountService.account(withAppleID: username) { (storeAccount: ISStoreAccount?) in
|
|
|
|
if let _account = storeAccount {
|
2018-04-15 03:03:19 +00:00
|
|
|
debugPrint("ISStoreAccount: \(String(describing: _account))")
|
2018-02-21 16:18:47 +00:00
|
|
|
_account.password = password
|
2018-04-15 03:03:19 +00:00
|
|
|
accountService.add(_account)
|
|
|
|
// let response = ISAuthenticationResponse()
|
|
|
|
// response.accountIdentifier = username
|
|
|
|
// accountService.addAccount(with: response, makePrimary: true) { (storeAccount: ISStoreAccount?) in
|
|
|
|
// if let _account = storeAccount {
|
|
|
|
// account = _account
|
|
|
|
// }
|
|
|
|
// }
|
2016-02-14 01:22:33 +00:00
|
|
|
account = _account
|
2018-04-15 03:03:19 +00:00
|
|
|
|
|
|
|
let accountStore = CKAccountStore.shared()
|
|
|
|
accountStore.addAccount(_account)
|
|
|
|
accountStore.signIn()
|
|
|
|
debugPrint("ISStoreAccount: \(String(describing: _account))")
|
2016-02-14 01:22:33 +00:00
|
|
|
} else {
|
2018-02-21 16:18:47 +00:00
|
|
|
// TODO: Handle failed AppleID lookup
|
2018-04-15 03:03:19 +00:00
|
|
|
print("No account found for username: \(username)")
|
2016-02-14 01:22:33 +00:00
|
|
|
}
|
2016-09-17 12:58:38 +00:00
|
|
|
group.leave()
|
2016-02-14 01:22:33 +00:00
|
|
|
}
|
2016-09-17 16:30:58 +00:00
|
|
|
|
2018-02-21 16:18:47 +00:00
|
|
|
// accountService.signIn(with: context) { success, _account, _error in
|
|
|
|
// if success {
|
|
|
|
// account = _account
|
|
|
|
// } else {
|
|
|
|
// error = .signInFailed(error: _error as NSError?)
|
|
|
|
// }
|
|
|
|
// group.leave()
|
|
|
|
// }
|
|
|
|
|
2016-09-17 16:30:58 +00:00
|
|
|
if systemDialog {
|
|
|
|
group.wait()
|
|
|
|
} else {
|
2016-10-21 21:59:33 +00:00
|
|
|
let _ = group.wait(timeout: .now() + 30)
|
2016-09-17 16:30:58 +00:00
|
|
|
}
|
2016-02-14 01:22:33 +00:00
|
|
|
|
|
|
|
if let account = account {
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
2016-09-25 21:13:23 +00:00
|
|
|
throw error ?? MASError.signInFailed(error: nil)
|
2016-02-14 01:22:33 +00:00
|
|
|
}
|
2015-08-24 18:44:47 +00:00
|
|
|
}
|