Skip to content

Commit

Permalink
Migrate from Alamafire to URLSession
Browse files Browse the repository at this point in the history
Cleanup code
  • Loading branch information
iMac0de committed Aug 10, 2020
1 parent b5169c9 commit e5afd53
Show file tree
Hide file tree
Showing 46 changed files with 75 additions and 8,774 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
os: osx
language: swift
osx_image: xcode10
osx_image: xcode11
script:
- xcodebuild -scheme AppStoreVersion -workspace AppStoreVersion.xcworkspace -sdk iphonesimulator12.0
- xcodebuild -scheme AppStoreVersion -project AppStoreVersion.xcodeproj -sdk iphonesimulator13.0
3 changes: 1 addition & 2 deletions AppStoreVersion.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "AppStoreVersion"
s.version = "1.0.1"
s.version = "1.0.2"
s.summary = "An easy Swift framework to check your latest app version available on the AppStore and compare it to the installed version"
s.description = <<-DESC
An easy Swift framework to check your latest app version available on the AppStore and compare it to the installed version.
Expand All @@ -12,5 +12,4 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/iMac0de/AppStoreVersion.git", :tag => "#{s.version}" }
s.source_files = "AppStoreVersion", "AppStoreVersion/*.{h,m,swift}"
s.resource_bundle = { "Localizable" => "AppStoreVersion/*.lproj/*.strings" }
s.dependency "Alamofire"
end
48 changes: 4 additions & 44 deletions AppStoreVersion.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
72E82A3F8B4A27B95F8B478B /* Pods_AppStoreVersion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A3BF646CC4621305B3776AB6 /* Pods_AppStoreVersion.framework */; };
D5B849F9218203C6006EF315 /* AppStoreVersion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5B849EF218203C5006EF315 /* AppStoreVersion.framework */; };
D5B84A00218203C6006EF315 /* AppStoreVersion.h in Headers */ = {isa = PBXBuildFile; fileRef = D5B849F2218203C5006EF315 /* AppStoreVersion.h */; settings = {ATTRIBUTES = (Public, ); }; };
D5B84A0B2182046F006EF315 /* AppStoreVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5B84A0A2182046F006EF315 /* AppStoreVersion.swift */; };
Expand All @@ -25,11 +24,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0BEA609445A9E1B91B6A036D /* Pods-AppStoreVersion.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppStoreVersion.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AppStoreVersion/Pods-AppStoreVersion.debug.xcconfig"; sourceTree = "<group>"; };
20268B6886596A84AC13F546 /* Pods_AppVersionChecker.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AppVersionChecker.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2B64D09D4B8DE4313772D127 /* Pods_AppVersionCheckerTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AppVersionCheckerTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
49AF80AEFEA48493EE661A9C /* Pods-AppVersionChecker.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppVersionChecker.release.xcconfig"; path = "Pods/Target Support Files/Pods-AppVersionChecker/Pods-AppVersionChecker.release.xcconfig"; sourceTree = "<group>"; };
9C9D33D47611D89B82C8D110 /* Pods-AppStoreVersion.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppStoreVersion.release.xcconfig"; path = "Pods/Target Support Files/Pods-AppStoreVersion/Pods-AppStoreVersion.release.xcconfig"; sourceTree = "<group>"; };
A3BF646CC4621305B3776AB6 /* Pods_AppStoreVersion.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AppStoreVersion.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D5B849EF218203C5006EF315 /* AppStoreVersion.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AppStoreVersion.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D5B849F2218203C5006EF315 /* AppStoreVersion.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppStoreVersion.h; sourceTree = "<group>"; };
Expand All @@ -38,15 +34,13 @@
D5B84A0A2182046F006EF315 /* AppStoreVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStoreVersion.swift; sourceTree = "<group>"; };
D5B84A0F21820B84006EF315 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
D5B84A1121820B96006EF315 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/Localizable.strings; sourceTree = "<group>"; };
DB3761010F32D0998F27EE3F /* Pods-AppVersionChecker.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppVersionChecker.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AppVersionChecker/Pods-AppVersionChecker.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
D5B849EC218203C5006EF315 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
72E82A3F8B4A27B95F8B478B /* Pods_AppStoreVersion.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -71,23 +65,11 @@
name = Frameworks;
sourceTree = "<group>";
};
C8D310C554C3063CC38C6E62 /* Pods */ = {
isa = PBXGroup;
children = (
DB3761010F32D0998F27EE3F /* Pods-AppVersionChecker.debug.xcconfig */,
49AF80AEFEA48493EE661A9C /* Pods-AppVersionChecker.release.xcconfig */,
0BEA609445A9E1B91B6A036D /* Pods-AppStoreVersion.debug.xcconfig */,
9C9D33D47611D89B82C8D110 /* Pods-AppStoreVersion.release.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
};
D5B849E5218203C5006EF315 = {
isa = PBXGroup;
children = (
D5B849F1218203C5006EF315 /* AppStoreVersion */,
D5B849F0218203C5006EF315 /* Products */,
C8D310C554C3063CC38C6E62 /* Pods */,
47583B556A22FF885D0608A8 /* Frameworks */,
);
sourceTree = "<group>";
Expand Down Expand Up @@ -130,7 +112,6 @@
isa = PBXNativeTarget;
buildConfigurationList = D5B84A03218203C6006EF315 /* Build configuration list for PBXNativeTarget "AppStoreVersion" */;
buildPhases = (
D06DD8141890C6801B763E50 /* [CP] Check Pods Manifest.lock */,
D5B849EA218203C5006EF315 /* Headers */,
D5B849EB218203C5006EF315 /* Sources */,
D5B849EC218203C5006EF315 /* Frameworks */,
Expand Down Expand Up @@ -219,27 +200,6 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
D06DD8141890C6801B763E50 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-AppStoreVersion-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
D5B849EB218203C5006EF315 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down Expand Up @@ -405,7 +365,6 @@
};
D5B84A04218203C6006EF315 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 0BEA609445A9E1B91B6A036D /* Pods-AppStoreVersion.debug.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
Expand All @@ -423,18 +382,18 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = "com.jeremy-peltier.AppStoreVersion";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
D5B84A05218203C6006EF315 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 9C9D33D47611D89B82C8D110 /* Pods-AppStoreVersion.release.xcconfig */;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
Expand All @@ -452,10 +411,11 @@
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0.2;
PRODUCT_BUNDLE_IDENTIFIER = "com.jeremy-peltier.AppStoreVersion";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
10 changes: 0 additions & 10 deletions AppStoreVersion.xcworkspace/contents.xcworkspacedata

This file was deleted.

This file was deleted.

80 changes: 56 additions & 24 deletions AppStoreVersion/AppStoreVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import Foundation
import Alamofire

open class AppStoreVersion {

Expand All @@ -23,7 +22,7 @@ open class AppStoreVersion {

private static var cache: [String: Any]? = nil

struct Keys {
private struct Keys {
static let kAppStoreResultsKey = "results"
static let kAppStoreVersionKey = "version"
static let kAppStoreURLKey = "trackViewUrl"
Expand All @@ -40,7 +39,7 @@ open class AppStoreVersion {
}

public static var latestVersionAvailable: String = ""

private class func endpoint(for bundle: Bundle) -> URL? {
if let identifier = bundle.bundleIdentifier {
let base = "https://itunes.apple.com/%@/lookup?bundleId=%@"
Expand All @@ -52,6 +51,23 @@ open class AppStoreVersion {
}
return nil
}

/**
The `AppStoreVersionEnum` holds all error cases of the framework.
*/
public enum AppStoreVersionError: Error {
// This error will be returned if the request to the App Store lookup API returns an invalid response code.
case invalidAppStoreResponseCode

// This error will be returned if the framework is unable to read the JSON returned from the App Store lookup API.
case unableToReadAppStoreResponse

// This error will be returned if the framework is not able to find the mandatory keys in the JSON returned from the App Store lookup API.
case mandatoryKeysNotFound

// This error will be returned if the framework is not able to find the App Store version of your app in the JSON returned from the App Store lookup API.
case appStoreVersionNotFound
}

/**
This method can be used to check if the param bundle version is the latest version available on the AppStore corresponding to the param bundle identifier. It will manage the display of an `UIAlertController` with an `UIAlertAction` to download the latest version if needed.
Expand Down Expand Up @@ -90,7 +106,7 @@ open class AppStoreVersion {
- Parameter bundle: The bundle of the application to check. Can be retrieve with `Bundle.main`.
- Parameter completion: A completion handler which will be call when the checking is completed.
- Parameter upToDate: If `true`, the given bundle version is the latest version available on the AppStore. If `false`, a new version is available.
- Parameter error: An optional `Error` which will help you to understand why it doesn't work.
- Parameter error: An optional `AppStoreVersionError` which will help you to understand why it doesn't work.
*/
open class func check(bundle: Bundle, _ completion: @escaping (_ upToDate: Bool, _ error: Error?) -> Void) {
guard let url = endpoint(for: bundle), let currentVersion = bundle.infoDictionary!["CFBundleShortVersionString"] as? String else {
Expand All @@ -100,35 +116,51 @@ open class AppStoreVersion {
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}

Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).validate().responseJSON { (response) in

var request = URLRequest(url: url)
request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}

guard let data = data, let response = response as? HTTPURLResponse else {
completion(false, AppStoreVersionError.unableToReadAppStoreResponse)
return
}

guard (200...299) ~= response.statusCode else {
completion(false, AppStoreVersionError.invalidAppStoreResponseCode)
return
}

do {
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
completion(false, AppStoreVersionError.unableToReadAppStoreResponse)
return
}

switch response.result {
case .success:
if let data = response.result.value as? [String: Any], let results = data[Keys.kAppStoreResultsKey] as? NSArray, let result = results.firstObject as? [String: Any] {
if let appStoreVersion = result[Keys.kAppStoreVersionKey] as? String {
self.cache = result
self.latestVersionAvailable = appStoreVersion
if currentVersion >= appStoreVersion {
completion(true, nil)
} else {
completion(false, nil)
}
} else {
completion(false, nil)
}
guard let results = json[Keys.kAppStoreResultsKey] as? NSArray, let result = results.firstObject as? [String: Any] else {
completion(false, AppStoreVersionError.mandatoryKeysNotFound)
return
}

guard let appStoreVersion = result[Keys.kAppStoreVersionKey] as? String else {
completion(false, AppStoreVersionError.appStoreVersionNotFound)
return
}

if currentVersion >= appStoreVersion {
completion(true, nil)
} else {
completion(false, nil)
}
break
case .failure(let error):
completion(false, error)
break
} catch {
completion(false, AppStoreVersionError.unableToReadAppStoreResponse)
}
}
task.resume()
}

}
2 changes: 1 addition & 1 deletion AppStoreVersion/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
Expand Down
13 changes: 0 additions & 13 deletions Podfile

This file was deleted.

16 changes: 0 additions & 16 deletions Podfile.lock

This file was deleted.

19 changes: 0 additions & 19 deletions Pods/Alamofire/LICENSE

This file was deleted.

Loading

0 comments on commit e5afd53

Please sign in to comment.