From 722edad36df007251bc248aa8f1f227c7e6d8131 Mon Sep 17 00:00:00 2001 From: 127-Z3R0 <81heti1bif@hft-stuttgart.de> Date: Thu, 18 Jun 2020 20:28:01 +0200 Subject: [PATCH] Add UpdatePopUp because Pfusch and its easier instead of rewriting ModalEntity * still fixes needed with getting AppVersion from API * --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 7 ++ GoldWars/GoldWars/AppUpdater.swift | 83 +++++++++++++++++++ .../GoldWars/Entities/EntityManager.swift | 5 ++ GoldWars/GoldWars/Entities/UpdatePopUp.swift | 44 ++++++++++ GoldWars/GoldWars/Scenes/MenuScene.swift | 7 +- 5 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 GoldWars/GoldWars/AppUpdater.swift create mode 100644 GoldWars/GoldWars/Entities/UpdatePopUp.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index c286db4..441b01b 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -54,6 +54,8 @@ C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A7246C0EA50022B228 /* LabelNode.swift */; }; C064E9AA246C114C0022B228 /* LabelComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9A9246C114C0022B228 /* LabelComponent.swift */; }; C064E9AC246C151F0022B228 /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = C064E9AB246C151F0022B228 /* Label.swift */; }; + C06C71DD249BC74D00B2D63C /* AppUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = C06C71DC249BC74D00B2D63C /* AppUpdater.swift */; }; + C06C71DF249BDADF00B2D63C /* UpdatePopUp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C06C71DE249BDADF00B2D63C /* UpdatePopUp.swift */; }; C099579C246C5E5C0016AA22 /* DataService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C099579B246C5E5C0016AA22 /* DataService.swift */; }; /* End PBXBuildFile section */ @@ -120,6 +122,8 @@ C064E9A7246C0EA50022B228 /* LabelNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelNode.swift; sourceTree = ""; }; C064E9A9246C114C0022B228 /* LabelComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelComponent.swift; sourceTree = ""; }; C064E9AB246C151F0022B228 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = ""; }; + C06C71DC249BC74D00B2D63C /* AppUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUpdater.swift; sourceTree = ""; }; + C06C71DE249BDADF00B2D63C /* UpdatePopUp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdatePopUp.swift; sourceTree = ""; }; C099579B246C5E5C0016AA22 /* DataService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataService.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -231,6 +235,7 @@ 9EEDE02C246FCD770096C735 /* SpinningLogoEntity.swift */, 3E6785412472CBEC007B9DE4 /* Way.swift */, C064E9AB246C151F0022B228 /* Label.swift */, + C06C71DE249BDADF00B2D63C /* UpdatePopUp.swift */, ); path = Entities; sourceTree = ""; @@ -422,6 +427,7 @@ 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, 3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */, + C06C71DD249BC74D00B2D63C /* AppUpdater.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */, ABC0C3732481509300387B8F /* MapUtils.swift in Sources */, @@ -430,6 +436,7 @@ 9E61EAC7249BB61A00334DDE /* SpinningLogo3DNode.swift in Sources */, 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, + C06C71DF249BDADF00B2D63C /* UpdatePopUp.swift in Sources */, C05BB9C4247D890C00411249 /* SliderComponent.swift in Sources */, 110360D3244B101A008610AF /* AppDelegate.swift in Sources */, 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */, diff --git a/GoldWars/GoldWars/AppUpdater.swift b/GoldWars/GoldWars/AppUpdater.swift new file mode 100644 index 0000000..c96b874 --- /dev/null +++ b/GoldWars/GoldWars/AppUpdater.swift @@ -0,0 +1,83 @@ +// +// AppUpdater.swift +// GoldWars +// +// Created by Tim Herbst on 18.06.20. +// Copyright © 2020 SP2. All rights reserved. +// +import GameKit + +enum VersionError: Error { + case invalidBundleInfo, invalidResponse +} + +class LookupResult: Decodable { + var results: [AppInfo] +} + +class AppInfo: Decodable { + var version: String + var trackViewUrl: String +} + +class AppUpdater: NSObject { + static var appStoreUrl = "" + static let appUpdaterInstance = AppUpdater() + var entityManager = EntityManager.menuEMInstance + + private func getAppInfo(completion: @escaping (AppInfo?, Error?) -> Void) -> URLSessionTask? { + guard let identifier = Bundle.main.infoDictionary!["CFBundleIdentifier"] as? String, + let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else { + DispatchQueue.main.async { + completion(nil, VersionError.invalidBundleInfo) + } + return nil + } + let task = URLSession.shared.dataTask(with: url) { (data, response, error) in + do { + if error != nil { throw error! } + guard let data = data else { throw VersionError.invalidResponse } + + print("Data: ", data) + print("response: ", response!) + + let result = try JSONDecoder().decode(LookupResult.self, from: data) + + let dictionary = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) + + print("dictionary: ", dictionary) + + guard let info = result.results.first else { throw VersionError.invalidResponse } + print("result: ", result) + + completion(info, nil) + } catch { + completion(nil, error) + } + } + task.resume() + print("task***",task) + return task + } + + func checkVersion() -> Bool { + let info = Bundle.main.infoDictionary + let currentVersion = info?["CFBundleShortVersionString"] as? String + var appStoreVersion: AppInfo + var isUpToDate = true + _ = getAppInfo{ (info, error) in + appStoreVersion = info!.version + print("info-version", info?.version) + if let error = error { + print("error: ",error) + } + } + print("currentVersion", currentVersion) + print("appStoreVersion", appStoreVersion) + if !(currentVersion == appStoreVersion) { + isUpToDate = false + } + print("isUptoDate", isUpToDate) + return isUpToDate + } +} diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 83e8b7e..e71cb09 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -43,6 +43,11 @@ class EntityManager { isModal = true } + if let updateEntity = entity as? UpdatePopUp { + scene.addChild(updateEntity.background) + scene.addChild(updateEntity.message) + } + if let hudEntitiy = entity as? HUD { scene.addChild(hudEntitiy.hostLabel) scene.addChild(hudEntitiy.hostUnitsLabel) diff --git a/GoldWars/GoldWars/Entities/UpdatePopUp.swift b/GoldWars/GoldWars/Entities/UpdatePopUp.swift new file mode 100644 index 0000000..bdcbc45 --- /dev/null +++ b/GoldWars/GoldWars/Entities/UpdatePopUp.swift @@ -0,0 +1,44 @@ +// +// UpdatePopUp.swift +// GoldWars +// +// Created by Tim Herbst on 18.06.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import GameplayKit + +class UpdatePopUp: GKEntity { + + var entityManager = EntityManager.menuEMInstance + var background: SKSpriteNode + var message: SKLabelNode + + init(anchorPoint: CGPoint) { + let texture = SKTexture(imageNamed: "ModalBackground") + background = SKSpriteNode(texture: texture, size: texture.size()) + background.setScale(2.4) + background.position = anchorPoint + background.zPosition = 4 + + message = SKLabelNode(text: "Update erforderlich") + self.message.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y + 60) + self.message.fontName = "HelveticaNeue-Bold" + self.message.fontSize = 40 + self.message.zPosition = 5 + + super.init() + + addComponent(ButtonComponent(textureName: "yellow_button04", text: "zum Update", position: CGPoint(x: anchorPoint.x, y: anchorPoint.y - 105), isEnabled: true, onButtonPress: { + if let url = URL(string: AppUpdater.appStoreUrl), UIApplication.shared.canOpenURL(url) { + if #available(iOS 13.2, *) { + UIApplication.shared.open(url, options: [:], completionHandler: nil) + } + } + })) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/GoldWars/GoldWars/Scenes/MenuScene.swift b/GoldWars/GoldWars/Scenes/MenuScene.swift index 19ccec5..937c1a9 100644 --- a/GoldWars/GoldWars/Scenes/MenuScene.swift +++ b/GoldWars/GoldWars/Scenes/MenuScene.swift @@ -11,6 +11,7 @@ import SceneKit class MenuScene: SKScene { var entityManager = EntityManager.menuEMInstance + var appOutdated = false override func sceneDidLoad() { GameCenterManager.sharedInstance.menusc = self @@ -28,7 +29,7 @@ class MenuScene: SKScene { } else { if GameCenterManager.isAuthenticated { - GameCenterManager.sharedInstance.presentMatchmaker() + GameCenterManager.sharedInstance.presentGameCenter() }else { GameCenterManager.sharedInstance.authUser() } @@ -70,7 +71,9 @@ class MenuScene: SKScene { override func update(_ currentTime: TimeInterval) { if entityManager.entities.count != 0 { entityManager.getBackground()!.update(deltaTime: currentTime) - entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated + if !appOutdated { + entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterManager.isAuthenticated + } } if GameCenterManager.sharedInstance.initIsFinish {