【问题标题】:Access an iOS configuration profile from React Native?从 React Native 访问 iOS 配置文件?
【发布时间】:2019-03-19 20:42:09
【问题描述】:

我的 iPhone 上运行了一个 React Native 应用程序,我想在安装特定配置文件后更改应用程序配置(针对另一个环境)。

如何从 React Native 应用程序读取/访问 iOS 配置文件?

【问题讨论】:

    标签: react-native configuration-profile


    【解决方案1】:

    您可以制作简单的自定义模块以从 iOS 原生端获取该值。 这是名为 RNConfig

    的自定义模块

    RNConfig.h

    #import <Foundation/Foundation.h>
    #import "React/RCTBridgeModule.h"
    
    @interface RNConfig : NSObject<RCTBridgeModule>
    
    @end
    

    RNConfig.m

    #import "RNConfig.h"
    
    @implementation RNConfig
    
    RCT_EXPORT_MODULE();
    
    - (NSDictionary *)constantsToExport
    {
      NSString* platform_name = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"PlatformName"];
      return @{ @"PlatformName": platform_name };
    }
    
    @end
    

    使用 React Native,您可以通过以下代码 sn-ps 使用该模块。

    const { RNConfig } = require('NativeModules');
    let platformName = RNConfig.PlatformName;
    

    当您在 iOS 端有多个目标时,可以使用此模块。 所以在 React Native 方面,根据每个目标,你有很多配置值。

    config/index.js

    import { targetA } from './targetA';
    import { targetB } from './targetB';
    
    const variables = {
      targetA,
      targetB
    };
    
    const { RNConfig } = require('NativeModules');
    export default variables[RNConfig.PlatformName];
    

    如果您的 iOS 目标名称是 targetAtargetB,那么您可以通过像上面那样获取 iOS 平台名称来使用 React Native 端的特定配置设置。

    无论如何,你可以通过更改RNConfig.m文件在iOS端获取任何类型的配置值。

    【讨论】:

    • 感谢您的回答!你知道一个文档页面,我可以在其中找到我可以从 RNConfig.m 文件中获取值的所有可用键吗?还是来自自定义配置文件?
    • 它是来自 Info.plist 文件的字典。 developer.apple.com/documentation/foundation/nsbundle/…
    • 好的,所以您的解决方案不会访问电话配置文件,对吧?还是我理解错了?
    • 当您想为 iOS 应用程序上的每个目标使用不同的环境时,可以使用它。使用上述方法,您可以访问每个目标的 Info.plist 文件。因此,如果您为每个目标制作不同的 Info.plist 文件(这是可能的,如果您有很多目标,您应该这样做)并且您可以使用上述方法访问每个信息文件。
    • 感谢您的 cmets。我说的是 iOS 配置文件(如本文所述:howtogeek.com/253325/…
    猜你喜欢
    • 2021-07-22
    • 2017-01-30
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多