Skip to content

Commit 7e746ce

Browse files
committed
Add App, UIAlert, and View Controller Utils
1 parent 0681f28 commit 7e746ce

4 files changed

Lines changed: 359 additions & 0 deletions

File tree

Pod/Classes/AppUtils.swift

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//
2+
// AppUtils.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 5/8/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public func AppDisplayName() -> String? {
12+
13+
guard let displayName = NSBundle.mainBundle().objectForInfoDictionaryKey("CGBundleDisplayName") as? String else {
14+
15+
guard let name: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String else {
16+
17+
return nil
18+
19+
}
20+
21+
return name
22+
23+
}
24+
25+
return displayName
26+
27+
}
28+
29+
public func AppVersion() -> String? {
30+
31+
guard let version: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as? String else {
32+
33+
return nil
34+
35+
}
36+
37+
return version
38+
39+
}
40+
41+
public func AppBuildNumber() -> String? {
42+
43+
guard let build: String = NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as? String else {
44+
45+
return nil
46+
47+
}
48+
49+
return build
50+
51+
}
52+
53+
public func AppBuildVersion() -> String? {
54+
55+
guard let version: String = AppVersion(), build: String = AppBuildNumber() else {
56+
57+
return nil
58+
59+
}
60+
61+
if version == build {
62+
63+
return "v" + version
64+
65+
} else {
66+
67+
return "v" + version + "(" + build + ")"
68+
69+
}
70+
71+
}
72+
73+
public func DeviceVersion() -> String? {
74+
75+
var x = 0
76+
77+
sysctlbyname("hw.machine", nil, &x, nil, 0)
78+
79+
var device = [CChar](count: x, repeatedValue: 0)
80+
81+
sysctlbyname("hw.machine", &device, &x, nil, 0)
82+
83+
return String.fromCString(device)
84+
85+
}
86+
87+
public func DetectScreenshot(completion: () -> ()) {
88+
89+
let queue = NSOperationQueue.mainQueue()
90+
91+
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification, object: nil, queue: queue) {
92+
93+
notification in
94+
95+
completion()
96+
97+
}
98+
99+
}
100+
101+
public func IsDebug() -> Bool {
102+
103+
#if DEBUG
104+
return true
105+
#else
106+
return false
107+
#endif
108+
109+
}
110+
111+
public func IsRelease() -> Bool {
112+
113+
#if DEBUG
114+
return false
115+
#else
116+
return true
117+
#endif
118+
119+
}
120+
121+
public func IsSimulator() -> Bool {
122+
123+
#if ( (arch(i386)) || (arch(x86_64)) ) && os(iOS)
124+
return true
125+
#else
126+
return false
127+
#endif
128+
129+
}
130+
131+
132+
public func IsDevice() -> Bool {
133+
134+
#if ( (arch(i386)) || (arch(x86_64)) ) && os(iOS)
135+
return false
136+
#else
137+
return true
138+
#endif
139+
140+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// UIAlertControllerExtensions.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 4/26/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public extension UIAlertController {
12+
13+
public func show() {
14+
15+
RootViewController()?.presentViewController(self, animated: true, completion: nil)
16+
17+
}
18+
19+
public func showWithCompletion(completion: () -> Void) {
20+
21+
RootViewController()?.presentViewController(self, animated: true, completion: completion)
22+
23+
}
24+
25+
public var setVisible: Bool {
26+
27+
get {
28+
29+
return self.setVisible
30+
31+
}
32+
33+
set {
34+
35+
if newValue == true {
36+
37+
show()
38+
39+
}
40+
41+
self.setVisible = newValue
42+
43+
}
44+
45+
}
46+
47+
}

