OS情報の取得

Swift3.0
import UIKit
import AdSupport
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let mySystemLabel: UILabel = UILabel(frame: CGRect(x:0,y:0,width:self.view.bounds.width,height:50))
mySystemLabel.backgroundColor = UIColor.orange
mySystemLabel.layer.masksToBounds = true
mySystemLabel.layer.cornerRadius = 20.0
mySystemLabel.textColor = UIColor.white
mySystemLabel.shadowColor = UIColor.gray
mySystemLabel.textAlignment = NSTextAlignment.center
mySystemLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: 200)
self.view.addSubview(mySystemLabel)
let mySystemName = UIDevice.current.systemName
let mySystemVersion = UIDevice.current.systemVersion
print("Name: \(mySystemName)")
print("Version: \(mySystemVersion)")
mySystemLabel.text = "Name: \(mySystemName) Ver: \(mySystemVersion)"
let myIDforVender = UIDevice.current.identifierForVendor
let myASManager = ASIdentifierManager()
let myIDforAd = myASManager.advertisingIdentifier
print("myIDforAd :\(myIDforAd!.uuidString.utf8)")
let myASManagerLabel: UILabel = UILabel(frame: CGRect(x:0,y:0,width:self.view.bounds.width,height:50))
myASManagerLabel.backgroundColor = UIColor.orange
myASManagerLabel.layer.masksToBounds = true
myASManagerLabel.layer.cornerRadius = 20.0
myASManagerLabel.textColor = UIColor.white
myASManagerLabel.font = UIFont.systemFont(ofSize: CGFloat(10))
myASManagerLabel.shadowColor = UIColor.gray
myASManagerLabel.textAlignment = NSTextAlignment.center
myASManagerLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: 300)
self.view.addSubview(myASManagerLabel)
myASManagerLabel.text = "\(myIDforAd!.uuidString)"
}
}
Swift 2.3
import UIKit
import AdSupport
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let mySystemLabel: UILabel = UILabel(frame: CGRectMake(0,0,self.view.bounds.width,50))
mySystemLabel.backgroundColor = UIColor.orangeColor()
mySystemLabel.layer.masksToBounds = true
mySystemLabel.layer.cornerRadius = 20.0
mySystemLabel.textColor = UIColor.whiteColor()
mySystemLabel.shadowColor = UIColor.grayColor()
mySystemLabel.textAlignment = NSTextAlignment.Center
mySystemLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: 200)
self.view.addSubview(mySystemLabel)
let mySystemName = UIDevice.currentDevice().systemName
let mySystemVersion = UIDevice.currentDevice().systemVersion
print("Name: \(mySystemName)")
print("Version: \(mySystemVersion)")
mySystemLabel.text = "Name: \(mySystemName) Ver: \(mySystemVersion)"
let myIDforVender = UIDevice.currentDevice().identifierForVendor
let myASManager = ASIdentifierManager()
let myIDforAd = myASManager.advertisingIdentifier
print("myIDforAd :\(myIDforAd.UUIDString.utf8)")
let myASManagerLabel: UILabel = UILabel(frame: CGRectMake(0,0,self.view.bounds.width,50))
myASManagerLabel.backgroundColor = UIColor.orangeColor()
myASManagerLabel.layer.masksToBounds = true
myASManagerLabel.layer.cornerRadius = 20.0
myASManagerLabel.textColor = UIColor.whiteColor()
myASManagerLabel.font = UIFont.systemFontOfSize(CGFloat(10))
myASManagerLabel.shadowColor = UIColor.grayColor()
myASManagerLabel.textAlignment = NSTextAlignment.Center
myASManagerLabel.layer.position = CGPoint(x: self.view.bounds.width/2,y: 300)
self.view.addSubview(myASManagerLabel)
myASManagerLabel.text = "\(myIDforAd.UUIDString.utf8)"
}
}
2.3と3.0の差分
- UIColorの参照方法が変更(UIColor.grayColor()->UIColor.gray)
- CGRect,CGPointの初期化方法の変更(CGRectMake,CGPointMakeの廃止)
UIDevice.currentDevice()
がUIDevice.current
に変更
Reference
- UIDevice Class
- ASIdentifierManager Class