Skip to content

Commit

Permalink
version comparison check
Browse files Browse the repository at this point in the history
  • Loading branch information
s4rv4d committed Aug 14, 2020
1 parent 4aa1b6e commit 32f9a0f
Show file tree
Hide file tree
Showing 7 changed files with 620 additions and 2 deletions.
16 changes: 14 additions & 2 deletions docWind.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
40DF994824B2E452001CEC67 /* DWDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40DF994724B2E452001CEC67 /* DWDateFormatter.swift */; };
40DF994A24B2F5A2001CEC67 /* AddPdfMainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40DF994924B2F5A2001CEC67 /* AddPdfMainView.swift */; };
40E387D524E65049001E23D2 /* SearchNavigation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40E387D424E65049001E23D2 /* SearchNavigation.swift */; };
40E387D724E66A6C001E23D2 /* AppstoreUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40E387D624E66A6C001E23D2 /* AppstoreUpdate.swift */; };
40E387D924E66B0F001E23D2 /* ItunesAppInfoResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40E387D824E66B0F001E23D2 /* ItunesAppInfoResult.swift */; };
40E387DB24E66B40001E23D2 /* ItunesAppInfoItunes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40E387DA24E66B40001E23D2 /* ItunesAppInfoItunes.swift */; };
40EF391C24BF5E82007BBAC0 /* ToastView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40EF391B24BF5E82007BBAC0 /* ToastView.swift */; };
40EF462C24B4ADF700D07A06 /* ConfPdfView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40EF462B24B4ADF700D07A06 /* ConfPdfView.swift */; };
40FB481724B8AE900045CF13 /* OCRTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40FB481624B8AE900045CF13 /* OCRTextView.swift */; };
Expand Down Expand Up @@ -183,6 +186,9 @@
40DF994724B2E452001CEC67 /* DWDateFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DWDateFormatter.swift; sourceTree = "<group>"; };
40DF994924B2F5A2001CEC67 /* AddPdfMainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddPdfMainView.swift; sourceTree = "<group>"; };
40E387D424E65049001E23D2 /* SearchNavigation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchNavigation.swift; sourceTree = "<group>"; };
40E387D624E66A6C001E23D2 /* AppstoreUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppstoreUpdate.swift; sourceTree = "<group>"; };
40E387D824E66B0F001E23D2 /* ItunesAppInfoResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItunesAppInfoResult.swift; sourceTree = "<group>"; };
40E387DA24E66B40001E23D2 /* ItunesAppInfoItunes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItunesAppInfoItunes.swift; sourceTree = "<group>"; };
40EF391B24BF5E82007BBAC0 /* ToastView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastView.swift; sourceTree = "<group>"; };
40EF462B24B4ADF700D07A06 /* ConfPdfView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfPdfView.swift; sourceTree = "<group>"; };
40FB481624B8AE900045CF13 /* OCRTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OCRTextView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -287,6 +293,9 @@
4012033B24BDE0D700167917 /* NonSelectableView.swift */,
40DD4F7424C02F3700D37EFA /* ImageAnnotation.swift */,
40DD4F7624C087AE00D37EFA /* ImagePanGestureRecognizer.swift */,
40E387D624E66A6C001E23D2 /* AppstoreUpdate.swift */,
40E387D824E66B0F001E23D2 /* ItunesAppInfoResult.swift */,
40E387DA24E66B40001E23D2 /* ItunesAppInfoItunes.swift */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -590,6 +599,7 @@
40C5140324AE0D2900126ABA /* DWFMAppSettings.swift in Sources */,
4069EF9F24D69AAF003083C6 /* ViewControllerResolver.swift in Sources */,
40CFB05124D9B1E500955957 /* ErrorView.swift in Sources */,
40E387D724E66A6C001E23D2 /* AppstoreUpdate.swift in Sources */,
40485D8A24B98C9F008A137A /* TextView.swift in Sources */,
40DD4F7324C02CDC00D37EFA /* SignaturePageView.swift in Sources */,
40CFB04D24D9794100955957 /* DocumentPickerView.swift in Sources */,
Expand All @@ -608,6 +618,7 @@
405EFDC024BC886A0070BE5F /* SettingsRow.swift in Sources */,
405EFDBE24BC76EB0070BE5F /* IAPService.swift in Sources */,
4012033424BDD9DF00167917 /* ODVThumbnailView.swift in Sources */,
40E387DB24E66B40001E23D2 /* ItunesAppInfoItunes.swift in Sources */,
40FDE49A24AC4C95006AAFD2 /* AppSettings.swift in Sources */,
40CDC4CA24AF72CD00462277 /* PdfFilemodel+CoreDataProperties.swift in Sources */,
40DF994A24B2F5A2001CEC67 /* AddPdfMainView.swift in Sources */,
Expand All @@ -630,6 +641,7 @@
405EFDC624BC9ED90070BE5F /* AppVersionRow.swift in Sources */,
402C0B7224B0B0FF00A5A6C9 /* ScanPdfView.swift in Sources */,
40920EB024B744BE00E2293C /* DrawOnImageView.swift in Sources */,
40E387D924E66B0F001E23D2 /* ItunesAppInfoResult.swift in Sources */,
40C5140524AE14C000126ABA /* SearchBarView.swift in Sources */,
40CF63E224CD565F0089D3D3 /* Reachability.swift in Sources */,
409B927F24E43E6300F30EFF /* ActivityIndicatorView.swift in Sources */,
Expand Down Expand Up @@ -792,7 +804,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = docWind/docWind.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 7;
CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_ASSET_PATHS = "\"docWind/Preview Content\"";
DEVELOPMENT_TEAM = N4FDL334LZ;
ENABLE_PREVIEWS = YES;
Expand All @@ -816,7 +828,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = docWind/docWind.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 7;
CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_ASSET_PATHS = "\"docWind/Preview Content\"";
DEVELOPMENT_TEAM = N4FDL334LZ;
ENABLE_PREVIEWS = YES;
Expand Down
116 changes: 116 additions & 0 deletions docWind/Classes/AppstoreUpdate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// AppstoreUpdate.swift
// docWind
//
// Created by Sarvad shetty on 8/14/20.
// Copyright © 2020 Sarvad shetty. All rights reserved.
//

