Skip to content

Commit d2f5922

Browse files
committed
If your app is only available outside the U.S. App Store, you will need to set countryCode to the two-letter country code of the store you want to search
1 parent b227969 commit d2f5922

File tree

3 files changed

+120
-56
lines changed

3 files changed

+120
-56
lines changed

AppStoreManager.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |spec|
22

33
spec.name = "AppStoreManager"
4-
spec.version = "1.3.4"
4+
spec.version = "1.3.5"
55
spec.summary = "A new version checking framework in Swift."
66

77
spec.homepage = "https://knottx.github.io"

README.md

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,93 @@ pod 'AppStoreManager'
1111
### Swift Package Manager
1212
- File > Swift Packages > Add Package Dependency
1313
- Add `https://github.com/knottx/AppStoreManager.git`
14-
- Select "Up to Next Major" with "1.3.4"
14+
- Select "Up to Next Major" with "1.3.5"
1515

1616
## 📝 How
17+
1718
### Code Implementation
19+
1820
First:
21+
1922
```swift
2023
import AppStoreManager
2124
```
2225

23-
Show alert when update available, do something like this:
24-
```swift
25-
// Can select version check type => .immediately, .daily, .weekly
26-
AppStoreManager.shared.checkNewVersionAndShowAlert(.immediately, at: self, canSkip: true, preferredStyle: .alert)
27-
// If you don't need to show skip button, you can set the 'canSkip: false'
28-
// PreferredStyle default is '.alert', Can be select between '.alert' and '.actionSheet'
29-
```
26+
Check New Version Available:
3027

31-
For handle when update available, do something like this:
32-
```swift
33-
// Can select version check type => .immediately, .daily, .weekly
34-
AppStoreManager.shared.checkNewVersion(.immediately) { (isAvailable) in
35-
if isAvailable {
36-
// has new version available.
37-
AppStoreManager.shared.showAlertUpdate(at: self, canSkip: true, preferredStyle: .alert)
38-
// If you don't need to show skip button, you can set the 'canSkip: false'
39-
// PreferredStyle default is '.alert', Can be select between '.alert' and '.actionSheet'
40-
}else{
41-
// no new version.
28+
- Can select version check type => `.immediately`, `.daily`, `.weekly`
29+
30+
31+
```swift
32+
AppStoreManager.shared.checkNewVersion(.immediately) { (isAvailable) in
33+
if isAvailable {
34+
// New Version Available.
35+
} else {
36+
// No New Version.
37+
}
4238
}
43-
}
44-
```
39+
```
40+
41+
- If your app is only available outside the U.S. App Store, you will need to set **countryCode** to the two-letter country code of the store you want to search.
42+
> See http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for a list of ISO Country Codes.
43+
44+
```swift
45+
AppStoreManager.shared.checkNewVersion(.immediately, countryCode: "th") { (isAvailable) in
46+
if isAvailable {
47+
// New Version Available.
48+
} else {
49+
// No New Version.
50+
}
51+
}
52+
```
53+
54+
55+
Check New Version Available and Show Alert:
56+
57+
- Can select version check type => `.immediately`, `.daily`, `.weekly`
58+
- If you don't need to show skip button, you can set the **canSkip:** `false`
59+
60+
```swift
61+
AppStoreManager.shared.checkNewVersionAndShowAlert(.immediately, at: self, canSkip: true)
62+
```
63+
64+
- If your app is only available outside the U.S. App Store, you will need to set **countryCode** to the two-letter country code of the store you want to search.
65+
> See http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for a list of ISO Country Codes.
66+
67+
```swift
68+
AppStoreManager.shared.checkNewVersionAndShowAlert(.immediately, countryCode: "th", at: self, canSkip: true)
69+
```
70+
71+
72+
Open AppStore:
73+
74+
```swift
75+
AppStoreManager.shared.openAppStore()
76+
```
77+
78+
- If your app is only available outside the U.S. App Store, you will need to set **countryCode** to the two-letter country code of the store you want to search.
79+
> See http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for a list of ISO Country Codes.
80+
81+
```swift
82+
AppStoreManager.shared.openAppStore(countryCode: "th")
83+
```
84+
85+
Alert Open AppStore:
86+
87+
- If you don't need to show skip button, you can set the **canSkip:** `false`
88+
- **preferredStyle** default is `.alert`, Can be select between `.alert` and `.actionSheet`
89+
90+
```swift
91+
AppStoreManager.shared.showAlertUpdate(at: self, canSkip: true)
92+
```
93+
94+
- If your app is only available outside the U.S. App Store, you will need to set **countryCode** to the two-letter country code of the store you want to search.
95+
> See http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 for a list of ISO Country Codes.
96+
97+
```swift
98+
AppStoreManager.shared.showAlertUpdate(countryCode: "th",at: self, canSkip: true)
99+
```
45100

46-
For open AppStore, do something like this:
47-
```swift
48-
AppStoreManager.shared.openAppStore()
49-
// go to AppStore for update your Application
50-
```
51101
### Customization
52102
#### AppStoreManager
53103
`AppStoreManager` supports the following:

Sources/AppStoreManager/AppStoreManager.swift

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ public class AppStoreManager {
4848
self.lastVersionCheckDate = UserDefaults.standard.object(forKey: AppStoreDefaults.storedVersionCheckDate) as? Date
4949
}
5050

