-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
์๋ ์ ๋ฐ์ดํธ
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// AppstoreCheck.swift | ||
// BeMe | ||
// | ||
// Created by Yunjae Kim on 2021/02/10. | ||
// | ||
|
||
import Foundation | ||
|
||
enum VersionError: Error { | ||
case invalidResponse, invalidBundleInfo | ||
} | ||
|
||
|
||
class AppStoreCheck { | ||
|
||
static 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/kr/lookup?bundleId=\(identifier)") else { | ||
|
||
throw VersionError.invalidBundleInfo | ||
|
||
} | ||
|
||
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 | ||
|
||
} | ||
|
||
let verFloat = NSString.init(string: version).floatValue | ||
|
||
let currentVerFloat = NSString.init(string: currentVersion).floatValue | ||
|
||
completion(verFloat > currentVerFloat, nil) // ํ์ฌ ๋ฒ์ ์ด ์ฑ์คํ ์ด ๋ฒ์ ๋ณด๋ค ํฐ์ง๋ฅผ Bool๊ฐ์ผ๋ก ๋ฐํ | ||
|
||
} catch { | ||
|
||
completion(nil, error) | ||
|
||
} | ||
|
||
} | ||
|
||
task.resume() | ||
|
||
return task | ||
|
||
} | ||
|
||
} | ||
|