Pod/Classes/UIAlertUtils.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// UIAlertUtils.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 5/5/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public func showErrorAlert(description: String, error: NSError? = nil, sender: UIViewController) {
12+
13+
// Set Text of Alert
14+
let text: String = (error != nil) ? "Error description: \(description), error: \(error)" : "Error description: \(description)"
15+
16+
// Setup Alert Controller
17+
let alertController = UIAlertController(title: "Error", message: text, preferredStyle: .Alert)
18+
let alertAction = UIAlertAction(title: "Dismiss", style: .Default, handler: nil)
19+
alertController.addAction(alertAction)
20+
21+
// Present Alert using Sender View Controller
22+
sender.presentViewController(alertController, animated: true, completion: nil)
23+
24+
}
25+
26+
public func showErrorAlertUsingRoot(description: String, error: NSError? = nil) {
27+
28+
// Set Text of Alert
29+
let text: String = (error != nil) ? "Error description: \(description), error: \(error)" : "Error description: \(description)"
30+
31+
// Setup Alert Controller
32+
let alertController = UIAlertController(title: "Error", message: text, preferredStyle: .Alert)
33+
let alertAction = UIAlertAction(title: "Dismiss", style: .Default, handler: nil)
34+
alertController.addAction(alertAction)
35+
36+
// Present Alert using Root View Controller
37+
RootViewController()?.presentViewController(alertController, animated: true, completion: nil)
38+
39+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//
2+
// ViewControllerUtils.swift
3+
// Ahtau
4+
//
5+
// Created by Mark Hamilton on 5/8/16.
6+
// Copyright © 2016 dryverless. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public func RootViewController() -> UIViewController? {
12+
13+
guard let rootVC: UIViewController = UIApplication.sharedApplication().keyWindow?.rootViewController else {
14+
15+
return nil
16+
17+
}
18+
19+
return rootVC.presentedViewController ?? nil
20+
21+
}
22+
23+
public func ScreenOrientation() -> UIInterfaceOrientation {
24+
25+
return UIApplication.sharedApplication().statusBarOrientation
26+
27+
}
28+
29+
public func HorizontalSizeClass() -> UIUserInterfaceSizeClass {
30+
31+
guard let horizSizeClass = RootViewController()?.traitCollection.horizontalSizeClass else {
32+
33+
return UIUserInterfaceSizeClass.Unspecified
34+
35+
}
36+
37+
return horizSizeClass
38+
39+
}
40+
41+
public func VerticalSizeClass() -> UIUserInterfaceSizeClass {
42+
43+
guard let vertSizeClass = RootViewController()?.traitCollection.verticalSizeClass else {
44+
45+
return UIUserInterfaceSizeClass.Unspecified
46+
47+
}
48+
49+
return vertSizeClass
50+
51+
}
52+
53+
public func ScreenWidth() -> CGFloat {
54+
55+
if ScreenOrientation() == UIInterfaceOrientation.Portrait {
56+
57+
guard let sWidth: CGFloat = UIScreen.mainScreen().bounds.size.width else {
58+
59+
return CGFloat(0)
60+
61+
}
62+
63+
return sWidth
64+
65+
} else {
66+
67+
guard let sHeight: CGFloat = UIScreen.mainScreen().bounds.size.height else {
68+
69+
return CGFloat(0)
70+
71+
}
72+
73+
return sHeight
74+
75+
}
76+
77+
}
78+
79+
public func ScreenHeight() -> CGFloat {
80+
81+
if ScreenOrientation() == UIInterfaceOrientation.Portrait {
82+
83+
guard let sHeight: CGFloat = UIScreen.mainScreen().bounds.size.height else {
84+
85+
return CGFloat(0)
86+
87+
}
88+
89+
return sHeight
90+
91+
} else {
92+
93+
guard let sWidth: CGFloat = UIScreen.mainScreen().bounds.size.width else {
94+
95+
return CGFloat(0)
96+
97+
}
98+
99+
return sWidth
100+
101+
}
102+
103+
}
104+
105+
public func ScreenBounds() -> CGRect {
106+
107+
guard let sBounds: CGRect = UIScreen.mainScreen().bounds else {
108+
109+
return CGRect()
110+
111+
}
112+
113+
return sBounds
114+
115+
}
116+
117+
public func StatusBarHeight() -> CGFloat {
118+
119+
guard let sbHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.height else {
120+
121+
return CGFloat(0)
122+
123+
}
124+
125+
return sbHeight
126+
127+
}
128+
129+
public func ScreenHeightMinusStatusBar() -> CGFloat {
130+
131+
return ( ScreenHeight() - StatusBarHeight() )
132+
133+
}

0 commit comments

Comments
 (0)