Skip to content

Commit

Permalink
feat(ios): add openAppSettings method
Browse files Browse the repository at this point in the history
  • Loading branch information
pwespi committed Oct 2, 2021
1 parent 53f1b3d commit f57fac7
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Below is an index of all the methods available.
- [`isLocationEnabled()`](#islocationenabled)
- [`openLocationSettings()`](#openlocationsettings)
- [`openBluetoothSettings()`](#openbluetoothsettings)
- [`openAppSettings()`](#openappsettings)
- [`setDisplayStrings(...)`](#setdisplaystrings)
- [`requestDevice(...)`](#requestdevice)
- [`requestLEScan(...)`](#requestlescan)
Expand Down Expand Up @@ -366,6 +367,17 @@ Only available on **Android**.

---

### openAppSettings()

```typescript
openAppSettings() => Promise<void>
```

Open App settings.
Only available on **iOS**.

---

### setDisplayStrings(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ class BluetoothLe : Plugin() {
call.resolve()
}

@PluginMethod
fun openAppSettings(call: PluginCall) {
call.unavailable("openAppSettings is not available on Android.")
}

@PluginMethod
fun setDisplayStrings(call: PluginCall) {
displayStrings = DisplayStrings(
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CAP_PLUGIN_METHOD(isLocationEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(openLocationSettings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(openBluetoothSettings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(openAppSettings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setDisplayStrings, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(requestDevice, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(requestLEScan, CAPPluginReturnPromise);
Expand Down
19 changes: 19 additions & 0 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public class BluetoothLe: CAPPlugin {
call.unavailable("openBluetoothSettings is not available on iOS.")
}

@objc func openAppSettings(_ call: CAPPluginCall) {
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
call.reject("Cannot open app settings.")
return
}


DispatchQueue.main.async {
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
call.resolve([
"value": success
])
})
} else {
call.reject("Cannot open app settings.")
}
}
}

@objc func setDisplayStrings(_ call: CAPPluginCall) {
for key in ["noDeviceFound", "availableDevices", "scanning", "cancel"] {
Expand Down
15 changes: 15 additions & 0 deletions src/bleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export interface BleClientInterface {
*/
openBluetoothSettings(): Promise<void>;

/**
* Open App settings.
* Only available on **iOS**.
* On **iOS** when a user declines the request to use Bluetooth on the first call of `initialize`, it is not possible
* to request for Bluetooth again from within the app. In this case Bluetooth has to be enabled in the app settings
* for the app to use it.
*/
openAppSettings(): Promise<void>;

/**
* Set the strings that are displayed in the `requestDevice` dialog.
* @param displayStrings
Expand Down Expand Up @@ -295,6 +304,12 @@ class BleClientClass implements BleClientInterface {
});
}

async openAppSettings(): Promise<void> {
await this.queue(async () => {
await BluetoothLe.openAppSettings();
});
}

async setDisplayStrings(displayStrings: DisplayStrings): Promise<void> {
await this.queue(async () => {
await BluetoothLe.setDisplayStrings(displayStrings);
Expand Down
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export interface BluetoothLePlugin {
isLocationEnabled(): Promise<BooleanResult>;
openLocationSettings(): Promise<void>;
openBluetoothSettings(): Promise<void>;
openAppSettings(): Promise<void>;
setDisplayStrings(displayStrings: DisplayStrings): Promise<void>;
requestDevice(options?: RequestBleDeviceOptions): Promise<BleDevice>;
requestLEScan(options?: RequestBleDeviceOptions): Promise<void>;
Expand Down
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class BluetoothLeWeb extends WebPlugin implements BluetoothLePlugin {
throw this.unavailable('openBluetoothSettings is not available on web.');
}

async openAppSettings(): Promise<void> {
throw this.unavailable('openAppSettings is not available on web.');
}

async setDisplayStrings(): Promise<void> {
// not available on web
}
Expand Down

0 comments on commit f57fac7

Please sign in to comment.