UpholdSdk 0.17.0

UpholdSdk 0.17.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新版本2018年5月
SPM支持 SPM

Sandro Machado 维护。



 
依赖
KeychainSwift= 8.0.3
ObjectMapper= 2.2.9
PromiseKit= 4.5.0
SwiftClient= 3.0.1
 

UpholdSdk 0.17.0

  • 作者
  • Diogo Guimarães 和 Sandro Machado

Uphold SDK for iOS Build Status Carthage compatible CocoaPods

Uphold 是一个下一代平台,允许任何人免费、即时和安全地进行价值转移和交换。

iOS Uphold SDK 为开发者提供了将 iOS 应用程序与 Uphold API 集成的一种简单方法。

要求

* Xcode 8
* Swift 3
* Carthage or CocoaPods

安装

使用 CocoaPods

  1. 将以下内容添加到您的 Podfile 中。

    platform :ios, '10.0'
    use_frameworks!
    
    # To use Uphold's production environment.
    pod 'UpholdSdk/Production'
    
    # To use Uphold's sandbox environment:
    # pod 'UpholdSdk/Sandbox'
    
  2. 运行 pod install

使用 Carthage

  1. 将以下内容添加到您的 Cartfile 中。

    github "uphold/uphold-sdk-ios" ~> 0.17.0
    
  2. 运行 carthage update --platform iOS,指定所需的构建配置和Uphold的不同环境。

    # To use Uphold's production environment.
    carthage update --platform iOS --configuration ProductionRelease
    
    # To use Uphold's sandbox environment:
    # carthage update --platform iOS --configuration SandboxRelease
    

基本用法

要了解更多关于Uphold API的信息,请访问开发者网站

要使用SDK,您必须首先注册一个应用程序并获取唯一的CLIENT_IDCLIENT_SECRET组合。我们建议您首先在沙盒环境中注册应用程序,这样您可以在开发过程中安全地进行实验。

在您的账户中的应用程序页面上,您可以获取client idclient secret,配置所需的redirect URI以及所需的scopes

用户认证

为了在授权过程后允许用户重定向回应用程序,您需要通过在Info.plist文件中添加以下密钥,将您自定义的scheme与您的应用程序关联

  • CFBundleURLTypes - 要由应用程序处理的URL类型列表。
    • CFBundleURLSchemes - 自定义应用程序scheme。

例如,我们的演示应用程序具有以下配置

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>uphold-demo</string>
            </array>
        </dict>
    </array>

我们通过实例化UpholdClient并调用beginAuthorization方法来启动认证过程

/// LoginViewController.swift

let upholdClient = UpholdClient()
let authorizationViewController = upholdClient.beginAuthorization(self, clientId: CLIENT_ID, scopes: scopes, state: state)