51-
func getStoreVersion(completion: @escaping (AppStoreResult?) -> Void) {
52-
guard let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(self.bundleId)") else {
51+
private func getStoreVersion(countryCode: String?,
52+
completion: @escaping (AppStoreResult?) -> Void) {
53+
var baseUrl = "https://itunes.apple.com"
54+
if let code = countryCode {
55+
baseUrl.append("/\(code)")
56+
}
57+
baseUrl.append("/lookup?bundleId=\(self.bundleId)")
58+
guard let url = URL(string: baseUrl) else {
5359
completion(nil)
5460
return
5561
}
@@ -74,8 +80,10 @@ public class AppStoreManager {
7480
task.resume()
7581
}
7682

77-
public func checkNewVersion(_ type: VersionCheckType, isAvailable: @escaping (Bool) -> Void) {
78-
self.getStoreVersion { [weak self] result in
83+
public func checkNewVersion(_ type: VersionCheckType,
84+
countryCode: String? = nil,
85+
isAvailable: @escaping (Bool) -> Void) {
86+
self.getStoreVersion(countryCode: countryCode) { [weak self] result in
7987
if let currentInstalledVersion = self?.currentInstalledVersion,
8088
let appStoreVersion = result?.version {
8189
switch currentInstalledVersion.compare(appStoreVersion, options: .numeric) {
@@ -101,51 +109,49 @@ public class AppStoreManager {
101109
isAvailable(false)
102110
}
103111
} else {
112+
self?.log("Can't get Version")
104113
isAvailable(false)
105114
}
106115
}
107116
}
108117

109118
public func checkNewVersionAndShowAlert(_ type: VersionCheckType,
119+
countryCode: String? = nil,
110120
at vc: UIViewController,
111-
canSkip: Bool,
112-
preferredStyle: UIAlertController.Style = .alert) {
113-
self.getStoreVersion { [weak self] result in
114-
if let currentInstalledVersion = self?.currentInstalledVersion,
115-
let appStoreVersion = result?.version {
116-
switch currentInstalledVersion.compare(appStoreVersion, options: .numeric) {
117-
case .orderedAscending:
118-
self?.lastVersionCheckDate = Date()
119-
self?.showAlertUpdate(at: vc, canSkip: canSkip, preferredStyle: preferredStyle)
120-
case .orderedDescending, .orderedSame:
121-
break
122-
}
123-
} else {
124-
self?.log("Can't get Version")
121+
canSkip: Bool) {
122+
self.checkNewVersion(type) { [weak self] isAvailable in
123+
if isAvailable {
124+
self?.showAlertUpdate(at: vc, canSkip: canSkip)
125125
}
126126
}
127127
}
128128

129129
// MARK: - Alert
130130

131-
public func configureAlert(title: String?, message: String?) {
131+
public func configureAlert(title: String?,
132+
message: String?) {
132133
self.title = title ?? AppStoreManagerConstant.alertTitle
133134
self.message = message
134135
}
135136

136-
public func configureAlert(updateButtonTitle: String?, skipButtonTitle: String?) {
137+
public func configureAlert(updateButtonTitle: String?,
138+
skipButtonTitle: String?) {
137139
self.updateButtonTitle = updateButtonTitle ?? AppStoreManagerConstant.updateButtonTitle
138140
self.skipButtonTitle = skipButtonTitle ?? AppStoreManagerConstant.skipButtonTitle
139141
}
140142

141-
public func showAlertUpdate(at vc: UIViewController, canSkip: Bool, preferredStyle: UIAlertController.Style = .alert) {
143+
public func showAlertUpdate(countryCode: String? = nil,
144+
at vc: UIViewController,
145+
canSkip: Bool) {
142146
DispatchQueue.main.async { [weak self] in
143-
let alertVc = UIAlertController(title: self?.title, message: self?.message, preferredStyle: preferredStyle)
147+
let alertVc = UIAlertController(title: self?.title,
148+
message: self?.message,
149+
preferredStyle: .alert)
144150
let skip = UIAlertAction(title: self?.skipButtonTitle ?? AppStoreManagerConstant.skipButtonTitle, style: .cancel) { _ in
145151
//
146152
}
147153
let update = UIAlertAction(title: self?.updateButtonTitle ?? AppStoreManagerConstant.updateButtonTitle, style: .default) { _ in
148-
self?.openAppStore()
154+
self?.openAppStore(countryCode: countryCode)
149155
}
150156
alertVc.addAction(update)
151157
if canSkip {
@@ -155,22 +161,30 @@ public class AppStoreManager {
155161
}
156162
}
157163

158-
public func openAppStore() {
164+
public func openAppStore(countryCode: String? = nil) {
159165
if let appStoreId = self.appStoreResult?.trackId {
160-
self.openAppStore(id: appStoreId)
166+
self.openAppStore(countryCode: countryCode,
167+
id: appStoreId)
161168
} else {
162-
self.getStoreVersion { [weak self] result in
169+
self.getStoreVersion(countryCode: countryCode) { [weak self] result in
163170
guard let appStoreId = result?.trackId else {
164171
self?.log("Can't get an AppId")
165172
return
166173
}
167-
self?.openAppStore(id: appStoreId)
174+
self?.openAppStore(countryCode: countryCode,
175+
id: appStoreId)
168176
}
169177
}
170178
}
171179

172-
func openAppStore(id appStoreId: Int) {
173-
if let url = URL(string: "https://itunes.apple.com/app/id\(appStoreId)"),
180+
private func openAppStore(countryCode: String?,
181+
id appStoreId: Int) {
182+
var baseUrl = "https://itunes.apple.com"
183+
if let code = countryCode {
184+
baseUrl.append("/\(code)")
185+
}
186+
baseUrl.append("/app/id\(appStoreId)")
187+
if let url = URL(string: baseUrl),
174188
UIApplication.shared.canOpenURL(url) {
175189
UIApplication.shared.open(url, options: [:], completionHandler: nil)
176190
} else {

0 commit comments

Comments
 (0)