FatSecret API的iOS客户端。
通过CocoaPods安装,在您的Podfile
中添加以下内容
pod 'FatSecretKit'
然后根据需要导入
#import <FatSecretKit/FSClient.h>
创建自己的客户端中最困难的部分是OAuth协商,因此这应该能节省您宝贵的时间。您需要的只是您的OAuth消费者密钥和密钥。推荐使用sharedClient
。
[FSClient sharedClient].oauthConsumerKey = @"12345";
[FSClient sharedClient].oauthConsumerSecret = @"67890";
您应该将其放入AppDelegate.m
或类似的文件中,这样它只会运行一次。您始终可以使用[[FSClient alloc] init]
创建自己的客户端。
现在您可以开始使用API了。
[[FSClient sharedClient] searchFoods:term
completion:^(NSArray *foods, NSInteger maxResults, NSInteger totalResults, NSInteger pageNumber) {
// Use data as you will.
self.foods = foods;
[self.tableView reloadData];
}];
// A more verbose version of the above, if you want to utilize the full paramters of the API
[[FSClient sharedClient] searchFoods:term
pageNumber:0
maxResults:50
completion:^(NSArray *foods, NSInteger maxResults, NSInteger totalResults, NSInteger pageNumber) {
// Use data as you will.
self.foods = foods;
[self.tableView reloadData];
}];
[[FSClient sharedClient] getFood:item.identifier
completion:^(FSFood *food) {
NSLog(@"Name: %@", food.name)
}];
还有一个本地对象表示API返回的资源,包括FSFood
,表示食物资源,以及FSServing
,它表示每种食物的分量,由food.get API方法(以及其他方法)返回。
应将用于获取资源的新方法添加到FSClient
类中,并在必要时创建新的本地对象。客户端方法的命名约定应遵循相同的约定
API: foods.search
,iOS: searchFoods
API: food.get
,iOS: getFood
等等。
本地对象上新的方法/属性应遵循类似的约定
API: trans_fat
,iOS: transFat
API: saturated_fat
,iOS: saturatedFat
等等。
为支持新的API方法,需要在FSClient
中创建一个适当命名的函数,并且应该支持所有必要和可选参数,尽管方便的函数也很受欢迎(可参考searchFoods
方法。)您始终可以通过Twitter或通过问题(issues)提出疑问,我们都是好人。
git checkout -b my-new-feature
)git commit -am '添加了一些功能'
)git push origin my-new-feature
)Parker Wightman (@parkerwightman)
感谢atebits提供的OAuthCore库,使这项任务简化了很多。也感谢Sam Vermette提供的出色的SVHTTPRequest库。