From 63509a4f7a1fcb9efefe1fae9ef34f057db1a7ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Fri, 3 Jun 2016 00:57:13 -0700 Subject: [PATCH] Configure user agent string When the Directions object sends a request, it sets the User-Agent HTTP header to a value of the form: SanDiego/1.2.3 MapboxDirections/0.5.0 iOS/9.3.0 (x86_64) This user agent string is based on the one used by the Mapbox iOS and OS X SDKs and MapboxGeocoder.swift (mapbox/MapboxGeocoder.swift#50). Fixes #20. --- MapboxDirections/MBDirections.swift | 49 ++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/MapboxDirections/MBDirections.swift b/MapboxDirections/MBDirections.swift index d34581558..836f84db1 100644 --- a/MapboxDirections/MBDirections.swift +++ b/MapboxDirections/MBDirections.swift @@ -6,6 +6,51 @@ public let MBDirectionsErrorDomain = "MBDirectionsErrorDomain" /// The Mapbox access token specified in the main application bundle’s Info.plist. let defaultAccessToken = NSBundle.mainBundle().objectForInfoDictionaryKey("MGLMapboxAccessToken") as? String +/// The user agent string for any HTTP requests performed directly within this library. +let userAgent: String = { + var components: [String] = [] + + if let appName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? String ?? NSBundle.mainBundle().infoDictionary?["CFBundleIdentifier"] as? String { + let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String ?? "" + components.append("\(appName)/\(version)") + } + + let libraryBundle: NSBundle? = NSBundle(forClass: Directions.self) + + if let libraryName = libraryBundle?.infoDictionary?["CFBundleName"] as? String, version = libraryBundle?.infoDictionary?["CFBundleShortVersionString"] as? String { + components.append("\(libraryName)/\(version)") + } + + let system: String + #if os(OSX) + system = "OS X" + #elseif os(iOS) + system = "iOS" + #elseif os(watchOS) + system = "watchOS" + #elseif os(tvOS) + system = "tvOS" + #elseif os(Linux) + system = "Linux" + #endif + let systemVersion = NSProcessInfo().operatingSystemVersion + components.append("\(system)/\(systemVersion.majorVersion).\(systemVersion.minorVersion).\(systemVersion.patchVersion)") + + let chip: String + #if arch(x86_64) + chip = "x86_64" + #elseif arch(arm) + chip = "arm" + #elseif arch(arm64) + chip = "arm64" + #elseif arch(i386) + chip = "i386" + #endif + components.append("(\(chip))") + + return components.joinWithSeparator(" ") +}() + extension CLLocationCoordinate2D { /** Initializes a coordinate pair based on the given GeoJSON coordinates array. @@ -138,7 +183,9 @@ public class Directions: NSObject { - postcondition: The caller must resume the returned task. */ private func dataTaskWithURL(url: NSURL, completionHandler: (json: JSONDictionary) -> Void, errorHandler: (error: NSError) -> Void) -> NSURLSessionDataTask { - return NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in + let request = NSMutableURLRequest(URL: url) + request.setValue(userAgent, forHTTPHeaderField: "User-Agent") + return NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in var json: JSONDictionary = [:] if let data = data { do {