我有一个食谱详细信息屏幕,我想在每次用户进入以查看食谱详细信息时添加一个插页式广告,但我希望将其限制为每3次显示一次,因为当用户退出并重新输入时食谱或其他食谱再次显示广告,这是我不想要的。
我该怎么办?
import { AdMobInterstitial, setTestDeviceIDAsync } from 'expo-ads-admob';
export default class RecipeDetails extends Component {
initAds = async () => {
const INTERSTITIAL_ID = Platform.OS == "ios" ? ConfigApp.IOS_INTERSTITIAL_ID : ConfigApp.ANDROID_INTERSTITIAL_ID;
AdMobInterstitial.setAdUnitID(INTERSTITIAL_ID);
await setTestDeviceIDAsync(ConfigApp.TESTDEVICE_ID);
await AdMobInterstitial.requestAdAsync({ servePersonalizedAds: true});
await AdMobInterstitial.showAdAsync();
};
componentDidMount() {
this.initAds();
}
render() {
return (
<View>
// content
</View>
);
}
答案 0 :(得分:0)
我认为您每次致电广告时都可以使用async-storage来存储时间。并在播放广告之前阅读您存储的每一次以比较它是否等于三?
存储数据(时间)
storeData = async () => {
try {
await AsyncStorage.setItem('@storage_Key', 'stored value')//
} catch (e) {
// saving error
}
}
读取数据(次)
getData = async () => {
try {
const value = await AsyncStorage.getItem('@storage_Key')
if(value !== null) {
// value previously stored
}
} catch(e) {
// error reading value
}
}
-------------- update ----------------您想要的东西-------
initAds = async () => {
const INTERSTITIAL_ID = Platform.OS == "ios" ? ConfigApp.IOS_INTERSTITIAL_ID : ConfigApp.ANDROID_INTERSTITIAL_ID;
try{
const value = await AsyncStorage.getItem('play_ad_times')
if(value !== null) {
if(value == "3"){
await AsyncStorage.setItem('play_ad_times', "1"); //If three times back to one times and play once
AdMobInterstitial.setAdUnitID(INTERSTITIAL_ID);
await setTestDeviceIDAsync(ConfigApp.TESTDEVICE_ID);
await AdMobInterstitial.requestAdAsync({ servePersonalizedAds: true});
await AdMobInterstitial.showAdAsync();
}else{
var temp = parseInt(value)+1;
await AsyncStorage.setItem('play_ad_times', temp.toString() );
}
// value previously stored
}else{
//first time in
await AsyncStorage.setItem('play_ad_times', "1"); //Set time 1
AdMobInterstitial.setAdUnitID(INTERSTITIAL_ID);
await setTestDeviceIDAsync(ConfigApp.TESTDEVICE_ID);
await AdMobInterstitial.requestAdAsync({ servePersonalizedAds: true});
await AdMobInterstitial.showAdAsync();
}
}catch(e) {
// error reading value
await AsyncStorage.setItem('play_ad_times', "1");
}
};