测试已测试 | ✓ |
语言语言 | Objective-CObjective C |
许可 | MIT |
发布上次发布 | 2016年10月 |
由Isaac Waller、Dai Hovey、Johnny、Jane Chung维护。
使用 Coinbase 全功能的比特币支付 API 将比特币集成到您的 iOS 应用程序中。Coinbase 允许通过一个 API 完成所有主要的比特币操作。更多信息,请访问 https://developers.coinbase.com/docs/wallet。
要尝试示例项目,请安装 CocoaPods,然后运行 pod try coinbase-official
。
Coinbase iOS SDK 可与 Coinbase API 密钥和 OAuth2 认证一起使用。如果您只需要在应用程序中访问自己的 Coinbase 账户,请使用 API 密钥。如果您需要访问用户的账户,请使用 OAuth2。大多数 iOS 应用程序都需要使用 OAuth2。
OAuth2 允许您访问其他用户的账户。如果用户的手机上安装了 Coinbase 应用程序,则使用 OAuth2 认证只需单击一次。
如果没有安装 Coinbase 应用程序,认证将无缝回退到 Safari。
要使用 OAuth2,您需要在应用程序中添加一个自定义 URI 方案。此 URI 方案必须以您的应用程序包标识符开头。例如,如果您的包标识符为 "com.example.app",则您的 URI 方案可以是 "com.example.app.coinbase-oauth"。要通过添加 URI 方案
现在您需要在 https://www.coinbase.com/oauth/applications 为您的 iOS 应用程序创建一个 OAuth2 应用程序。单击 + 创建一个应用程序
并为应用程序输入一个名称。在 Permitted Redirect URIs
中,您应输入 "your_scheme://coinbase-oauth" - 例如,如果您的自定义 URI 方案为 "com.example.app.coinbase-oauth",则应输入 "com.example.app.coinbase-oauth://coinbase-oauth"。保存应用程序并记下客户端 ID 和密钥。
现在您可以将 OAuth2 登录流程集成到应用程序中。使用 startOAuthAuthenticationWithClientId:scope:redirectUri:meta:
启动外部登录过程。
// Launch the web browser or Coinbase app to authenticate the user.
[CoinbaseOAuth startOAuthAuthenticationWithClientId:@"your client ID"
scope:@"user balance"
redirectUri:@"com.example.app.coinbase-oauth://coinbase-oauth" // Same as entered into Create Application
meta:nil];
CoinbaseOAuth.startOAuthAuthenticationWithClientId("your client ID", scope: "user balance", redirectUri: "com.example.app.coinbase-oauth://coinbase-oauth", meta: nil)
您必须在您的应用代理中重写 openURL
方法以接收OAuth授权码,并将其传递回Coinbase SDK。
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([[url scheme] isEqualToString:@"com.example.app.coinbase-oauth"]) {
// This is a redirect from the Coinbase OAuth web page or app.
[CoinbaseOAuth finishOAuthAuthenticationForUrl:url
clientId:@"your client ID"
clientSecret:@"your client secret"
completion:^(id result, NSError *error) {
if (error) {
// Could not authenticate.
} else {
// Tokens successfully obtained!
// Do something with them (store them, etc.)
Coinbase *apiClient = [Coinbase coinbaseWithOAuthAccessToken:[result objectForKey:@"access_token"]];
// Note that you should also store 'expire_in' and refresh the token using [CoinbaseOAuth getOAuthTokensForRefreshToken] when it expires
}
}];
return YES;
}
return NO;
}
if url.scheme == "com.example.app.coinbase-oauth" {
CoinbaseOAuth.finishOAuthAuthenticationForUrl(url, clientId: "your client ID", clientSecret: "your client secret", completion: { (result : AnyObject?, error: NSError?) -> Void in
if error != nil {
// Could not authenticate.
} else {
// Tokens successfully obtained!
// Do something with them (store them, etc.)
if let result = result as? [String : AnyObject] {
if let accessToken = result["access_token"] as? String {
let apiClient = Coinbase(OAuthAccessToken: accessToken)
}
}
// Note that you should also store 'expire_in' and refresh the token using CoinbaseOAuth.getOAuthTokensForRefreshToken() when it expires
}
})
return true
}
else {
return false
}
查阅 示例
文件夹以获取完整的示例。
直接使用 coinbaseWithApiKey:secret:
。示例
Coinbase *apiClient = [Coinbase coinbaseWithApiKey:myKey secret:mySecret];
let coinbase = Coinbase(apiKey: myKey, secret: mySecret)
使用上述认证方法之一创建 Coinbase
对象后,可以使用 Coinbase
的便利方法调用位于 https://developers.coinbase.com/api 上的API方法。示例
[apiClient getCurrentUser:^(CoinbaseUser *user, NSError *error) {
if (error) {
NSLog(@"Could not load user: %@", error);
} else {
NSLog(@"Signed in as: %@", user.email);
}
}];
CoinbaseAccount *account = [[CoinbaseAccount alloc] initWithID:@"536a541fa9393bb3c7000034" client:apiClient];
[account getBalance:^(CoinbaseBalance *balance, NSError *error) {
if (error) {
NSLog(@"Could not get balance: %@", error);
} else {
NSLog(@"User's balance: %@ in %@", balance.amount, balance.currency);
}
}];
Coinbase().getSupportedCurrencies() { (response: Array?, error: NSError?) in
if let error = error {
NSLog("Error: \(error)")
} else {
self.currencies = (response as? [CoinbaseCurrency])!
self.tableView.reloadData()
}
}
Coinbase().getBuyPrice { (btc: CoinbaseBalance?, fees: Array?, subtotal: CoinbaseBalance?, total: CoinbaseBalance?, error: NSError?) in
if let error = error {
NSLog("Error: \(error)")
} else {
self.buyTotal.text = "Buy Price: \(total!.amount!) BTC"
}
}
coinbase遵照MIT许可证提供。更多信息请查阅LICENSE文件。