Skip to content

Commit

Permalink
접속 기록을 파일에 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
taggon committed Jan 12, 2017
1 parent cd3dca2 commit a3b09d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
19 changes: 16 additions & 3 deletions Coffeefy/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import MASPreferences
import ReachabilitySwift
import CoreWLAN
import Alamofire
import CocoaLumberjack

public let PostResultNotification = NSNotification.Name("PostResultNotification")
public let ddLogLevel: DDLogLevel = DDLogLevel.info

let keyIgnoreVersion = "Ignore Version"

Expand Down Expand Up @@ -40,6 +42,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let bot = LoginBot()

func applicationDidFinishLaunching(_ aNotification: Notification) {
// initiate logger
#if DEBUG
DDLog.add(DDTTYLogger.sharedInstance())
#else
let fileLogger = DDFileLogger()
fileLogger?.rollingFrequency = 7 * 60 * 60 * 24 // a week
fileLogger?.logFileManager.maximumNumberOfLogFiles = 1
DDLog.add(fileLogger)
#endif

// status menu
statusItem.menu = statusMenu
loadStatusImage(name: "coffeefy3")

Expand All @@ -53,7 +66,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
do {
try reachability.startNotifier()
} catch {
NSLog("Could not start reachability notifier")
DDLogWarn("Could not start reachability notifier")
}
}

Expand All @@ -67,12 +80,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

// 스타벅스 SSID일 때만 반응
let ssid = CWWiFiClient()?.interface(withName: nil)?.ssid() ?? ""
let ssid = CWWiFiClient()?.interface(withName: nil)?.ssid() ?? "[Empty SSID]"
if ssid.lowercased().contains("starbucks") {
startAnimatingStatusImage()
bot.login()
} else {
NSLog("This application works only with Starbucks Wifi network")
DDLogWarn("Unknown SSID - \(ssid) is not Starbucks Wifi network")
}
}

Expand Down
14 changes: 11 additions & 3 deletions Coffeefy/LoginBot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Cocoa
import WebKit
import Alamofire
import CocoaLumberjack

let messageHandlerKey = "coffeefy"
let firstURL = "http://first.wifi.olleh.com/starbucks/index_en_new.html"
Expand All @@ -26,6 +27,7 @@ class LoginBot: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
self.initWebview(wv)
return wv
}()

var branch = ""

func initWebview(_ webView: WKWebView) {
Expand All @@ -47,6 +49,7 @@ class LoginBot: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
Alamofire.request(firstURL).responseString { response in
if let content = response.result.value {
if !content.hasPrefix("<script") {
DDLogInfo("Moving to Google...")
// 다른 사이트로 먼저 이동해야 접속이 이루어짐 HTTP 프로토콜 필수
self.webview.load( URLRequest(url: URL(string: "http://google.com")!) )
// self.webview.load( URLRequest(url: URL(string: firstURL)!) ) // 에러가 발생하는 사례
Expand All @@ -55,7 +58,7 @@ class LoginBot: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
}
}

NSLog("This application works only with Startbucks Wifi network.")
DDLogWarn("This application works only with Startbucks Wifi network.")
self.postResult(success: false)
}
}
Expand All @@ -67,8 +70,8 @@ class LoginBot: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
let content = try String(contentsOfFile: path!, encoding: .utf8)
return content
} catch let error {
NSLog("Error loading contentsOf url \(path)")
NSLog(error.localizedDescription)
DDLogError("Error loading contentsOf url \(path)")
DDLogError(error.localizedDescription)
}

return ""
Expand Down Expand Up @@ -97,11 +100,15 @@ class LoginBot: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
let name = UserDefaults.standard.string(forKey: "name") ?? ""
let email = UserDefaults.standard.string(forKey: "email") ?? ""
let phone = UserDefaults.standard.string(forKey: "phone") ?? ""

webview.evaluateJavaScript("coffeefy({name:'\(name)', email:'\(email)', phone: '\(phone)', branch: '\(self.branch)'})", completionHandler: nil)
DDLogInfo("Trying to sign in... (name=\(name), email=\(email))")
} else if body.hasPrefix("alert:") {
let errorMessage = body.substring(from: body.characters.index(body.characters.startIndex, offsetBy: 6))
notify(title: "인증 에러", subtitle: errorMessage, informativeText: nil)
postResult(success: false)

DDLogError("Authorization failed: \(errorMessage)")
}
}

Expand All @@ -114,6 +121,7 @@ class LoginBot: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
if host == "www.istarbucks.co.kr" || host.hasPrefix("www.google.") {
webView.stopLoading()
notify(title: "접속 성공", subtitle: "이제 스타벅스 와이파이를 사용할 수 있습니다.", informativeText: nil)
DDLogInfo("Successfully connected!")
postResult(success: true)
}
}
Expand Down
1 change: 1 addition & 0 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ target 'Coffeefy' do
pod 'MASPreferences'
pod 'ReachabilitySwift'
pod 'Alamofire', '~> 4.0'
pod 'CocoaLumberjack/Swift'
end

0 comments on commit a3b09d5

Please sign in to comment.