ios - 如何使用 React Native 从 UIDevice 获取设备名称?

标签 ios react-native

我想使用 React Native 访问设备名称(来自 UIDevice)。有什么好的方法吗?

最佳答案

你需要做一个Native Modules (iOS) .以下是步骤:

  • 制作用于获取设备名称的原生 iOS 组件
  • 在JS中使用

1.制作组件

//  Device.h
@import UIKit;
#import "RCTBridgeModule.h"

@interface Device : NSObject <RCTBridgeModule>

@end

//  Device.m
#import "Device.h"

@implementation Device
RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(deviceName:(RCTResponseSenderBlock)callback) {
  NSString *deviceName = [[UIDevice currentDevice] name];
  callback(@[deviceName]);
}
@end

2.在JS中使用

var Device = require('react-native').NativeModules.Device;
Device.deviceName( (name) => {
  console.log(name) 
});

关于ios - 如何使用 React Native 从 UIDevice 获取设备名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31933385/

相关文章:

ios - 如何检索 Firebase Swift 中不同键下存储的数据?

ios - 对成员 'create(_:value:update:)' 的模糊引用

ios - 找不到目标 'Alamofire' 的模块 'arm64-apple-ios-simulator' ;找到 : x86_64-apple-ios-simulator, x86_64

javascript - 使用 asyncStorage 进行原生 react 以进行日常日记和图表

javascript - 错误 : Unable to resolve module `@react-native-community/toolbar-android`

ios - 如何使用 Coreplot 在 CPTLayerHostingView 中添加关闭按钮

javascript - 你如何硬件加速 iOS 上的窗口滚动?

javascript - 这个 setState 的 React hooks 等价物是什么?

android - 如何避免在 react-native 中迁移到 AndroidX?

ios - 检测到二维码,但在 iPhone 中找不到可用数据