Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[General] [add] - Add openSettings method to Linking module #23965

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -152,6 +152,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