Code Monkey home page Code Monkey logo

coinbase-ios-sdk's Introduction

coinbase

Integrate bitcoin into your iOS application with Coinbase's fully featured bitcoin payments API. Coinbase allows all major operations in bitcoin through one API. For more information, visit https://coinbase.com/docs/api/overview.

To run the example project, clone the repo, and run pod install from the Example directory first.

Version License Platform

Installation

coinbase is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "coinbase-official"

Authentication

The Coinbase iOS SDK can be used with both Coinbase API keys and OAuth2 authentication. Use API keys if you only need to access your own Coinbase account from within your application. Use OAuth2 if you need to access your user's accounts. Most iOS apps will need to use OAuth2.

API key authentication

Simply use coinbaseWithApiKey:secret:. Example:

Coinbase *apiClient = [Coinbase coinbaseWithApiKey:myKey secret:mySecret];

OAuth2

To use OAuth2 you will need to add a custom URI scheme to your application. This URI scheme must start with your app's bundle identifier. For example, if your bundle ID is "com.example.app", your URI scheme could be "com.example.app.coinbase-oauth". To add a URI scheme:

  1. In Xcode, click on your project in the Project Navigator
  2. Select your app's target
  3. Click Info
  4. Open URL Types
  5. Click "+" to create a new URL Type
  6. Enter your new URL scheme in both Identifier and URL Schemes

You now need to create an OAuth2 application for your iOS application at https://www.coinbase.com/oauth/applications. Click + Create an Application and enter a name for your application. In Permitted Redirect URIs, you should enter "your_scheme://coinbase-oauth" - for example, if your custom URI scheme is "com.example.app.coinbase-oauth", then you should enter "com.example.app.coinbase-oauth://coinbase-oauth". Save the application and take note of the Client ID and Secret.

You can now integrate the OAuth2 sign in flow into your application. Use startOAuthAuthenticationWithClientId:scope:redirectUri:meta: to start the external sign in process.

// 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];

You must override openURL in your application delegate to receive the OAuth authorization grant code and pass it back in to the 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;

}

See the Example folder for a fully functional example.

Usage

After creating a Coinbase object using one of the authentication methods above, the API methods at https://developers.coinbase.com/api can be called using the doGet, doPost, doPut and doDelete methods on Coinbase. Example:

[apiClient doGet:@"users/self" parameters:nil completion:^(id result, NSError *error) {
    if (error) {
        NSLog(@"Could not load user: %@", error);
    } else {
        NSLog(@"Signed in as: %@", [[result objectForKey:@"user"] objectForKey:@"email"]);
    }
}];

License

coinbase is available under the MIT license. See the LICENSE file for more info.

coinbase-ios-sdk's People

Contributors

isaacwaller avatar mickeyreiss avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.