SWIFT中获取app本地版本以及从AppStore获取最新版本的方法

获取app本地版本

let infoDictionary = NSBundle.mainBundle().infoDictionary

let OldVersion :AnyObject? = infoDictionary! ["CFBundleShortVersionString"]

let OldVersionStr = OldVersion as? NSString

OldVersionStr!.stringByReplacingOccurrencesOfString(".", withString: "")

let iOldVersion = OldVersionStr!.intValue

获取AppStore版本

let appidUrl = "http://itunes.apple.com/lookup?id=appid"   appid是你的app在AppStore提交时候的获得的

let url = NSURL(string: appidUrl)

let request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: 10)

request.HTTPMethod = "post"

let queue = NSOperationQueue()

NSURLConnection.sendAsynchronousRequest(request, queue: queue) { (response : NSURLResponse?,

data : NSData?,

errer : NSError?) in

let receiveStatusDic = NSMutableDictionary()

if (data != nil) {

let receiveDic : NSDictionary

try!receiveDic = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves) as! NSDictionary

if receiveDic.valueForKey("resultCount")?.intValue > 0{

receiveStatusDic.setValue("1", forKey: "status")

receiveStatusDic.setValue((receiveDic.valueForKey("results"))?.objectAtIndex(0).valueForKey("version"), forKey: "version")

}else {

receiveStatusDic.setValue("-1", forKey: "status")

}

}else{

receiveStatusDic.setValue("-1", forKey: "status")

}

self.performSelectorOnMainThread("receiveData(_:)"), withObject:receiveStatusDic, waitUntilDone: false)

}

你可能感兴趣的:(SWIFT中获取app本地版本以及从AppStore获取最新版本的方法)