diff --git a/StudyCK/StudyCK.xcodeproj/project.pbxproj b/StudyCK/StudyCK.xcodeproj/project.pbxproj index a082d22..d77adf9 100644 --- a/StudyCK/StudyCK.xcodeproj/project.pbxproj +++ b/StudyCK/StudyCK.xcodeproj/project.pbxproj @@ -8,20 +8,22 @@ /* Begin PBXBuildFile section */ 4F2F116220EDB07B0074B9A6 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F2F116120EDB07B0074B9A6 /* AppDelegate.swift */; }; - 4F2F116420EDB07B0074B9A6 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F2F116320EDB07B0074B9A6 /* ViewController.swift */; }; + 4F2F116420EDB07B0074B9A6 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F2F116320EDB07B0074B9A6 /* LoginViewController.swift */; }; 4F2F116720EDB07B0074B9A6 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4F2F116520EDB07B0074B9A6 /* Main.storyboard */; }; 4F2F116920EDB07E0074B9A6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4F2F116820EDB07E0074B9A6 /* Assets.xcassets */; }; 4F2F116C20EDB07E0074B9A6 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4F2F116A20EDB07E0074B9A6 /* LaunchScreen.storyboard */; }; + 4F2F117420EF0E5B0074B9A6 /* LoginViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F2F117320EF0E5B0074B9A6 /* LoginViewModel.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ 4F2F115E20EDB07B0074B9A6 /* StudyCK.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = StudyCK.app; sourceTree = BUILT_PRODUCTS_DIR; }; 4F2F116120EDB07B0074B9A6 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 4F2F116320EDB07B0074B9A6 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + 4F2F116320EDB07B0074B9A6 /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = ""; }; 4F2F116620EDB07B0074B9A6 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 4F2F116820EDB07E0074B9A6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 4F2F116B20EDB07E0074B9A6 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 4F2F116D20EDB07E0074B9A6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 4F2F117320EF0E5B0074B9A6 /* LoginViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewModel.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -55,7 +57,8 @@ isa = PBXGroup; children = ( 4F2F116120EDB07B0074B9A6 /* AppDelegate.swift */, - 4F2F116320EDB07B0074B9A6 /* ViewController.swift */, + 4F2F116320EDB07B0074B9A6 /* LoginViewController.swift */, + 4F2F117320EF0E5B0074B9A6 /* LoginViewModel.swift */, 4F2F116520EDB07B0074B9A6 /* Main.storyboard */, 4F2F116820EDB07E0074B9A6 /* Assets.xcassets */, 4F2F116A20EDB07E0074B9A6 /* LaunchScreen.storyboard */, @@ -135,8 +138,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 4F2F116420EDB07B0074B9A6 /* ViewController.swift in Sources */, + 4F2F116420EDB07B0074B9A6 /* LoginViewController.swift in Sources */, 4F2F116220EDB07B0074B9A6 /* AppDelegate.swift in Sources */, + 4F2F117420EF0E5B0074B9A6 /* LoginViewModel.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/StudyCK/StudyCK.xcodeproj/project.xcworkspace/xcuserdata/secminhr.xcuserdatad/UserInterfaceState.xcuserstate b/StudyCK/StudyCK.xcodeproj/project.xcworkspace/xcuserdata/secminhr.xcuserdatad/UserInterfaceState.xcuserstate index 1dea264..d20efcc 100644 Binary files a/StudyCK/StudyCK.xcodeproj/project.xcworkspace/xcuserdata/secminhr.xcuserdatad/UserInterfaceState.xcuserstate and b/StudyCK/StudyCK.xcodeproj/project.xcworkspace/xcuserdata/secminhr.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/StudyCK/StudyCK.xcodeproj/xcuserdata/secminhr.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/StudyCK/StudyCK.xcodeproj/xcuserdata/secminhr.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..fe2b454 --- /dev/null +++ b/StudyCK/StudyCK.xcodeproj/xcuserdata/secminhr.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,5 @@ + + + diff --git a/StudyCK/StudyCK/Base.lproj/Main.storyboard b/StudyCK/StudyCK/Base.lproj/Main.storyboard index 03c13c2..464653f 100644 --- a/StudyCK/StudyCK/Base.lproj/Main.storyboard +++ b/StudyCK/StudyCK/Base.lproj/Main.storyboard @@ -1,24 +1,142 @@ - + + + + - + + - + + + + + + + + + + + + + + + + + + + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/StudyCK/StudyCK/Info.plist b/StudyCK/StudyCK/Info.plist index 16be3b6..5ee6264 100644 --- a/StudyCK/StudyCK/Info.plist +++ b/StudyCK/StudyCK/Info.plist @@ -2,6 +2,11 @@ + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable diff --git a/StudyCK/StudyCK/LoginViewController.swift b/StudyCK/StudyCK/LoginViewController.swift new file mode 100644 index 0000000..adf5e6e --- /dev/null +++ b/StudyCK/StudyCK/LoginViewController.swift @@ -0,0 +1,50 @@ +import UIKit + +class LoginViewController: UIViewController { + + let viewModel = LoginViewModel() + + @IBOutlet weak var loginIndicator: UIActivityIndicatorView! + + @IBAction func loginFieldChanged(_ sender: UITextField) { + if sender.tag == 0 { + viewModel.username = sender.text! + } else { + viewModel.password = sender.text! + } + } + + @IBAction func loginButtonClicked() { + viewModel.login() + } + + override func viewDidLoad() { + viewModel.showLoginFailedAlert = { (title:String, message:String) -> Void in + DispatchQueue.main.async { + let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "重試", style: .cancel, handler: nil)) + self.present(alertController, animated: true, completion: nil) + } + } + viewModel.loginStateChanged = { (loggingIn: Bool) -> Void in + DispatchQueue.main.async { + self.loginIndicator.setAnimating(to: loggingIn) + } + } + viewModel.loginSuccessfully = { + DispatchQueue.main.async { + + } + } + } +} + +extension UIActivityIndicatorView { + func setAnimating(to animating: Bool) { + if animating { + startAnimating() + } else { + stopAnimating() + } + } +} diff --git a/StudyCK/StudyCK/LoginViewModel.swift b/StudyCK/StudyCK/LoginViewModel.swift new file mode 100644 index 0000000..5138323 --- /dev/null +++ b/StudyCK/StudyCK/LoginViewModel.swift @@ -0,0 +1,44 @@ +import Foundation +class LoginViewModel { + + var username: String = "" + var password: String = "" + var loginState = false { + didSet { + self.loginStateChanged?(loginState) + } + } + + let loginURL = "http://study.ck.tp.edu.tw/login_chk.asp" + let requestBody = "f_mnuid=&f_uid=%@&f_pwd=%@&submit=%B5n%A4J" + + var showLoginFailedAlert: ((String, String) -> Void)? = nil + var loginStateChanged: ((Bool) -> Void)? = nil + var loginSuccessfully: (() -> Void)? = nil + + func login() { + loginState = true + var request = URLRequest(url: URL(string: loginURL)!) + request.httpMethod = "POST" + let cfEnc = CFStringEncodings.big5 + let nsEnc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue)) + let big5encoding = String.Encoding(rawValue: nsEnc) + let body = String(format: requestBody, username, password) + request.httpBody = body.data(using: big5encoding) + URLSession.shared.dataTask(with: request) { data, response, error in + self.handleData(string: String(data: data!, encoding: big5encoding)!) + self.loginState = false + }.resume() + } + + private func handleData(string data: String) { + print(data) + if data.contains("密碼錯誤") { + showLoginFailedAlert?("錯誤", "密碼錯誤,請再試一次") + } else if data.contains("帳號") && data.contains("不存在") { + showLoginFailedAlert?("錯誤", "帳號\(username)不存在") + } else { //login successfully + loginSuccessfully?() + } + } +} diff --git a/StudyCK/StudyCK/ViewController.swift b/StudyCK/StudyCK/ViewController.swift deleted file mode 100644 index 708cfbe..0000000 --- a/StudyCK/StudyCK/ViewController.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// ViewController.swift -// StudyCK -// -// Created by secminhr on 2018/7/5. -// Copyright © 2018年 secminhr. All rights reserved. -// - -import UIKit - -class ViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view, typically from a nib. - } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } - - -} -