最近刚刚学了Swift,然后就用Swift做了一个公司的项目。中途也算是曲折不断了。实话说,Swift真的好用,现在的我已经不想再用oc做开发,我觉代替oc也就是一两年的事吧!废话不多说,切入正题。
1.获取已经上架的App版本信息(腾讯QQ为例)
2.与当前安装的版本作比较
3.决定是否要跳转到App Store 进行下载
以下是我参照的一个前辈的OC版本进行修改的
let url = "http://itunes.apple.com/cn/lookup?id=444934666"
postpath(url)
func postpath(path: String) {
let url = NSURL(string: path)
let request = NSMutableURLRequest(URL: url!, cachePolicy: .ReloadIgnoringCacheData, timeoutInterval: 10)
request.HTTPMethod = "POST"
let queue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue) { (response, data, error) in
var receiveStatusDic = [String: AnyObject]()
if data != nil {
var receiveDic = [String: AnyObject]()
do {
receiveDic = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as! [String : AnyObject]
} catch {
print(error)
}
if receiveDic["resultCount"]?.integerValue > 0 {
receiveStatusDic["status"] = "1"
} else {
receiveStatusDic["status"] = "-1"
}
} else {
receiveStatusDic["status"] = "-1"
}
self.performSelectorOnMainThread(#selector(ViewController.receiveData(_:)), withObject: receiveStatusDic, waitUntilDone: false)
}
}
func receiveData(sender: [String: AnyObject]) {
print("receiveData=\(sender)")
let infoDictionary = NSBundle.mainBundle().infoDictionary
// app版本
let app_Version = infoDictionary!["CFBundleShortVersionString"] as? String
if app_Version != (sender["version"] as? String) {
let alert = UIAlertController(title: "提示", message: "版本有更新", preferredStyle: .Alert)
let action_sure = UIAlertAction(title: "更新", style: .Default) { (action) in
let url = NSURL(string: "https://itunes.apple.com/cn/app/qq-2011/id444934666?mt=8")
UIApplication.sharedApplication().openURL(url!)
}
let action_cancel = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
alert.addAction(action_sure)
alert.addAction(action_cancel)
presentViewController(alert, animated: true, completion: nil)
}
}
最后给大家一个swift和oc代码互转的网页,我在stack overflow交流问题的时候热心的网友给的。(转化中还是有些小问题吧,不过可以用的!)https://objectivec2swift.com/#/home/converter/