CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2019-07-03
by Emre Ciftci

Original Post

Original - Posted on 2016-12-02
by juanjo



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I think I found the answer:
func isUpdateAvailable(completion: @escaping (Bool?, Error?) -> Void) throws -> URLSessionDataTask { guard let info = Bundle.main.infoDictionary, let currentVersion = info["CFBundleShortVersionString"] as? String, let identifier = info["CFBundleIdentifier"] as? String, let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else { throw VersionError.invalidBundleInfo } Log.debug(currentVersion) let task = URLSession.shared.dataTask(with: url) { (data, response, error) in do { if let error = error { throw error } guard let data = data else { throw VersionError.invalidResponse } let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] guard let result = (json?["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String else { throw VersionError.invalidResponse } completion(version != currentVersion, nil) } catch { completion(nil, error) } } task.resume() return task }
- **Usage:**
_ = try? isUpdateAvailable { (update, error) in
if let error = error { print(error) } else if let update = update { print(update) } }
[juanjo's answer][1]

[1]: https://stackoverflow.com/a/40939740/7512091
Swift 3 version:
func isUpdateAvailable() throws -> Bool { guard let info = Bundle.main.infoDictionary, let currentVersion = info["CFBundleShortVersionString"] as? String, let identifier = info["CFBundleIdentifier"] as? String, let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else { throw VersionError.invalidBundleInfo } let data = try Data(contentsOf: url) guard let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else { throw VersionError.invalidResponse } if let result = (json["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String { return version != currentVersion } throw VersionError.invalidResponse }
I think is better to throw an error instead of returning false, in this case I created a VersionError but it can be some other you define or NSError
enum VersionError: Error { case invalidResponse, invalidBundleInfo }
Also consider to call this function from another thread, if the connection is slow it can block the current thread.
DispatchQueue.global().async { do { let update = try self.isUpdateAvailable() DispatchQueue.main.async { // show alert } } catch { print(error) } }
**Update**
Using URLSession:
Instead of using `Data(contentsOf: url)` and block a thread, we can use `URLSession`:
func isUpdateAvailable(completion: @escaping (Bool?, Error?) -> Void) throws -> URLSessionDataTask { guard let info = Bundle.main.infoDictionary, let currentVersion = info["CFBundleShortVersionString"] as? String, let identifier = info["CFBundleIdentifier"] as? String, let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else { throw VersionError.invalidBundleInfo } Log.debug(currentVersion) let task = URLSession.shared.dataTask(with: url) { (data, response, error) in do { if let error = error { throw error } guard let data = data else { throw VersionError.invalidResponse } let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] guard let result = (json?["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String else { throw VersionError.invalidResponse } completion(version != currentVersion, nil) } catch { completion(nil, error) } } task.resume() return task }
example:
_ = try? isUpdateAvailable { (update, error) in if let error = error { print(error) } else if let update = update { print(update) } }

        
Present in both answers; Present only in the new answer; Present only in the old answer;