react-native 跳转到ios/android 权限设置界面

本文详细介绍了如何使用react-native在iOS和Android应用中跳转到各自的权限设置界面。对于iOS,利用Linking API实现;而对于Android,通过创建模块文件和注册OpenSettingsModule来调用openNetworkSettings函数,引导用户进入Android设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ios

使用react-native中Linking

import { Linking } from 'react-native';

Linking.openURL('app-settings:')
  .catch((err) => console.log('error', err));

android

1、在android/app/src/main/java/com/<projectname>文件夹下创建opensettings文件夹

2、在opensettings文件夹下创建模块文件OpenSettingsModule.java(模块功能)

package com.<projectname>.opensettings; // 记得把<projectname>改为你的项目名称
import android.app.Activity;
import android.content.Intent;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactContextBaseJavaModule;

public class OpenSettingsModule extends ReactContextBaseJavaModule {

  @Override
  public String getName() {
    /**
     * return the string name of the NativeModule which represents this class in JavaScript
     * In JS access this module through React.NativeModules.OpenSettings
     */
    return "OpenSettings";
  }

  @ReactMethod
  public void openNetworkSettings(Callback cb) {
    Activity currentActivity = getCurrentActivity();

    if (currentActivity == null) {
      cb.invoke(false);
      return;
    }
    try {
      currentActivity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
      cb.invoke(true);
    } catch (Exception e) {
      cb.invoke(e.getMessage());
    }
  }

  /* constructor */
  public OpenSettingsModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }
}

上面的模块功能可以通过调用openNetworkSettings函数打开android设置。

3、在opensettings文件夹下创建包文件OpenSettingsPackage.java(注册模块OpenSettingsModule)

package com.<projectname>.opensettings; // 记得把<projectname>改为你的项目名称
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OpenSettingsPackage implements ReactPackage {
  @Override
  public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();

    modules.add(new OpenSettingsModule(reactContext));

    return modules;
  }

//   @Override
//   public List<<Class>? extends JavaScriptModule> createJSModules() {
//     return Collections.emptyList();
//   }

  @Override
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
  }
}

4、把包提供到MainApplication.java文件的getPackages方法中:

import com.<projectname>.opensettings.*; // 还是要修改成自己项目名
...

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      ...
      new OpenSettingsPackage() /* <---- add here */
  );
}

5、准备工作完成,接下来调用:

import { NativeModules } from 'react-native'

NativeModules.OpenSettings.openNetworkSettings((data) => {
  console.log('call back data', data);
});  

转载于:https://www.cnblogs.com/qiqi715/p/10150964.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值