Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
feat: add iOS support (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Aug 12, 2021
1 parent 648a64a commit afa71c4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<!-- ALL-CONTRIBUTORS-BADGE:END -->
</p>

> ⚠️ The iOS implementation could not be tested yet. I appreciate any feedback.
## Maintainers

| Maintainer | GitHub | Social |
Expand Down Expand Up @@ -138,6 +140,8 @@ getBoolean(options: GetOptions) => Promise<GetResult<boolean>>

On **Android**, see [Set up device owner for testing](https://source.android.com/devices/tech/admin/testing-setup#set_up_the_device_owner_for_testing) and follow the instructions to set up a device owner testing environment.

On **iOS**, you need to install the app as a [managed app](https://support.apple.com/de-de/guide/deployment-reference-ios/iorf4d72eded/web) with a MDM solution.

## Changelog

See [CHANGELOG.md](https://github.com/robingenz/capacitor-managed-configurations/blob/master/CHANGELOG.md).
Expand Down
29 changes: 27 additions & 2 deletions ios/Plugin/ManagedConfigurations.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import Foundation

@objc public class ManagedConfigurations: NSObject {
@objc public func echo(_ value: String) -> String {
return value
private let managedConfig: [String: Any?]

override init() {
managedConfig = UserDefaults.standard.object(forKey: "com.apple.configuration.managed") as? [String: Any?] ?? [:]
}

@objc public func keyExists(_ key: String) -> Bool {
if managedConfig[key] != nil {
return true
}
return false
}

@objc public func getString(_ key: String) -> String {
// swiftlint:disable:next force_cast
return managedConfig[key] as! String
}

@objc public func getInt(_ key: String) -> Int {
// swiftlint:disable:next force_cast
return managedConfig[key] as! Int
}

@objc public func getBool(_ key: String) -> Bool {
// swiftlint:disable:next force_cast
return managedConfig[key] as! Bool
}

}
2 changes: 1 addition & 1 deletion ios/Plugin/ManagedConfigurationsPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
CAP_PLUGIN(ManagedConfigurationsPlugin, "ManagedConfigurations",
CAP_PLUGIN_METHOD(getString, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getInteger, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getNumber, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getBoolean, CAPPluginReturnPromise);
)
48 changes: 43 additions & 5 deletions ios/Plugin/ManagedConfigurationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,55 @@ import Capacitor
*/
@objc(ManagedConfigurationsPlugin)
public class ManagedConfigurationsPlugin: CAPPlugin {
private let implementation = ManagedConfigurations()
public let errorKeyMissing = "traceName must be provided."
private var implementation: ManagedConfigurations?

override public func load() {
implementation = ManagedConfigurations()
}

@objc func getString(_ call: CAPPluginCall) {
call.reject("Not implemented on iOS.")
guard let key = call.getString("key") else {
call.reject(errorKeyMissing)
return
}

var result = JSObject()
if implementation?.keyExists(key) == true {
result["value"] = implementation?.getString(key)
} else {
result["value"] = nil
}
call.resolve(result)
}

@objc func getInteger(_ call: CAPPluginCall) {
call.reject("Not implemented on iOS.")
@objc func getNumber(_ call: CAPPluginCall) {
guard let key = call.getString("key") else {
call.reject(errorKeyMissing)
return
}

var result = JSObject()
if implementation?.keyExists(key) == true {
result["value"] = implementation?.getInt(key)
} else {
result["value"] = nil
}
call.resolve(result)
}

@objc func getBoolean(_ call: CAPPluginCall) {
call.reject("Not implemented on iOS.")
guard let key = call.getString("key") else {
call.reject(errorKeyMissing)
return
}

var result = JSObject()
if implementation?.keyExists(key) == true {
result["value"] = implementation?.getBool(key)
} else {
result["value"] = nil
}
call.resolve(result)
}
}

0 comments on commit afa71c4

Please sign in to comment.