Withings-SDK-iOS 0.2.2

Withings-SDK-iOS 0.2.2

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2016
SwiftSwift Version 3.0

Maintained by Johan Drevet.



 
Depends on:
OAuthSwift~> 1.1.0
DCKeyValueObjectMapping~> 1.5
SAMKeychain~> 1.5.2
 

  • By
  • Johan Drevet

Overview

Withings-SDK-iOS provides an Objective-C interface for integrating iOS apps with the Withings API. It handles OAuth 1.0 authentication using OAuthSwift library.

Features

For now, the SDK implements the following Withings API:

The following features will be added in the future:

Requirements

Withings-SDK-iOS requires iOS 8.0 and above.

In order to use the API, you will need to register as a developer here to get a consumer key and secret. Note that you will also need to have an end-user Withings account to fetch data from.

Several third-party open source libraries are used within Withings-SDK-iOS:

  1. OAuthSwift - OAuth support
  2. DCKeyValueObjectMapping - JSON mapping
  3. SAMKeyChain - Keychain wrapper

Usage

SDK setup

Before any other call, set up the shared WithingsAPI object with your application keys. For example, you can set up the SDK in the method application:didFinishLaunchingWithOptions: in your AppDelegate.

// AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString *consumerKey = @"<Your consumer key>";
    NSString *consumerSecret = @"<Your consumer secret>";
    [[WithingsAPI sharedInstance] setUpWithConsumerKey:consumerKey consumerSecret:consumerSecret];
    return YES;
}

To get your keys, register as a developer here.

Callback management

During the OAuth 1.0 authentication process, the user will be redirect to a web page managed by Withings to authorize your application to access to his resources. Your application should be configured to handle the callback called at the end of the process.

  1. Declare an URL scheme for your application. Image

  2. In your AppDelegate, implement application:openURL: and call handleOpenURL: on the shared WithingsAPI object.

// AppDelegate.m

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    [[WithingsAPI sharedInstance] handleOpenURL:url];
    return YES;
}

Do not forget the deprecated method to manage the callbacks on iOS 8.0.

// AppDelegate.m

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation
{
    [[WithingsAPI sharedInstance] handleOpenURL:url];
    return YES;
}

Request user authorization

Request the user authorization by calling the following method. The user will be redirect to a web page provided by Withings to authorize your application to access to his resources.

// SomeViewController.h

[[WithingsAPI sharedInstance] requestAccessAuthorizationWithCallbackScheme:@"<Your application scheme>" presenterViewController:self success:^(NSString *userId) {
    //Persist the user id to be able to request Withings API without requesting again the user authorization
} failure:^(WithingsError *error) {
    //Manage the error
}];

Call APIs

Once you have user authorization, you can call any API provided by the API client. You can manage one or more instance of clients or simply use the instance of client held by the WithingsAPI singleton. For example, to get all the activities measures for an user, call:

// SomeViewController.h

[[WithingsAPI sharedInstance].measureAPIClient getActivitiesMeasuresForUser:@"<The user id>" success:^(NSArray<WithingsActivity *> *activitiesMeasures) {
    //Process the results
} failure:^(WithingsError *error) {
    //Manage the error
}

Author

Johan Drevet

License

Withings-SDK-iOS is released under the MIT license. See LICENSE for details.