AppDelegate类中,您需要实现当用户完成授权过程时被调用的方法application(application: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool

/// AppDelegate.swift

func application(application: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    loginViewController.completeAuthorization(url)

    return true
}

要完成授权过程,您需要从upholdClient中调用completeAuthorization方法并从认证响应中获取用户bearing token。

/// LoginViewController.swift

upholdClient.completeAuthorization(authorizationViewController, clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, grantType: "authorization_code", state: state, uri: url).then { (response: AuthenticationResponse) -> () in
    // Get the user bearer token from the authenticationResponse.
}

要获取当前用户信息,只需使用用户携带令牌实例化Uphold客户端,然后调用 getUser() 函数

let upholdClient = UpholdClient(bearertoken: bearerToken)

upholdClient.getUser().then { (user: User) -> () in
    /// The user information is available at the user object.
}

注意:不要忘记在应用程序目标的 功能 选项卡内添加密钥链共享功能。

使用链式方法获取用户卡片

let upholdClient = UpholdClient(bearerToken: bearerToken)

upholdClient.getUser().then { (user: User) -> Promise<[Card]> in
    return user.getCards()
.then { (cards: [Card]) -> () in
    /// Do something with the list of cards.         
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.
}

获取用户卡片

user.getCards().then { (cards: [Card]) -> () in
    /// Do something with the list of cards.        
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.            
}

为用户创建新卡片

let cardRequest: CardRequest = CardRequest(currency: "foo", label: "BTC", settings: CardSettings(position: 1, starred: true))

// Or just create a card without specifying the card settings.
// let cardRequest: CardRequest = CardRequest(currency: "foo", label: "BTC")

user.createCard(cardRequest).then { (card: Card) -> () in
    /// Do something with the card.        
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.            
}

为卡片创建新地址

let addressRequest: AddressRequest = AddressRequest(network: "bitcoin")

card.createAddress(addressRequest).then { (address: Address) -> () in
    /// Do something with the address of the card.        
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.            
}

获取交易牌价

/// Instantiate the client. In this case, we don't need an
/// AUTHORIZATION_TOKEN because the Ticker endpoint is public.
let upholdClient = UpholdClient()

/// Get tickers.
upholdClient.getTickers().then { (rateList: [Rate]) -> () in
    /// Do something with the rates list.
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.
}

或者您可以获取特定货币的交易牌价

/// Get tickers for BTC.
upholdClient.getTickersByCurrency("BTC").then { (rateList: [Rate]) -> () in
    /// Do something with the rates list.
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.
}

创建并提交新交易

let transactionDenominationRequest = TransactionDenominationRequest(amount: "1.0", currency: "BTC")

/// A transaction to a destination (card id, crypto address, email, phone number or username).
let transactionTransferRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, destination: "[email protected]")

card.createTransaction(transactionTransferRequest).then { (transaction: Transaction) -> () in
    /// Commit the transaction.
    transaction.commit(TransactionCommitRequest("Commit message"))
}.error({ (error: ErrorType) -> Void in
    /// Do something with the error.            
})

/// A transaction to a destination (card id, crypto address, email, phone number or username) with reference.
let transactionTransferRequest = TransactionTransferRequest(denomination: transactionDenominationRequest, destination: "[email protected]", reference: "123456")

card.createTransaction(transactionTransferRequest).then { (transaction: Transaction) -> () in
    /// Commit the transaction.
    transaction.commit(TransactionCommitRequest("Commit message"))
}.error({ (error: ErrorType) -> Void in
    /// Do something with the error.            
})

/// A deposit from an ACH or SEPA account.
let transactionDepositRequest = TransactionDepositRequest(denomination: transactionDenominationRequest, origin: "accountId")

card.createTransaction(transactionDepositRequest).then { (transaction: Transaction) -> () in
    /// Commit the transaction.
    transaction.commit(TransactionCommitRequest("Commit message"))
}.error({ (error: ErrorType) -> Void in
    /// Do something with the error.
})

/// A deposit from a credit card.
let transactionCardDepositRequest = TransactionCardDepositRequest(denomination: transactionDenominationRequest, origin: "creditCardId", securityCode: "1234")

card.createTransaction(transactionCardDepositRequest).then { (transaction: Transaction) -> () in
    /// Commit the transaction.
    transaction.commit(TransactionCommitRequest("Commit message"))
}.error({ (error: ErrorType) -> Void in
    /// Do something with the error.
})

如果希望在创建过程中提交交易,请使用第一个参数设置为 truecreateTransaction 方法。

card.createTransaction(true, transactionRequest: transactionRequest)

获取所有公开交易

/// Instantiate the client. In this case, we don't need an
/// AUTHORIZATION_TOKEN because the Ticker endpoint is public.
let upholdClient = UpholdClient()
let paginator: Paginator<Transaction> = client.getReserve().getTransactions()

/// Get the list of transactions.
paginator.elements.then { (transactions: [Transaction]) -> () in
    /// Do something with the list of transactions.            
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.         
}

/// Get the next page of transactions.
paginator.getNext().then { (transactions: [Transaction]) -> () in
    /// Do something with the list of transactions.         
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.         
}

或者,您可以获取特定的公开交易

/// Get one public transaction.
upholdClient.getReserve().getTransactionById("a97bb994-6e24-4a89-b653-e0a6d0bcf634").then { (transaction: Transaction) -> () in
    /// Do something with the list of transactions.     
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.      
}

获取储备状态

/// Instantiate the client. In this case, we don't need an
/// AUTHORIZATION_TOKEN because the Ticker endpoint is public.
let upholdClient = UpholdClient()

/// Get the reserve summary of all the obligations and assets within it.
client.getReserve().getStatistics().then { (reserveStatistics: [ReserveStatistics]) -> () in
    /// Do something with the reserve statistics.    
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.     
}

分页

一些端点将返回分页器。以下是处理它的示例

/// Get public transactions paginator.
let paginator: Paginator<Transaction> = client.getReserve().getTransactions()

/// Get the first page of transactions.
paginator.elements.then { (transactions: [Transaction]) -> () in
    /// Do something with the list of transactions.            
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.         
}

/// Check if the paginator has a valid next page.
paginator.hasNext().then { (hasNext: Bool) -> () in
    /// Do something with the hasNext.     
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.         
}

/// Get the number of paginator elements.
paginator.count().then { (count: Int) -> () in
    /// Do something with the count.    
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.     
}

/// Get the next page.
paginator.getNext().then { (transactions: [Transaction]) -> () in
    /// Do something with the list of transactions.         
}.error { (error: ErrorType) -> Void in
    /// Do something with the error.         
}

Uphold SDK 示例

查看示例应用以探索使用 Uphold iOS SDK 的应用程序。

构建

要构建示例应用程序,您需要Xcode。构建步骤

  1. 克隆存储库。
  2. 获取项目依赖项
carthage bootstrap --platform iOS
  1. 打开示例项目SampleApplication.xcodeproj
  2. 添加密钥链共享功能。
  3. 在 Xcode 内部构建并运行应用程序。

示例应用程序已配置为使用沙箱环境,请确保您使用沙箱账户进行登录。

贡献 & 开发

贡献

发现了一个错误或者有建议吗?请先搜索 问题,如果它是新的,请继续提交它

开发

如果您能帮助我们改进 uphold-sdk-ios,那就太棒了。想帮忙吗?

  1. 将它 Fork。.
  2. 开始编辑。
  3. 运行测试。
  4. 创建一个 Pull Request