Withings-SDK-iOS 0.2.2

Withings-SDK-iOS 0.2.2

测试测试
Lang语言 Obj-CObjective C
许可证 MIT
发布上次发布2016年12月
SwiftSwift 版本3.0

Johan Drevet 维护。



 
依赖
OAuthSwift~> 1.1.0
DCKeyValueObjectMapping~> 1.5
SAMKeychain~> 1.5.2
 

  • 作者
  • Johan Drevet

概述

Withings-SDK-iOS 为集成 iOS 应用与 Withings API 提供了一个 Objective-C 接口。它使用 OAuthSwift 库 处理 OAuth 1.0 认证。

功能

目前,SDK 实现了以下 Withings API:

以下功能将在未来添加:

要求

Withings-SDK-iOS 需要 iOS 8.0 或更高版本。

为了使用 API,您需要注册为开发者并在 此处 获取消费者密钥和密钥。请注意,您还需要有一个 Withings 最终用户新账户以获取数据。

Withings-SDK-iOS 中使用了几个第三方开源库:

  1. OAuthSwift - OAuth 支持
  2. DCKeyValueObjectMapping - JSON 映射
  3. SAMKeyChain - Keychain 包装器

使用

SDK 设置

在执行任何其他调用之前,使用您的应用程序密钥设置共享的 WithingsAPI 对象。例如,您可以在 AppDelegate 中的 application:didFinishLaunchingWithOptions: 方法中设置 SDK。

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

要获取您的密钥,请 在此处 注册为开发者。

回调管理

在 OAuth 1.0 认证过程中,用户将被重定向到由 Withings 管理的网页来授权您的应用程序访问其资源。您的应用程序应配置为处理在过程结束时调用的回调。

  1. 为您的应用声明一个URL方案。图片

  2. 在您的AppDelegate中实现application:openURL:并在共享的对象上调用handleOpenURL:

// AppDelegate.m

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

别忘了管理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;
}

请求用户授权

通过调用以下方法请求用户授权。用户将被重定向到由Withings提供的网页以授权您的应用访问其资源。

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

调用API

一旦您有了用户授权,就可以调用API客户端提供的任何API。您可以管理一个或多个客户端实例,或者简单地使用由单例持有的客户端实例。例如,要获取一个用户的所有活动测量值,调用

// SomeViewController.h

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

作者

Johan Drevet

许可

Withings-SDK-iOS是在MIT许可下发布的。有关详细信息,请参阅LICENSE文件。