UberKit 是一个简单的 Objective-C 封装,用于新的 Uber API。
或者,您可以直接将文件夹“UberKit”拖放到项目中,并使用 #import "UberKit.h"
即可。
这用于实现 Uber API,用户无需登录到他们的 Uber 账户。
要实现 UberKit,首先使用您的服务器令牌进行初始化
您可以从 Uber 开发者 获取您的服务器令牌
UberKit *uberKit = [[UberKit alloc] initWithServerToken:@"YOUR_SERVER_TOKEN"];
或者,您可以将服务器令牌设置为 UberKit 的一个实例,如下所示
[[UberKit sharedInstance] setServerToken:@"YOUR_SERVER_TOKEN"];
获取特定位置可用的所有产品
[uberKit getProductsForLocation:location withCompletionHandler:^(NSArray *products, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got the array of available products for the location.
}
else
{
NSLog(@"Error %@", error);
}
}];
获取产品到达特定位置的时间
[uberKit getTimeForProductArrivalWithLocation:location withCompletionHandler:^(NSArray *times, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got the array of available products and the time they'll take to reach the mentioned location.
}
else
{
NSLog(@"Error %@", error);
}
}];
获取两个位置之间的行程价格
[uberKit getPriceForTripWithStartLocation:location endLocation:endLocation withCompletionHandler:^(NSArray *prices, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got the array of available products and the price of a trip from the start location to the end location.
}
else
{
NSLog(@"Error %@", error);
}
}];
获取两个位置之间行程的可用优惠
[uberKit getPromotionForLocation:location endLocation:endLocation withCompletionHandler:^(UberPromotion *promotion, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got the promotion as an UberPromotion
}
else
{
NSLog(@"Error %@", error);
}
}];
这用于实现 Uber API,其中包含需要用户授权的端点,如用户历史和概况。授权过程通过用户登录自己的 Uber 账户来实现。
允许用户登录他们的 Uber 账户。
在用户同意应用程序的权限后,获取访问令牌。
使用此访问令牌调用 Uber API。
UberKit 会自动在你的应用程序中打开一个网页视图,用户可以通过该视图输入他们的 Uber 登录凭证,允许访问他们的个人资料。
在您可以使用 UberKit 并带有登录参数使用之前,您必须首先在 Uber 开发者 上创建一个 Uber 应用程序,并填写所有必填字段。
[注意:若要访问用户的个人资料和历史记录,请确保您已在其应用程序仪表板中启用这些权限]
为用户授权实现 UberKit,首先使用在 Uber 开发者上创建的应用程序的客户 ID、客户端密钥、重定向 URI 和应用程序名称对其进行初始化。
UberKit *uberKit = [[UberKit alloc] initWithClientID:@"YOUR_CLIENT_ID" ClientSecret:@"YOUR_CLIENT_SECRET" RedirectURL:@"YOUR_REDIRECT_URL" ApplicationName:@"YOUR_APPLICATION_NAME"]; //Set these fields from your app on Uber Developers.
uberKit.delegate = self; //Set the delegate (only for login)
将 UberKit 代理添加到您的视图控制器中的 @interface,以检测在成功授权后何时有可用的 Uber 访问令牌。然后,您必须在您的视图控制器中添加以下方法
- (void) uberKit: (UberKit *) uberKit didReceiveAccessToken: (NSString *) accessToken
{
//Got the access token, can now make requests for user data
}
- (void) uberKit: (UberKit *) uberKit loginFailedWithError: (NSError *) error
{
//An error occurred in the login process
}
您也可以在可用时使用以下代码检索访问令牌:NSString *token = [[UberKit sharedInstance] getStoredAuthToken];
要开始登录过程,通过传递您的视图控制器来调用方法 'startLogin': [uberKit startLoginWithViewController:self];
一旦成功检索到访问令牌,您就可以调用以下Uber API:
获取用户的所有活动
[uberKit getUserActivityWithCompletionHandler:^(NSArray *activities, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got an array of the history of activities performed by the user
}
else
{
NSLog(@"Error %@", error);
}
}];
获取用户的个人资料
[uberKit getUserProfileWithCompletionHandler:^(UberProfile *profile, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got the user's profile as an UberProfile
}
else
{
NSLog(@"Error %@", error);
}
}];
需要更多帮助?请查看 示例!
如需任何帮助,请在微博向我求助 @sachinkesiraju
告诉我您在哪里使用UberKit,我可以将其添加到这里!
如果您认为您可以为改进UberKit或添加新功能做出贡献,请随时提出问题/提交PR。
UberKit在MIT许可证下提供。有关更多信息,请参阅 LICENSE。