如何在react native iOS模块中访问AppDelegate方法



我正在构建一个iOS模块,想要钩子到AppDelegate方法,比如applicationWillResignActive,我怎么才能实现呢?

你正在尝试使用applicationWillResignActive,该方法将在每次应用程序进入后台时被触发。

你可以通过使用AppState在react native中实现它可以告诉你应用程序是在前台还是后台,并在状态改变时通知你。

例子:

import React, { useRef, useState, useEffect } from "react";
import { AppState, StyleSheet, Text, View } from "react-native";
const AppStateExample = () => {
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
useEffect(() => {
const subscription = AppState.addEventListener("change", nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
console.log("AppState", appState.current);
});
return () => {
subscription.remove();
};
}, []);
return (
<View style={styles.container}>
<Text>Current state is: {appStateVisible}</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
});
export default AppStateExample;

相关内容

  • 没有找到相关文章