完整的 SDK 文档可以在 http://jawbone.github.io/UPPlatform_iOS_SDK/ 查看
此 SDK 为 iOS 应用与 UP 平台 集成提供 Objective-C 接口。它通过 UIWebView 使用 OAuth 2.0 处理身份验证,并提供了简单的接口来调用平台 REST 终端。
开始使用 UP iOS SDK 开发的要求只有 OS X Mavericks、Xcode 5 和 iOS 7。如果您还没有升级,建议使用 OS X Mavericks、Xcode 5 和 iOS 7。
为了验证 UP 平台,您需要有 Jawbone UP 用户账户。您可以通过访问 jawbone.com/start/signup 创建新账户。
使用您的 Jawbone UP 账户登录到 Jawbone UP 开发者门户。如果您还没有账户,您可以通过访问 jawbone.com/start/signup 创建一个。
通过点击 "管理账户" 来注册您的组织。
按照说明创建一个新应用,并获取您用于通过 UP 平台进行身份验证的 OAuth 客户端 ID 和 应用程序密钥。
在 "OAuth 重定向 URI" 字段中指定您自定义的重定向 URI 或使用默认值 up-platform://redirect
。
您可以通过以下链接下载最新的iOS SDK版本,或者直接从本GitHub仓库克隆。
选项 1:下载UP iOS SDK v1.0.0(2013年11月)
https://github.com/Jawbone/UPPlatformSDK/releases/tag/v0.1-alpha
选项 2:从GitHub克隆此仓库
git clone [email protected]:Jawbone/UPPlatformSDK.git
Jawbone UP iOS SDK附带一个iOS示例应用,您可以使用它通过UP平台进行身份验证并开始与Jawbone UP账户交互。
您可以在UPPlatformSDK/HelloUP/HelloUP.xcodeproj
中找到并打开PlatformTest项目。
UPPlatformSDK.framework
拖放到Xcode项目或工作区中的Frameworks组中。-ObjC
添加到其他链接器标志。这样就完成了!
现在只需在项目中任何要使用SDK的地方添加#import <UPPlatformSDK/UPPlatformSDK.h>
即可。
身份验证通过共享的UPPlatform
对象来处理。要启动一个新会话,请使用startSessionWithClientID:clientSecret:authScope:completion:
方法。
注意:如果您在Jawbone UP开发者门户中指定了您的应用的定制重定向URI,则可以使用startSessionWithClientID:clientSecret:authScope:redirectURI:completion:
方法指定它。
[[UPPlatform sharedPlatform] startSessionWithClientID:@"MY_CLIENT_ID"
clientSecret:@"MY_CLIENT_SECRET"
authScope:(UPPlatformAuthScopeExtendedRead | UPPlatformAuthScopeMoveRead)
completion:^(UPSession *session, NSError *error) {
if (session != nil) {
// Your code to start making API requests goes here.
}
}];
注意:可用的UPPlatformAuthScope值可以在UPPlatform.h
中找到。
一旦建立了有效的会话,有几种方式可以创建API请求。您可以使用提供的封装大部分可用端点的对象,或者创建自定义请求。API对象是创建REST平台请求的最简单方式。它们将创建网络请求并解析结果JSON到NSObject
中。
要验证现有会话是否仍然有效,请调用validateSessionWithCompletion:
。如果传递给完成块中的session
对象不是nil
,则会话有效,可以发起API请求。此方法有助于防止在用户的访问令牌无效时意外返回401未授权
响应。
为了启用通过SDK发送和接收的网络请求和响应的额外日志记录,应将[UPPlatform SharedPlatform].enableNetworkLogging
设置为YES。
注意:所有单位(重量、距离)均为公制。
[UPUserAPI getCurrentUserWithCompletion:^(UPUser *user, UPURLResponse *response, NSError *error) {
// Your code to process returned UPUser object.
}];
[UPUserAPI getFriendsWithCompletion:^(NSArray *friends, UPURLResponse *response, NSError *error) {
// Your code here to process an array of UPUser objects.
}];
[UPUserAPI getTrendsWithEndDate:nil
rangeType:UPUserTrendsRangeTypeDays
rangeDuration:10U
bucketSize:UPUserTrendsBucketSizeDays
completion:^(NSArray *trends, UPURLResponse *response, NSError *error) {
// Your code here to process an array of UPTrend objects.
}];
[UPMoveAPI getMovesWithLimit:10U completion:^(NSArray *moves, UPURLResponse *response, NSError *error) {
// Your code here to process an array of UPMove objects.
}];
您可以通过请求一个560x300的具有透明背景的PNG图像,将用户的移动数据可视化。
[UPMoveAPI getMoveGraphImage:move completion:^(UIImage *image) {
// Your code here to process the graph image.
}];
[UPWorkoutAPI getWorkoutsWithLimit:10U completion:^(NSArray *workouts, UPURLResponse *response, NSError *error) {
// Your code here to process an array of UPWorkout objects.
}];
我们可以从创建一个新的锻炼事件开始。
UPWorkout *workout = [UPWorkout workoutWithType:UPWorkoutTypeBike
startTime:startTime
endTime:endTime
intensity:UPWorkoutIntensityEasy
caloriesBurned:@300];
workout.distance = @7;
workout.imageURL = @"YOUR_IMAGE_URL.png";
然后,我们可以将此锻炼事件发布到用户的帖子中。
[UPWorkoutAPI postWorkout:workout completion:^(UPWorkout *workout, UPURLResponse *response, NSError *error) {
// Your completion code here.
}];
您可以通过请求一个560x300的具有透明背景的PNG图像,将用户的锻炼数据可视化。
[UPWorkoutAPI getWorkoutGraphImage:workout completion:^(UIImage *image) {
// Your code here to use the graph image.
}];
[UPSleepAPI getSleepsWithLimit:10U completion:^(NSArray *sleeps, UPURLResponse *response, NSError *error) {
// Your code here to process an array of UISleep objects.
}];
您可以通过请求一个560x300的具有透明背景的PNG图像,将用户的睡眠数据可视化。
[UPSleepAPI getSleepGraphImage:sleep completion:^(UIImage *image) {
// Your code here to handle the graph image.
}];
[UPMealAPI getMealsWithLimit:5U completion:^(NSArray *meals, UPURLResponse *response, NSError *error) {
// Your code here to process the meals array.
}];
餐食事件应包含一个或多个餐食品项。要创建新的餐食事件,我们首先指定单个餐食品项的营养信息。
UPMealNutritionInfo *info = [[UPMealNutritionInfo alloc] init];
info.calories = @130;
info.sugar = @30;
info.carbohydrates = @10;
info.calcium = @80;
然后,我们创建一个新的餐食品项并设置其营养信息。
UPMealItem *item = [UPMealItem mealItemWithName:@"Granola Bar"
description:@"A fancy granola bar."
amount:@1
measurementUnits:@"bar"
servingType:UPMealItemServingTypePlate
foodType:UPMealItemFoodTypeBrand
nutritionInfo:info];
然后,我们创建一个新的餐食事件来保存我们刚才创建的餐食品项。
UPMeal *meal = [UPMeal mealWithTitle:@"Delicious Granola Bar"
note:@"It was tasty"
items:@[item]];
meal.photoURL = @"YOUR_PHOTO_URL.png";
最后,让我们在自己的用户帖子中发布新的餐食事件!
[UPMealAPI postMeal:meal completion:^(UPMeal *meal, UPURLResponse *response, NSError *error) {
// Your code here to process the meal object.
}];
[UPMealAPI getMealDetails:meal completion:^(UPMeal *meal, UPURLResponse *response, NSError *error) {
// Your code here to process the meal object.
}];
[UPMoodAPI getCurrentMoodWithCompletion:^(UPMood *mood, UPURLResponse *response, NSError *error) {
[self showResults:mood];
}];
要设置用户的心情,我们首先需要创建一个新的UPMood对象。
UPMood *newMood = [UPMood moodWithType:UPMoodTypePumpedUp title:@"I'm pumped!"];
然后,我们可以将新的UPMood对象发布到用户的帖子中。
[UPMoodAPI postMood:newMood completion:^(UPMood *mood, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
[UPMoodAPI getCurrentMoodWithCompletion:^(UPMood *mood, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
[UPMoodAPI deleteMood:mood completion:^(id result, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
要创建一个新的心脏事件,我们首先需要创建一个新的UPCardiacEvent对象。
UPCardiacEvent *cardiacEvent = [UPCardiacEvent eventWithTitle:@"Run"
heartRate:@120
systolicPressure:@120
diastolicPressure:@80
note:@"Good weather."
image:nil];
然后,我们可以将新的UPCardiacEvent对象发布到用户的帖子中。
[UPCardiacEventAPI postCardiacEvent:cardiacEvent
completion:^(UPCardiacEvent *event, UPURLResponse *response, NSError *error) {
}];
[UPCardiacEventAPI getCardiacEventsWithCompletion:^(NSArray *results, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
[UPCardiacEventAPI deleteCardiacEvent:event
completion:^(id result, UPURLResponse *response, NSError *error) {
// Your code goes here.
}};
要创建一个新的正文事件,我们首先需要创建一个新的 UPBodyEvent 对象。
UPBodyEvent *bodyEvent = [UPBodyEvent eventWithTitle:@"Morning start."
weight:@70
bodyFat:@0.14f
leanMass:@29.5f
bmi:@18.5f
note:@"Good progress."
image:nil];
然后,我们可以将新的 UPBodyEvent 对象发布到用户的动态里。
[UPBodyEventAPI postBodyEvent:bodyEvent
completion:^(UPBodyEvent *event, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
[UPBodyEventAPI getBodyEventsWithCompletion:^(NSArray *results, UPURLResponse *response, NSError *error)
// Your code goes here.
}];
[UPBodyEventAPI deleteBodyEvent:bodyEvent
completion:^(id result, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
要创建一个新的通用事件,我们首先需要创建一个新的 UPGenericEvent 对象。
UPGenericEvent *genericEvent = [UPGenericEvent eventWithTitle:@"Good deed."
verb:@"Done well."
attributes:@{}
note:@"Indeed."
image:nil];
然后,我们可以将新的 UPGenericEvent 对象发布到用户的动态里。
[UPGenericEventAPI postGenericEvent:genericEvent
completion:^(UPGenericEvent *event, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
[UPGenericEventAPI getGenericEventsWithCompletion:^(NSArray *results, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
[UPGenericEventAPI deleteGenericEvent:genericEvent
completion:^(id result, UPURLResponse *response, NSError *error) {
// Your code goes here.
}];
可以使用 UPURLRequest
对象进行自定义 API 请求,这也是 API 对象所使用的。这允许您向任何端点发出请求,提供任何参数,并接收结果的 JSON 对象。以下是一些示例
UPURLRequest *request = [UPURLRequest getRequestWithEndpoint:@"nudge/api/users/@me"
params:nil];
[[UPPlatform sharedPlatform] sendRequest:request
completion:^(UPURLRequest *request, UPURLResponse *response, NSError *error) {
// The resulting response.data is an NSDictionary with the JSON results.
}];
NSDictionary *params = @{
@"title": @"I feel great!",
@"sub_type": @1,
@"tz": [NSTimeZone localTimeZone].name
};
UPURLRequest *request = [UPURLRequest postRequestWithEndpoint:@"nudge/api/users/@me/mood"
params:params];
[[UPPlatform sharedPlatform] sendRequest:request
completion:^(UPURLRequest *request, UPURLResponse *response, NSError *error) {
// The resulting response.data is an NSDictionary describing the created mood.
}];
SDK 包含了一组覆盖 API 功能的 XCTest 单元测试。您可以通过在 Xcode 5 中打开 UP iOSK SDK 项目并按 ⌘ + Shift + U 来运行测试。
您可以在 https://jawbone.com/up/developer 找到额外的 Jawbone UP 平台文档。
管理 UP API 使用的主要原则
通过使用 API,您同意服务条款,并可进行审查。
请访问 UP 平台的 状态页面 查看平台的实时状态更新。
关注我们的 Twitter @JawboneDev 获取有关 API 的最新消息和更新。
通过发送电子邮件到 [email protected] 联系开发人员支持团队。
Andy Roth
资深软件工程师
Jawbone
使用遵循Apache许可证(v2.0)。详细信息请见LICENSE文件。