This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
/
GlobalFunctions.swift
59 lines (48 loc) · 1.97 KB
/
GlobalFunctions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import ReactiveCocoa
import Moya
// Ideally a Pod. For now a file.
func delayToMainThread(delay:Double, closure:()->()) {
dispatch_after (
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
func logPath() -> NSURL {
let docs = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last!
return docs.URLByAppendingPathComponent("logger.txt")
}
let logger = Logger(destination: logPath())
let reachabilityManager = ReachabilityManager()
// A signal that completes when the app gets online (possibly completes immediately).
func connectedToInternetSignal() -> RACSignal {
return reachabilityManager.reachSignal.filter { ($0 as! Bool) }.take(1).ignoreValues()
}
func responseIsOK(object: AnyObject!) -> AnyObject {
if let response = object as? MoyaResponse {
return response.statusCode == 200
}
return false
}
// Adapted from https://github.com/FUKUZAWA-Tadashi/FHCCommander/blob/67c67757ee418a106e0ce0c0820459299b3d77bb/fhcc/Convenience.swift#L33-L44
func getSSID() -> String? {
let interfaces: CFArray! = CNCopySupportedInterfaces()
if interfaces == nil { return nil }
let if0: UnsafePointer<Void>? = CFArrayGetValueAtIndex(interfaces, 0)
if if0 == nil { return nil }
let interfaceName: CFStringRef = unsafeBitCast(if0!, CFStringRef.self)
let dictionary = CNCopyCurrentNetworkInfo(interfaceName) as NSDictionary?
if dictionary == nil { return nil }
return dictionary?[kCNNetworkInfoKeySSID as String] as? String
}
/// Looks for a connection to an Artsy WiFi network.
func detectDevelopmentEnvironment() -> Bool {
var developmentEnvironment = false
#if (arch(i386) || arch(x86_64)) && os(iOS)
developmentEnvironment = true
#else
developmentEnvironment = getSSID()?.lowercaseString.containsString("artsy") ?? false
#endif
return developmentEnvironment
}