UberKit is a simple Objective-C wrapper for the new Uber API .
UberKit enhanced by me is currently unavailable with Cocoapods. Alternatively, you can always just drag and drop the folder 'UberKit' into your project and ``#import "UberKit.h"`` and you're good to go.This is to implement the Uber API without having users sign in to their Uber account.
To implement UberKit, first initialize it with your server token
You can get your server token from Uber Developers
UberKit *uberKit = [[UberKit alloc] initWithServerToken:@"YOUR_SERVER_TOKEN"];
Alternatively, you can set your server token to an instance of UberKit as follows:
[[UberKit sharedInstance] setServerToken:@"YOUR_SERVER_TOKEN"];
To get all products available from a particular location
[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);
}
}];
To get the time for arrival of a product to a particular location
[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);
}
}];
To get the price for a trip between two locations
[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);
}
}];
To get the available promotion for a trip between two locations
[uberKit getPromotionForLocation:location endLocation:endLocation withCompletionHandler:^(UberPromotion *promotion, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got the promotion as an UberPromotion
}
else
{
NSLog(@"Error %@", error);
}
}];
This is to implement the Uber API with endpoints that require user authorization such as user history and profile. The authorization process is implemented by:
-
Allowing the users to sign in to their Uber accounts.
-
Obtaining an access token on user approval of app's permissions.
-
Using this access token to make calls to the Uber API.
UberKit automatically redirects to Safari where users enter their Uber login credentials to allow access to their profiles.
Before you can get started using UberKit with login parameters, you must first create an Uber application at Uber Developers and fill in all necessary fields.
[Note: To gain access to the user's profile and history, ensure that you have these permissions enabled in your app's dashboard]
To implement UberKit with authorization from the user first initialize it with your client id, client secret, redirect URI and application name from the application you made on Uber Developer.
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)
Add the UberKit delegate to the @interface of your view controller to detect when an Uber access token becomes available for use after successful authorization. Then, you must add the following methods to your view controller:
- (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
}
You can also retrieve the access token when it is available by using NSString *token = [[UberKit sharedInstance] getStoredAuthToken];
and set the access token when it is stored in your app by using - (void) setAuthTokenWith:(NSString *)token;
To begin the login process, call the method 'startLogin' using [uberKit startLogin];
Once you've successfully retrieved an access token, you can then make the following calls to the Uber API :
To get all activity by the user
[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);
}
}];
To get the profile of the user
[uberKit getUserProfileWithCompletionHandler:^(UberProfile *profile, NSURLResponse *response, NSError *error)
{
if(!error)
{
//Got the user's profile as an UberProfile
}
else
{
NSLog(@"Error %@", error);
}
}];
To make a ride request (Uber sandbox environment)
[[UberKit sharedInstance] getResponseForRequestWithParameters:parameters withCompletionHandler:^(UberRequest *requestResult, UberSurgeErrorResponse *surgeErrorResponse, NSURLResponse *response, NSError *error) {
if(!error)
{
//Got the response from the request
}
else
{
NSLog(@"Error %@", error);
}
}];
To check the request status and details (Uber sandbox environment)
[[UberKit sharedInstance] getDetailsForRequestId:(NSString *)requestId withCompletionHandler:^(UberRequest *requestResult, UberSurgeErrorResponse *surgeErrorResponse, NSURLResponse *response, NSError *error) {
if(!error)
{
//Got the details of the request
}
else
{
NSLog(@"Error %@", error);
}
}];
To estimate a ride before making the real request
[[UberKit sharedInstance] getRequestEstimateWithProductId:(NSString *)productId andStartLocation:(CLLocation *)start endLocation:(CLLocation *)end withCompletionHandler:^(UberEstimate *estimateResult, NSURLResponse *response, NSError *error) {
if(!error)
{
//Got the estimate result
}
else
{
NSLog(@"Error %@", error);
}
}];
To get a map during the ride (Uber sandbox environment)
[[UberKit sharedInstance] getMapForRequestId:(NSString *)requestId withCompletionHandler:^(UberMap *mapResult, NSURLResponse *response, NSError *error) {
if(!error)
{
//Got the map href
}
else
{
NSLog(@"Error %@", error);
}
}];
To get the receipt of your rides
[[UberKit sharedInstance] getReceiptForRequestId:(NSString *)requestId withCompletionHandler:^(UberReceipt *receiptResult, NSURLResponse *response, NSError *error) {
if(!error)
{
//Got the receipt detail
}
else
{
NSLog(@"Error %@", error);
}
}];
To cancel your request (Uber sandbox environment)
[[UberKit sharedInstance] cancelRequestForId:(NSString *)requestId withCompletionHandler:^(NSURLResponse *response, NSError *error) {
if(!error)
{
//Got the http status code
}
else
{
NSLog(@"Error %@", error);
}
}];
For more help, check out the Demo !
(New demo on the request API will come out soon.)
For any assistance, reach out to my QQ: 15387636 or original author on Twitter @sachinkesiraju
Let me know where you use UberKit so I can add it here!
If you feel that you can contribute to improving UberKit or add a new feature, feel free to raise an issue/submit a PR.
UberKit is available under the MIT License. See the LICENSE for more information.