要运行示例项目,请克隆仓库,并首先从Example目录中运行pod install
。
这是一个用于Yelp API的CocoaPod。它将简化Objective-C或Swift的开发者从Yelp API获取数据的过程。该库包括搜索、商家和电话搜索API功能。
请记住,在创建应用程序之前阅读并遵守使用条款和展示要求。
YelpAPI可通过CocoaPods获取。要安装它,只需在Podfile中添加以下行
pod "YelpAPI"
在您可以向API发出请求之前,您必须通过使用您的应用程序ID和密钥授权API来创建一个YLPClient
。
[YLPClient authorizeWithAppId:<id> secret:<secret> completionHandler:^
(YLPClient *client, NSError *error) {
// Save your newly authorized client
self.client = client;
}];
一旦您有一个YLPClient
对象,您就可以使用各种与搜索相关的功能
- (void)searchWithLocation:(NSString *)location
term:(nullable NSString *)term
limit:(NSUInteger)limit
offset:(NSUInteger)offset
sort:(YLPSortType)sort
completionHandler:(YLPSearchCompletionHandler)completionHandler;
- (void)searchWithLocation:(NSString *)location
completionHandler:(YLPSearchCompletionHandler)completionHandler;
- (void)searchWithCoordinate:(YLPCoordinate *)coordinate
term:(nullable NSString *)term
limit:(NSUInteger)limit
offset:(NSUInteger)offset
sort:(YLPSortType)sort
completionHandler:(YLPSearchCompletionHandler)completionHandler;
- (void)searchWithCoordinate:(YLPCoordinate *)coordinate
completionHandler:(YLPSearchCompletionHandler)completionHandler;
每个接口都提供了根据您手上信息类型的不同对搜索API进行查询的不同方式。有两种不同的查询搜索API的方法,每种方法都接受不同格式的位置输入。因此,客户端库中存在两套函数来支持对每个版本的搜索API的调用。每套函数都包含一个仅调用API所需参数的版本,另一个则接受所有可选参数的参数。
YLPSearchCompletionHandler
是一个接受一个YLPSearch*
和一个NSError*
对象作为参数的块。API调用成功完成时,结果将返回在YLPSearch*
对象中,或者在NSError*
对象中返回错误。
[self.client searchWithLocation:@"San Francisco, CA" completionHandler:^
(YLPSearch *search, NSError *error) {
// Perform any tasks you need to here
}];
YLPClient
对象还将提供对商家API的访问,相关的功能是
- (void)businessWithId:(NSString *)businessId
completionHandler:(YLPBusinessCompletionHandler)completionHandler;
YLPBusinessCompletionHandler
是一个接收 YLPBusiness*
和 NSError*
对象作为参数的封装块。在 API 调用成功完成之后,结果将返回在 YLPBusiness*
对象中,否则错误将返回在 NSError*
对象中。
[self.client businessWithId:@"yelp-san-francisco" completionHandler:^
(YLPBusiness *search, NSError *error) {
// Perform any tasks you need to here
}];
YLPClient
对象也将提供对电话搜索 API 的访问,相关的函数是
- (void)businessWithPhoneNumber:(NSString *)phoneNumber
completionHandler:(YLPPhoneSearchCompletionHandler)completionHandler;
YLPPhoneSearchCompletionHandler
是一个接收 YLPSearch*
和 NSError*
对象作为参数的封装块。API 调用成功完成之后,结果将返回在 YLPSearch*
对象中,否则错误将返回在 NSError*
对象中。
[self.client businessWithPhoneNumber:@"+14159083801" completionHandler:^
(YLPSearch *search, NSError *error) {
// Perform any tasks you need to here
}];
YLPClient
对象也提供了对评论 API 的访问。相关的方法是
- (void)reviewsForBusinessWithId:(NSString *)businessId
completionHandler:(YLPReviewsCompletionHandler)completionHandler;
- (void)reviewsForBusinessWithId:(NSString *)businessId
locale:(nullable NSString *)locale
completionHandler:(YLPReviewsCompletionHandler)completionHandler;
完成时,YLPReviewsCompletionHandler
将传递成功的 YLPBusinessReviews*
对象,或者在出现错误时传递 NSError*
对象。
[self.client reviewsForBusinessWithId:@"yelp-san-francisco" completionHandler:^
(YLPBusinessReviews *reviews, NSError *error) {
// Perform any tasks you need to here
}];
一个 Response
对象是每次 API 调用成功返回的数据结构。这些对象可以直接使用,它们将包含我们的 API 文档 中记录的所有可用的响应字段。
API 调用返回的 Response
对象可能包含其他 Response
对象。例如,YLPSearch
对象包含一个 YLPBusiness
对象的数组。所有 Response
对象都可以在 这里 找到。