-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bundle+Extensions.swift
159 lines (133 loc) · 6.19 KB
/
Bundle+Extensions.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Bundle+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import Foundation
extension Bundle {
/// `Bundle.main.oy_langugage`→ output → "en"
public var oy_langugage: String? {
UserDefaults.standard.stringArray(forKey: "AppleLanguages")?.first
}
/// `Bundle.main.oy_appName`→ output → "OYExtensions"
public var oy_appName: String? { oy_infoPlistValue(key: "CFBundleName") as? String }
/// `Bundle.main.oy_targetName`→ output → "OYExtensionsStage"
public var oy_targetName: String? { oy_infoPlistValue(key: "TargetName") as? String }
/// `Bundle.main.oy_displayName`→ output → "OYExtensions"
public var oy_displayName: String? { oy_infoPlistValue(key: "CFBundleDisplayName") as? String }
/// `Bundle.main.oy_appVersionNumber`→ output → "1.0"
public var oy_appVersionNumber: String? { oy_infoPlistValue(key: "CFBundleShortVersionString") as? String }
/// `Bundle.main.oy_appStoreVersion(bundleId: "com.oy.extensions") { value in
/// value → "1.0.6"
/// `}
public func oy_appStoreVersion(bundleId: String, completion: @escaping ((String?) -> Void)) {
guard let url = URL(string: "http://itunes.apple.com/in/lookup?bundleId=\(bundleId)") else {
completion(nil)
return
}
let task = URLSession.shared.dataTask(with: URLRequest(url: url)) { data, _, _ in
guard let data = try? Data(contentsOf: url) else {
completion(nil)
return
}
guard let lookupResults = try? data.oy_dictionary() else {
completion(nil)
return
}
guard let resultCount = lookupResults["resultCount"] as? Int, resultCount == 1 else {
completion(nil)
return
}
guard let results = lookupResults["results"] as? [[String: Any]] else {
completion(nil)
return
}
guard let version = results[0]["version"] as? String else {
completion(nil)
return
}
completion(version)
}
task.resume()
}
/// Checks if the app is installed from the app store
/// `Bundle.main.oy_isAppInstalledFromAppStore`→ output → true
public var oy_isAppInstalledFromAppStore: Bool {
return Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
}
/// `Bundle.main.oy_appBuildNumber`→ output → "1"
public var oy_appBuildNumber: String? { oy_infoPlistValue(key: "CFBundleVersion") as? String }
/// `Bundle.main.oy_bundleIdentifier`→ output → "com.oy.oyExtensions"
public var oy_bundleIdentifier: String? { oy_infoPlistValue(key: "CFBundleIdentifier") as? String }
/// `Bundle.main.oy_isRunningOnSimulator`→ output → true
public var oy_isRunningOnSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
/// `Bundle.main.oy_isRunningInAppStore`→ output → true
public var oy_isRunningInAppStore: Bool {
!oy_isRunningOnSimulator && !oy_isInTestFlight && !oy_isAdHocDistributed
}
/// `Bundle.main.oy_isInTestFlight`→ output → true
public var oy_isInTestFlight: Bool {
!oy_isRunningOnSimulator && appStoreReceiptURL?.lastPathComponent == "sandboxReceipt" && !oy_isAdHocDistributed
}
/// `Bundle.main.oy_isAdHocDistributed`→ output → true
public var oy_isAdHocDistributed: Bool {
path(forResource: "embedded", ofType: "mobileprovision") != nil
}
/// `Bundle.main.oy_isExtension`→ output → true
public var oy_isExtension: Bool {
if bundlePath.hasSuffix(".appex") { return true }
guard let applicationClassName = NSClassFromString("UIApplication") else { return true }
return !applicationClassName.responds(to: Selector(("shared")))
}
/// `Bundle.main.oy_infoPlistAllData` → output → Dictionary<String, Any>
public var oy_infoPlistAllData: [String: Any] {
oy_read(type: [String: Any].self, name: "Info", fileType: "plist") ?? [:]
}
/// `Bundle.main.oy_read(type: [String: Any].self, name: "Info", fileType: "plist")`→ output → Dictionary<String, Any>
public func oy_read<T>(type: T.Type, name: String, fileType: String) -> T? {
guard let data = oy_data(name, fileType: fileType) else { return nil }
guard let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? T else { return nil }
return result
}
/// `Bundle.main.oy_infoPlistValue(key: "SecretKey")`→ output → "SecretValue"
public func oy_infoPlistValue(key: String) -> Any? {
object(forInfoDictionaryKey: key)
}
/// `Bundle.main.oy_decode(file: "sample", fileType: "json", type: Model.self)`
public func oy_decode<T: Codable>(file: String, fileType: String, type: T.Type) -> T? {
guard let data = oy_data(file, fileType: fileType) else { return nil }
guard let decoded = try? JSONDecoder().decode(T.self, from: data) else { return nil }
return decoded
}
/// `Bundle.main.oy_path(name: "sample", fileType: "html")`
public func oy_path(_ name: String, fileType: String? = nil) -> URL? {
return url(forResource: name, withExtension: fileType)
}
/// `Bundle.main.oy_write(resource: "Sample", fileType: "plist", value: newInstance)`
public func oy_write<T: Encodable>(file: String, fileType: String? = nil, value: T) {
guard let url = oy_path(file, fileType: fileType) else { return }
oy_write(url, value)
}
}
extension Bundle {
private func oy_data(_ resource: String, fileType: String?) -> Data? {
guard let url = oy_path(resource, fileType: fileType) else { return nil }
guard let data = try? Data(contentsOf: url) else { return nil }
return data
}
private func oy_write<T>(_ url: URL, _ value: T) where T: Encodable {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do {
let data = try encoder.encode(value)
try data.write(to: url)
} catch { }
}
}