Last active
August 29, 2015 14:25
-
-
Save RodolfoAntonici/c065a6acc3ae9e66158f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Uber.swift | |
// | |
// Created by Kirby Shabaga on 9/9/14. | |
// Modified by Rodolfo Antonici on 14/6/15 at GetIn. | |
// Copyright (c) 2014 Worxly. All rights reserved. | |
// | |
// | |
import CoreLocation | |
import Foundation | |
import UIKit | |
class Uber { | |
// To get your client key, go to https://developer.uber.com | |
let clientKey = "YOUR_CLIENT_KEY_HERE" | |
// If you don't set up a location, Uber will open the current user location as pickup location | |
var pickupLocation : CLLocationCoordinate2D? | |
// optional properties | |
var pickupNickname : String? | |
var pickupFormattedAddress : String? | |
var dropoffLocation : CLLocationCoordinate2D? | |
var dropoffNickname : String? | |
var dropoffFormattedAddress : String? | |
// ------------------------------------------------------------------- | |
// init with optional property | |
// ------------------------------------------------------------------- | |
init(pickupLocation : CLLocationCoordinate2D?) { | |
self.pickupLocation = pickupLocation | |
} | |
// ------------------------------------------------------------------- | |
// perform a deep link to the Uber App if installed | |
// check all optional properties while construcing the URL | |
// ------------------------------------------------------------------- | |
func deepLink() { | |
if let uberURL = self.constructURL() { | |
var sharedApp = UIApplication.sharedApplication() | |
println(uberURL) | |
sharedApp.openURL(uberURL) | |
} | |
} | |
private func constructURL() -> NSURL? { | |
let uberProtocol = "uber://" | |
let httpsProtocol = "https://m.uber.com/" | |
var uberString = Uber.isUberAppInstalled() ? uberProtocol : httpsProtocol | |
uberString += "?client_id=\(clientKey)" | |
uberString += "&action=setPickup" | |
if let unwrapedPickupLocation = self.pickupLocation { | |
uberString += "&pickup[latitude]=\(unwrapedPickupLocation.latitude)" | |
uberString += "&pickup[longitude]=\(unwrapedPickupLocation.longitude)" | |
} | |
else { | |
uberString += "&pickup=my_location" | |
} | |
uberString += self.pickupNickname == nil ? "" : | |
"&pickup[nickname]=\(self.pickupNickname!)" | |
uberString += self.pickupFormattedAddress == nil ? "" : | |
"&pickup[formatted_address]=\(self.pickupFormattedAddress!)" | |
if self.dropoffLocation != nil { | |
uberString += "&dropoff[latitude]=\(self.dropoffLocation!.latitude)" | |
uberString += "&dropoff[longitude]=\(self.dropoffLocation!.longitude)" | |
} | |
uberString += self.dropoffNickname == nil ? "" : | |
"&dropoff[nickname]=\(self.dropoffNickname!)" | |
uberString += self.dropoffFormattedAddress == nil ? "" : | |
"&dropoff[formatted_address]=\(self.dropoffFormattedAddress!)" | |
if let urlEncodedString = uberString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) { | |
return NSURL(string: urlEncodedString) | |
} else { | |
return nil | |
} | |
} | |
// ------------------------------------------------------------------- | |
// check if the Uber App is installed on the device | |
// ------------------------------------------------------------------- | |
class func isUberAppInstalled() -> Bool { | |
var sharedApp = UIApplication.sharedApplication() | |
let uberProtocol = NSURL(string: "uber://") | |
return sharedApp.canOpenURL(uberProtocol!) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment