Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Commit

Permalink
add hasAccount api
Browse files Browse the repository at this point in the history
  • Loading branch information
Elaz committed Sep 20, 2018
1 parent 8112e88 commit ff63b97
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions KinEcosystem/Core/Kin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,36 @@ public class Kin {

}

public func hasAccount(peer: String, handler: @escaping (Bool?, Error?) -> ()) {
guard let core = core else {
logError("Kin not started")
DispatchQueue.main.async {
handler(nil, KinEcosystemError.client(.notStarted, nil))
}
return
}
_ = core.network.dataAtPath("users/exists",
method: .get,
contentType: .json,
parameters: ["user_id" : peer])
.then { data in
if let response = String(data: data, encoding: .utf8),
let ans = Bool(response) {
DispatchQueue.main.async {
handler(ans, nil)
}
} else {
DispatchQueue.main.async {
handler(nil, KinEcosystemError.service(.response, nil))
}
}
}.error { error in
DispatchQueue.main.async {
handler(nil, KinEcosystemError.transform(error))
}
}
}

public func payToUser(offerJWT: String, completion: @escaping KinCallback) -> Bool {
return purchase(offerJWT: offerJWT, completion: completion)
}
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,27 @@ _ = Kin.shared.payToUser(offerJWT: encoded, completion: handler)

3. Complete the pay to user offer after you receive confirmation from the Kin Server that the funds were transferred successfully.

### Finding out if another user has a kin account ###

Before paying to a user, you might want to check if this user actually exists. to do that:

```swift
// from sample app:
Kin.shared.hasAccount(peer: otherUserId) { [weak self] response, error in
if let response = response {
guard response else {
self?.presentAlert("User Not Found", body: "User \(otherUserId) could not be found. Make sure the receiving user has activated kin, and in on the same environment as this user")
return
}
// Proceed with payment (transferKin is an internal function in the sample app)
self?.transferKin(to: otherUserId, appId: id, pKey: jwtPKey)
} else if let error = error {
self?.presentAlert("An Error Occurred", body: "\(error.localizedDescription)")
} else {
self?.presentAlert("An Error Occurred", body: "unknown error")
}
}
```

## License
The kin-ecosystem-ios-sdk library is licensed under [MIT license](LICENSE.md).

0 comments on commit ff63b97

Please sign in to comment.