Created
October 9, 2015 11:44
-
-
Save bryanburgers/daa09d5d8c3b61c6d98a 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
// | |
// UserAgent.swift | |
// | |
import Foundation | |
class UserAgent { | |
static func getUserAgent() -> String { | |
let bundleDict = NSBundle.mainBundle().infoDictionary! | |
let appName = bundleDict["CFBundleName"] as! String | |
let appVersion = bundleDict["CFBundleShortVersionString"] as! String | |
let appDescriptor = appName + "/" + appVersion | |
let currentDevice = UIDevice.currentDevice() | |
let osDescriptor = "iOS/" + currentDevice.systemVersion | |
let hardwareString = self.getHardwareString() | |
return appDescriptor + " " + osDescriptor + " (" + hardwareString + ")" | |
} | |
static func getHardwareString() -> String { | |
var name: [Int32] = [CTL_HW, HW_MACHINE] | |
var size: Int = 2 | |
sysctl(&name, 2, nil, &size, &name, 0) | |
var hw_machine = [CChar](count: Int(size), repeatedValue: 0) | |
sysctl(&name, 2, &hw_machine, &size, &name, 0) | |
let hardware: String = String.fromCString(hw_machine)! | |
return hardware | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works for Swift 5: