Swift Requires Chilkat v11.0.0+
Swift
Schwab: List authorized accounts and select one
See more Schwab Examples
Lists masked account numbers. Run once to see their endings, then enter the last four digits of the intended account. A unique match saves Schwab’s hashValue in a separate secret for the account-specific examples. If two accounts share an ending, use the full-account input from your account picker. A failed match leaves the previous selection in place.
Use the access token saved by desktop authorization. Use the same Chilkat Secrets store as the OAuth example.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false
let vault = CkoSecrets()!
vault.location = "local_manager"
vault.autoSplit = true
let entry = CkoJsonObject()!
entry.updateString(jsonPath: "appName", value: "SchwabTrader")
entry.updateString(jsonPath: "service", value: "OAuth2")
entry.updateString(jsonPath: "username", value: "personal-desktop")
let tokens = CkoJsonObject()!
success = vault.getSecretJson(jsonId: entry, jsonOut: tokens)
if success != true {
print("Could not read the token record. Check the store and complete desktop authorization first.")
return
}
var accessToken: String? = tokens.string(of: "access_token")
if accessToken == "" {
print("Run the Schwab authorization or refresh example to save an access token.")
return
}
let http = CkoHttp()!
http.requireSslCertVerify = true
http.authToken = accessToken
// Supply one of these from your account picker. Leave both empty to list only.
var selectedEnding: String? = ""
var selectedFullNumber: String? = ""
let resp = CkoHttpResponse()!
success = http.httpNoBody(verb: "GET", url: "https://api.schwabapi.com/trader/v1/accounts/accountNumbers", response: resp)
if (success != true) || (resp.statusCode.intValue != 200) {
if resp.statusCode.intValue == 0 {
print("No HTTP response. Check connectivity.")
}
else {
print("Schwab returned HTTP \(resp.statusCode.intValue)")
if resp.statusCode.intValue == 401 {
print("Refresh the access token or repeat desktop authorization.")
}
}
return
}
let accounts = CkoJsonArray()!
success = accounts.load(jsonArray: resp.bodyStr)
if success != true {
print("Schwab returned unexpected JSON.")
return
}
let row = CkoJsonObject()!
let number = CkoStringBuilder()!
var matches: Int = 0
var i: Int = 0
var selectedHash: String? = ""
while (i < accounts.size.intValue) {
accounts.objectAt2(index: i, jsonObj: row)
var accountNumber: String? = row.string(of: "accountNumber")
number.setString(value: accountNumber)
if number.length.intValue >= 4 {
var start: Int = number.length.intValue - 4
var ending: String? = number.getRange(startIndex: start, numChars: 4, removeFlag: false)
print("Account ending in \(ending!)")
var matched: Bool = false
if selectedFullNumber != "" {
if accountNumber == selectedFullNumber {
matched = true
}
}
else {
if ending == selectedEnding {
matched = true
}
}
if matched == true {
matches = matches + 1
selectedHash = row.string(of: "hashValue")
}
}
i = i + 1
}
if (matches != 1) || (selectedHash == "") {
print("Select exactly one account. For duplicate endings use the full-account input.")
return
}
entry.updateString(jsonPath: "service", value: "SelectedAccount")
let selection = CkoJsonObject()!
selection.updateString(jsonPath: "hashValue", value: selectedHash)
success = vault.updateSecretJson(jsonId: entry, secret: selection)
if success != true {
print("Could not save the selected account.")
return
}
print("Selected account saved.")
}