Skip to content

Commit

Permalink
- Add openSettings method to Linking module (#23965)
Browse files Browse the repository at this point in the history
Summary:
This will create a cross-platform and safe way to programmatically open the app's settings into the iOS /Android Settings app.

Right now it's possible to open the app's settings, but _**only for iOS**_ via `Linking.openURL("app-settings:")`

To do the same for Android, you need to either create NodeModule or install a dependency such as [react-native-open-settings](https://github.com/lunarmayor/react-native-open-settings).

Why this new method is useful: since Android 6, app permissions work similar to iOS. It's granular and it's requested in the app runtime.

https://developer.android.com/guide/topics/permissions/overview#runtime_requests_android_60_and_higher

> If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the user isn't notified of any app permissions at install time. Your app must ask the user to grant the dangerous permissions at runtime. When your app requests permission, the user sees a system dialog telling the user which permission group your app is trying to access. The dialog includes a Deny and Allow button.

Thus, if the user checks the **"Never ask again box"** and taps **"Deny"**, for some specific permission, the only way to change the permission is going to the Android Setting app.

And that's where this new method becomes useful. It'll allow our apps to programmatically send the the user to  settings app.

Also, `openSettings()` doesn't receive a parameter to redirect to specific subsections of the Settings app because there's no public API to do it on iOS ([there's a way to have, via private API, but it causes the app to get rejected.](mauron85/cordova-plugin-background-geolocation#394))

Create `Linking.openSettings()` for iOS and Android;

[General] [add ] - Add openSetting method to Linking module
Pull Request resolved: #23965

Differential Revision: D14502910

Pulled By: cpojer

fbshipit-source-id: d27d62282b9df499845c78d983d3b6936c36ea39
  • Loading branch information
Estevão Lucas authored and facebook-github-bot committed Mar 18, 2019
1 parent bce1e6f commit fa426cf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Libraries/Linking/Linking.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ class Linking extends NativeEventEmitter {
return LinkingManager.canOpenURL(url);
}

/**
* Open app settings.
*
* See https://facebook.github.io/react-native/docs/linking.html#opensettings
*/
openSettings(): Promise<any> {
return LinkingManager.openSettings();
}

/**
* If the app launch was triggered by an app link,
* it will give the link url, otherwise it will give `null`
Expand Down
22 changes: 22 additions & 0 deletions Libraries/LinkingIOS/RCTLinkingManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,26 @@ - (void)handleOpenURLNotification:(NSNotification *)notification
resolve(RCTNullIfNil(initialURL.absoluteString));
}

RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if (@available(iOS 10.0, *)) {
[RCTSharedApplication() openURL:url options:@{} completionHandler:^(BOOL success) {
if (success) {
resolve(nil);
} else {
reject(RCTErrorUnspecified, @"Unable to open app settings", nil);
}
}];
} else {
BOOL opened = [RCTSharedApplication() openURL:url];
if (opened) {
resolve(nil);
} else {
reject(RCTErrorUnspecified, @"Unable to open app settings", nil);
}
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;

import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -140,9 +141,36 @@ public void canOpenURL(String url, Promise promise) {
}
}

/**
* Starts an external activity to open app's settings into Android Settings
*
* @param promise a promise which is resolved when the Settings is opened
*/
@ReactMethod
public void openSettings(Promise promise) {
try {
Intent intent = new Intent();
Activity currentActivity = getCurrentActivity();
String selfPackageName = getReactApplicationContext().getPackageName();

intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + selfPackageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
currentActivity.startActivity(intent);

promise.resolve(true);
} catch (Exception e) {
promise.reject(new JSApplicationIllegalArgumentException(
"Could not open the Settings: " + e.getMessage()));
}
}

/**
* Allows to send intents on Android
*
*
* For example, you can open the Notification Category screen for a specific application
* passing action = 'android.settings.CHANNEL_NOTIFICATION_SETTINGS'
* and extras = [
Expand Down
1 change: 1 addition & 0 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const mockNativeModules = {
Linking: {
openURL: jest.fn(),
canOpenURL: jest.fn(() => Promise.resolve(true)),
openSettings: jest.fn(),
addEventListener: jest.fn(),
getInitialURL: jest.fn(() => Promise.resolve()),
removeEventListener: jest.fn(),
Expand Down

0 comments on commit fa426cf

Please sign in to comment.