mirror of
https://github.com/qinyuhang/ShadowsocksX-NG-R.git
synced 2026-10-21 23:34:14 +00:00
possible bug: when the NSAlert view is pop up click the menu bar may cause crash, digging bug.
107 lines
4.2 KiB
Swift
107 lines
4.2 KiB
Swift
//
|
|
// VersionChecker.swift
|
|
// ShadowsocksX-NG
|
|
//
|
|
// Created by 秦宇航 on 2017/1/9.
|
|
// Copyright © 2017年 qinyuhang. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
let _VERSION_XML_URL = "https://raw.githubusercontent.com/shadowsocksr/ShadowsocksX-NG/develop/ShadowsocksX-NG/Info.plist"
|
|
let _VERSION_XML_LOCAL:String = Bundle.main.bundlePath + "/Contents/Info.plist"
|
|
|
|
class VersionChecker: NSObject {
|
|
var haveNewVersion: Bool = false
|
|
enum versionError: Error {
|
|
case CanNotGetOnlineData
|
|
}
|
|
func saveFile(fromURL: String, toPath: String, withName: String) -> Bool {
|
|
let manager = FileManager.default
|
|
let url = URL(string:fromURL)!
|
|
do {
|
|
let st = try String(contentsOf: url, encoding: String.Encoding.utf8)
|
|
print(st)
|
|
let data = st.data(using: String.Encoding.utf8)
|
|
manager.createFile( atPath: toPath + withName , contents: data, attributes: nil)
|
|
return true
|
|
|
|
} catch {
|
|
print(error)
|
|
return false
|
|
}
|
|
}
|
|
func showAlertView(Title: String, SubTitle: String, ConfirmBtn: String, CancelBtn: String) -> Int {
|
|
let alertView = NSAlert()
|
|
alertView.messageText = Title
|
|
alertView.informativeText = SubTitle
|
|
alertView.addButton(withTitle: ConfirmBtn)
|
|
if CancelBtn != "" {
|
|
alertView.addButton(withTitle: CancelBtn)
|
|
}
|
|
let action = alertView.runModal()
|
|
return action
|
|
}
|
|
func checkNewVersion() -> [String:Any] {
|
|
// return
|
|
// newVersion: Bool,
|
|
// error: String,
|
|
// alertTitle: String,
|
|
// alertSubtitle: String,
|
|
// alertConfirmBtn: String,
|
|
// alertCancelBtn: String
|
|
let showAlert: Bool = true
|
|
func getOnlineData() throws -> NSDictionary{
|
|
guard NSDictionary(contentsOf: URL(string:_VERSION_XML_URL)!) != nil else {
|
|
throw versionError.CanNotGetOnlineData
|
|
}
|
|
return NSDictionary(contentsOf: URL(string:_VERSION_XML_URL)!)!
|
|
}
|
|
|
|
var localData: NSDictionary = NSDictionary()
|
|
var onlineData: NSDictionary = NSDictionary()
|
|
|
|
localData = NSDictionary(contentsOfFile: _VERSION_XML_LOCAL)!
|
|
do{
|
|
try onlineData = getOnlineData()
|
|
}catch{
|
|
return ["newVersion" : false,
|
|
"error": "network error",
|
|
"Title": "网络错误",
|
|
"SubTitle": "由于网络错误无法检查更新",
|
|
"ConfirmBtn": "确认",
|
|
"CancelBtn": ""
|
|
]
|
|
}
|
|
if (onlineData["CFBundleShortVersionString"] as! String == localData["CFBundleShortVersionString"] as! String && onlineData["CFBundleVersion"] as! String == localData["CFBundleVersion"] as! String){
|
|
|
|
let currentVersionString:String = localData["CFBundleShortVersionString"] as! String
|
|
let currentBuildString:String = localData["CFBundleVersion"] as! String
|
|
let subtitle = "当前版本 " + currentVersionString + " build " + currentBuildString
|
|
return ["newVersion" : false,
|
|
"error": "",
|
|
"Title": "已是最新版本!",
|
|
"SubTitle": subtitle,
|
|
"ConfirmBtn": "确认",
|
|
"CancelBtn": ""
|
|
]
|
|
}
|
|
else{
|
|
haveNewVersion = true
|
|
|
|
let versionString:String = onlineData["CFBundleShortVersionString"] as! String
|
|
let buildString:String = onlineData["CFBundleVersion"] as! String
|
|
let currentVersionString:String = localData["CFBundleShortVersionString"] as! String
|
|
let currentBuildString:String = localData["CFBundleVersion"] as! String
|
|
let subtitle = "新版本为 " + versionString + " build " + buildString + "\n" + "当前版本 " + currentVersionString + " build " + currentBuildString
|
|
return ["newVersion" : true,
|
|
"error": "",
|
|
"Title": "软件有更新!",
|
|
"SubTitle": subtitle,
|
|
"ConfirmBtn": "前往下载",
|
|
"CancelBtn": "取消"
|
|
]
|
|
}
|
|
}
|
|
}
|