import UIKit

enum CustomError: Error {
case jsonReading
case invalidIdentifires
case invalidURL
case invalidVersion
case invalidAppName
}

class AppStoreUpdate: NSObject {

static let shared = AppStoreUpdate()

func showAppStoreVersionUpdateAlert(isForceUpdate: Bool) {

do {
//Get Bundle Identifire from Info.plist
guard let bundleIdentifire = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String else {
print("No Bundle Info found.")
throw CustomError.invalidIdentifires
}

// Build App Store URL
guard let url = URL(string:"http://itunes.apple.com/lookup?bundleId=" + bundleIdentifire) else {
print("Isse with generating URL.")
throw CustomError.invalidURL
}

let serviceTask = URLSession.shared.dataTask(with: url) { (responseData, response, error) in

do {
// Check error
if let error = error { throw error }
//Parse response
guard let data = responseData else { throw CustomError.jsonReading }
let result = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
let itunes = ItunesAppInfoItunes.init(fromDictionary: result as! [String : Any])
print(itunes.results!)
if let itunesResult = itunes.results.first {
print("App Store Varsion: ",itunesResult.version!)

//Get Bundle Version from Info.plist
guard let appShortVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
print("No Short Version Info found.")
throw CustomError.invalidVersion
}

if appShortVersion >= itunesResult.version {
//App Store & Local App Have same Version.
print("Same Version at both side or maybe this version is higher")
} else {
//Show Update alert
var message = ""
//Get Bundle Version from Info.plist
if let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String {
message = "\(appName) has new version(\(itunesResult.version!)) available on App Store."
} else {
message = "This app has new version(\(itunesResult.version!)) available on App Store."
}

//Show Alert on main thread
DispatchQueue.main.async {
self.showUpdateAlert(message: message, appStoreURL: itunesResult.trackViewUrl, isForceUpdate: isForceUpdate)
}
}
}
} catch {
print(error)
}
}
serviceTask.resume()
} catch {
print(error)
}
}

func showUpdateAlert(message : String, appStoreURL: String, isForceUpdate: Bool) {

let controller = UIAlertController(title: "New Version", message: message, preferredStyle: .alert)

//Optional Button
if !isForceUpdate {
controller.addAction(UIAlertAction(title: "Later", style: .cancel, handler: { (_) in }))
}

controller.addAction(UIAlertAction(title: "Update", style: .default, handler: { (_) in
guard let url = URL(string: appStoreURL) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}

}))

// let applicationDelegate = UIApplication.shared.delegate as? AppDelegate
// applicationDelegate?.window?.rootViewController?.present(controller, animated: true)

guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let sceneDelegate = windowScene.delegate as? SceneDelegate
else { return }
sceneDelegate.window?.rootViewController?.present(controller, animated: true)

}
}
74 changes: 74 additions & 0 deletions docWind/Classes/ItunesAppInfoItunes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// ItunesAppInfoItunes.swift
// docWind
//
// Created by Sarvad shetty on 8/14/20.
// Copyright © 2020 Sarvad shetty. All rights reserved.
//

import Foundation


class ItunesAppInfoItunes : NSObject, NSCoding{

var resultCount : Int!
var results : [ItunesAppInfoResult]!


/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
resultCount = dictionary["resultCount"] as? Int
results = [ItunesAppInfoResult]()
if let resultsArray = dictionary["results"] as? [[String:Any]]{
for dic in resultsArray{
let value = ItunesAppInfoResult(fromDictionary: dic)
results.append(value)
}
}
}

/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if resultCount != nil{
dictionary["resultCount"] = resultCount
}
if results != nil{
var dictionaryElements = [[String:Any]]()
for resultsElement in results {
dictionaryElements.append(resultsElement.toDictionary())
}
dictionary["results"] = dictionaryElements
}
return dictionary
}

/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
@objc required init(coder aDecoder: NSCoder)
{
resultCount = aDecoder.decodeObject(forKey: "resultCount") as? Int
results = aDecoder.decodeObject(forKey: "results") as? [ItunesAppInfoResult]
}

/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
@objc func encode(with aCoder: NSCoder)
{
if resultCount != nil{
aCoder.encode(resultCount, forKey: "resultCount")
}
if results != nil{
aCoder.encode(results, forKey: "results")
}
}
}
Loading

0 comments on commit 32f9a0f

Please sign in to comment.