Commit 479e48297ccb59174b564486a73b24ba4e1ced2c

Authored by 葛建军
1 parent 10d1b272
Exists in parentassistant

版本更新提醒。

ParentAssistant/ParentAssistant/Classes/controllers/main/TabBarController.swift
... ... @@ -12,6 +12,7 @@ class TabBarController: UITabBarController {
12 12  
13 13 override func viewDidLoad() {
14 14 super.viewDidLoad()
  15 + updataVersion()
15 16 }
16 17  
17 18 override func didReceiveMemoryWarning() {
... ... @@ -29,5 +30,116 @@ class TabBarController: UITabBarController {
29 30 // Pass the selected object to the new view controller.
30 31 }
31 32 */
  33 + func updataVersion(_ isRemain:Bool=false){
  34 + let request = NSMutableURLRequest(url: URL(string: "http://itunes.apple.com/lookup?id=1357945086")!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 10)
  35 + request.httpMethod = "POST"
  36 + // NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main, completionHandler: { (respones, data, error) in
  37 + let config=URLSessionConfiguration.default
  38 + let session=URLSession(configuration: config, delegate: self, delegateQueue: nil)
  39 + let dataTask=session.dataTask(with: request as URLRequest, completionHandler: { (data, response, sessionError) in
  40 + if let datas = data{
  41 + do{
  42 + let dictionary = try JSONSerialization.jsonObject(with: datas, options: JSONSerialization.ReadingOptions.mutableLeaves)
  43 + if ((dictionary as! NSDictionary)["resultCount"] as AnyObject).int32Value > 0{
  44 + let dataInformatica = ((dictionary as! NSDictionary)["results"] as! NSArray)[0] as! NSDictionary
  45 + let version = dataInformatica["version"]! as! String
  46 + if UIApplication.appVersion() < version{
  47 + let alert = UIAlertController(title: "发现新版本", message: dataInformatica["releaseNotes"]! as? String, preferredStyle: .alert)
  48 + alert.addAction(UIAlertAction(title: "前往更新", style: .default, handler: { (action) in
  49 + // UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id1271291794")!)
  50 + UIApplication.shared.open(URL(string: "itms-apps://itunes.apple.com/app/id1357945086")!, options: [:], completionHandler: nil)
  51 + }))
  52 + alert.addAction(UIAlertAction(title: "以后再说", style: .cancel, handler: { (action) in
  53 +
  54 + }))
  55 + self.present(alert, animated: true, completion: nil)
  56 + }else{
  57 + if isRemain{
  58 + appDelegate.window?.makeToast("已经是最新版本")
  59 + }
  60 + NSLog("不需要更新")
  61 + }
  62 + }
  63 + }
  64 + catch let error as NSError {
  65 + NSLog("请求失败")
  66 + appDelegate.window?.makeToast(error.localizedDescription)
  67 + }
  68 + }
  69 + })
  70 + dataTask.resume()
  71 + }
  72 +}
  73 +extension TabBarController: URLSessionDelegate {
  74 + func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  75 + //认证服务器证书
  76 + if challenge.protectionSpace.authenticationMethod
  77 + == (NSURLAuthenticationMethodServerTrust) {
  78 + print("服务端证书认证!")
  79 + completionHandler(.performDefaultHandling, nil)
  80 + }
  81 + //认证客户端证书
  82 + else if challenge.protectionSpace.authenticationMethod
  83 + == NSURLAuthenticationMethodClientCertificate
  84 + {
  85 + print("客户端证书认证!")
  86 + //获取客户端证书相关信息
  87 + let identityAndTrust:IdentityAndTrust = self.extractIdentity();
  88 +
  89 + let urlCredential:URLCredential = URLCredential(
  90 + identity: identityAndTrust.identityRef,
  91 + certificates: identityAndTrust.certArray as? [AnyObject],
  92 + persistence: URLCredential.Persistence.forSession);
  93 +
  94 + completionHandler(.useCredential, urlCredential);
  95 + }
  96 +
  97 + // 其它情况(不接受认证)
  98 + else {
  99 + print("其它情况(不接受认证)")
  100 + completionHandler(.cancelAuthenticationChallenge, nil);
  101 + }
  102 + }
  103 + //获取客户端证书相关信息
  104 + func extractIdentity() -> IdentityAndTrust {
  105 + var identityAndTrust:IdentityAndTrust!
  106 + var securityError:OSStatus = errSecSuccess
  107 +
  108 + let path: String = Bundle.main.path(forResource: "pub", ofType: "cer")!
  109 + let PKCS12Data = try! Data(contentsOf: URL(fileURLWithPath: path))
  110 + let key : NSString = kSecImportExportPassphrase as NSString
  111 + let options : NSDictionary = [key : "123456"] //客户端证书密码
  112 + //create variable for holding security information
  113 + //var privateKeyRef: SecKeyRef? = nil
  114 +
  115 + var items : CFArray?
  116 +
  117 + securityError = SecPKCS12Import(PKCS12Data as CFData, options, &items)
  118 +
  119 + if securityError == errSecSuccess {
  120 + let certItems:CFArray = items as CFArray!;
  121 + let certItemsArray:Array = certItems as Array
  122 + let dict:AnyObject? = certItemsArray.first;
  123 + if let certEntry:Dictionary = dict as? Dictionary<String, AnyObject> {
  124 + // grab the identity
  125 + let identityPointer:AnyObject? = certEntry["identity"];
  126 + let secIdentityRef:SecIdentity = identityPointer as! SecIdentity!;
  127 + // grab the trust
  128 + let trustPointer:AnyObject? = certEntry["trust"];
  129 + let trustRef:SecTrust = trustPointer as! SecTrust;
  130 + // grab the cert
  131 + let chainPointer:AnyObject? = certEntry["chain"];
  132 + identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef,
  133 + trust: trustRef, certArray: chainPointer!);
  134 + }
  135 + }
  136 + return identityAndTrust;
  137 + }
  138 +}
32 139  
  140 +//定义一个结构体,存储认证相关信息
  141 +struct IdentityAndTrust {
  142 + var identityRef:SecIdentity
  143 + var trust:SecTrust
  144 + var certArray:AnyObject
33 145 }
... ...
ParentAssistant/ParentAssistant/Supporting Files/Info.plist
... ... @@ -19,7 +19,7 @@
19 19 <key>CFBundleShortVersionString</key>
20 20 <string>1.0.0</string>
21 21 <key>CFBundleVersion</key>
22   - <string>7</string>
  22 + <string>8</string>
23 23 <key>LSRequiresIPhoneOS</key>
24 24 <true/>
25 25 <key>NSAppTransportSecurity</key>
... ...