在 React Native 项目中,我想从 iOS npm 模块访问 Presented Viewcontroller。我可以使用下面的代码访问 RN 项目的 rootviewcontroller
UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
Run Code Online (Sandbox Code Playgroud)
但我希望当前的 VC 呈现在 RootVC 之上,以便我应该能够在其之上呈现本机(iOS)UINavigationController。
注意:[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController返回零。
因此,我正在练习用 Java 制作本机模块,这些模块可以将函数暴露给 React Native 的 JavaScript 代码。为此,我决定使用一个简单的演示数学库。我的代码如下。
MathOpsModule.java
package com.mb_rn_poc;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class MathOpsModule extends ReactContextBaseJavaModule {
@Override
public String getName() {
return "MathOps"; // Name of the Native Modules.
}
@ReactMethod
public int add(int a, int b) {
return (a + b);
}
}
Run Code Online (Sandbox Code Playgroud)
MathOpsPackage.java
package com.mb_rn_poc;
import com.facebook.react.ReactPackage;
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 MathOpsPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(
ReactApplicationContext reactContext
) { …Run Code Online (Sandbox Code Playgroud) 我正在编写一个反应本机应用程序来获取IOS和Android电池状态.我在网上搜索,发现很少的库可以获得手机的电池电量.
https://github.com/robinpowered/react-native-device-battery
https://github.com/remobile/react-native-battery-status
https://github.com/oojr/react-native-battery
当我尝试这个时,每个库都有问题,而在git hub上提问时,对开发人员的支持不多.
任何人都可以为我提供更好的解决方案或库来获取IOS和Android的电池状态.
提前致谢
batterylevel react-native native-module react-native-native-module
我正在使用 React Native 0.60.5 并且正在链接这个模块。它说对于 React Native 0.60+,“CLI 自动链接功能在构建应用程序时链接模块”,所以我们需要运行的只是yarn add @react-native-community/async-storage.
但是,在描述自动链接的主页上,它说我们需要运行:
yarn add @react-native-community/async-storage
cd ios && pod install && cd ..
我想知道的:
我们是否必须pod install使用 React Native 0.60+运行所有原生模块?
cocoapods reactjs react-native native-module react-native-native-module
我想在 React Native 中跟踪用户的总体应用程序使用时间(其他应用程序)。
我发现android.app.usage可以准确跟踪我想要的内容。
是否可以在 React Native 中使用“android.app.usage”?或者我是否必须为 React Native 制作自己的本机模块?
提前致谢!
我正在尝试使用 expo-modules 为官方 Spotify SDK 构建一个反应本机包装模块。我已.podspec使用s.vendored_frameworks. 示例应用程序的项目预构建工作正常,但框架的头文件不会被复制,并且在为模拟器构建应用程序时,我收到此错误:
\xe2\x9d\x8c (/Users/<username>/Library/Developer/Xcode/DerivedData/expospotifyexample-ayfjxgjzbhoakacqvfpiaawhvpwz/Build/Products/Debug-iphonesimulator/ExpoSpotify/ExpoSpotify-umbrella.h:13:9)\n\n 11 | #endif\n 12 | \n> 13 | #import "SpotifyAppRemote.h"\n | ^ 'SpotifyAppRemote.h' file not found\n 14 | #import "SpotifyiOS.h"\n 15 | #import "SPTAppRemote.h"\n 16 | #import "SPTAppRemoteAlbum.h"\nRun Code Online (Sandbox Code Playgroud)\n如果我手动将SDK的头文件复制到错误所在的路径,它就可以正常工作。
\n我的文件夹结构(只是重要的部分):
\n\xe2\x94\x9c\xe2\x94\x80 example/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 ios/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 Pods/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 Headers/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 Public/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 ExpoSpotify/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 ExpoSpotify.modulemap\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 …Run Code Online (Sandbox Code Playgroud) ios react-native expo react-native-native-module expo-module
关于这个问题,我一直在尝试通过Android中的本机模块来完成。
我已经.../java/com/myproject/multiplecamerastream按照React Native ToastModule的示例声明了我的模块(此处的功能并不重要):
public class MultipleCameraStreamModule extends ReactContextBaseJavaModule {
private static final String CAMERA_FRONT = "SHORT";
private static final String CAMERA_BACK = "LONG";
public MultipleCameraStreamModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "MultipleCameraStream";
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(CAMERA_FRONT, Toast.LENGTH_SHORT);
constants.put(CAMERA_BACK, Toast.LENGTH_LONG);
return constants;
}
@ReactMethod
public void show(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, duration).show();
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我的模块包装器:
public class …Run Code Online (Sandbox Code Playgroud) java android react-native native-module react-native-native-module
我正在构建具有一些本地android模块的react-native应用程序。
在MainApplication.java中,
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new VectorIconsPackage(),
new MyCustomPackage()
);
}
Run Code Online (Sandbox Code Playgroud)
在我的MyCustomPackage中,
public class MyCustomPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new MyCustomModule(reactContext));
return modules;
}
}
Run Code Online (Sandbox Code Playgroud)
我还有其他几个模块,但这只是一个例子。所有功能都运作良好。现在,我想向MyCustomModule Java类中的方法编写单元测试。我尝试使用Robolectric框架,但不知道它如何与react native一起工作。还有其他工具吗?谁能给我一些示例或指导,以编写本机原生Android代码的单元测试?
android unit-testing react-native react-native-android react-native-native-module
我正在开发一个 React Native 应用程序并尝试从 android 访问本机模块。我正在关注这个官方文档 在此处输入链接描述
创建本机模块后,当我尝试在 javaScript 类中访问它时,它显示错误“typeError: null is not an object (Evaluating _ToastExample.default.show)”
ToastModule.java
package com.awesomeproject;
import android.widget.Toast;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.Map;
import java.util.HashMap;
public class ToastModule extends ReactContextBaseJavaModule {
private static ReactApplicationContext reactContext;
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
ToastModule(ReactApplicationContext context) {
super(context);
reactContext = context;
}
@Override
public String getName() {
return "ToastExample";
}
@Override
public Map<String, Object> …Run Code Online (Sandbox Code Playgroud) react-native native-module react-native-android react-native-native-module
我知道以前有人问过这种问题,但它们与第三方库有关,我的与 requireNativeComponent
我已经React Native从本地iOS组件创建了一个组件
这是我的iOS代码
CanvasView 文件
class CanvasView: UIView {
var lines = [[CGPoint]]()
override init(frame: CGRect) {
super.init(frame:frame)
backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else {
return
}
context.setStrokeColor(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1))
context.setLineWidth(10)
context.setLineCap(.butt)
lines.forEach { (line) in
for(i,p) in line.enumerated() …Run Code Online (Sandbox Code Playgroud) react-native react-native-android react-native-ios react-native-native-module
react-native ×10
react-native-native-module ×10
android ×3
ios ×2
java ×2
batterylevel ×1
cocoapods ×1
expo ×1
expo-module ×1
npm ×1
reactjs ×1
unit-testing ×1