//Update
let identifier = Bundle.main.infoDictionary!["CFBundleIdentifier"] as? String
let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier ?? "")")
let req = NSMutableURLRequest(url: url!)
req.httpMethod = "GET"
URLSession.shared.dataTask(with: req as URLRequest) { data, response, error in
if error != nil {
print(error!.localizedDescription)
} else {
do {
let response = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]
let resoultCount = response?["resultCount"] as! Int
if resoultCount == 1 {
let result = response?["results"] as! Array<Dictionary<String, Any>>
let v = (result[0]["version"] as! NSString).doubleValue
let currentVersion = (Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! NSString).doubleValue
print(currentVersion)
print(v)
if currentVersion < v {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: "", message: "New version \(v) available, please update.", preferredStyle: .alert)
let defaultAction2 = UIAlertAction(title: "Cancel", style: .default, handler: { action in
})
alert.addAction(defaultAction2)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
let url = NSURL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?mt=8")
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}
})
alert.addAction(defaultAction)
self.present(alert, animated: true, completion: nil)
})
} else {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: "", message: "Updates not available...", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert.addAction(defaultAction)
self.present(alert, animated: true, completion: nil)
})
}
}
} catch let error as NSError {
print(error)
print(error.localizedDescription)
}
}
}.resume()
No comments:
Post a Comment