当前位置:网站首页>Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
2022-06-25 00:22:00 【Mr, Guo】
uniapp Simple package of incentive ads and plug-in ads
Today, I will write a uniapp Simple package of incentive advertising and screen advertising tutorial , The main problem to be solved is that if a small program has many pages and needs to call more advertisements , This can lead to a lot of duplicate code , And make the page js The file looks cumbersome , If it can be encapsulated , Then you only need to use one line of code to implement the call .
First, create a new one js file , It is named here for the convenience of demonstration Utils.js, It is better to create a new folder in the root directory and put it in , Convenient call after .
Because there are no other events to call in the screen advertisement , Quite able to run independently , So it's easy to write , So here js Writing in the file is actually similar to page calling , But we will advertise id Pass it in as a parameter , After that, you can directly call , The code is as follows :
var interstitialAd = null;
let interstitial = {
load(id) {
if (uni.createInterstitialAd) {
interstitialAd = uni.createInterstitialAd({
adUnitId: id
})
interstitialAd.onLoad(() => {
console.log(' The screen insert advertisement is loading ')
})
interstitialAd.onError((err) => {
console.log(' Load error ', err)
})
interstitialAd.onClose((res) => {
console.log(' The plug-in advertisement is turned off ', res)
})
}
},
show() {
if (interstitialAd) {
interstitialAd.show().catch((err) => {
console.error(err)
})
}
}
}
You can see here interstitial Object has two functions , One is the loading function , One is the display function , Then we can export them , Because we also need to encapsulate incentive advertising , Then we will export it together later .
Incentive advertising here we need to pass two parameters , One is advertising id, One is the reward event after the completion of incentive advertising , The code is as follows :
let videoAd = null;
let rewarded = {
load(id, e) {
if (uni.createRewardedVideoAd) {
videoAd = uni.createRewardedVideoAd({
adUnitId: id
})
videoAd.onError(err => {
})
videoAd.onClose((status) => {
if (status && status.isEnded || status === undefined) {
e()
} else {
}
})
}
},
show() {
if (videoAd) {
videoAd.show().catch(() => {
// Failure to retry
videoAd.load()
.then(() => videoAd.show())
.catch(err => {
console.log(' Incentive video Advertisement display failed ')
})
})
}
}
}
among e It is the incoming reward event , How to use it will be explained later , Finally, we can export the two encapsulated objects , The code is as follows :
module.exports = {
interstitial,
rewarded
};
Note that the relative path of the file is not misspelled , But you made a mistake ide Will also give you the wrong report .
Cut in ads
stay onLoad There are screen advertisements loaded , Display a plug-in advertisement , The code is as follows : The code is as follows :
Utils.interstitial.load(' Your advertisement id')
Utils.interstitial.show()
Encourage advertising
stay onLoad It is loaded with incentive advertisements , The code is as follows :
Utils.rewarded.load('adunit-85e98527c651f48f', () => {
// Write down any reward event you want here
});
Utils.rewarded.show();
Video or banner advertisement 、 You can write it directly on the page or use the template method to import
<ad unit-id="" ad-type="video" ad-theme="white"></ad>
<ad unit-id="" ad-type="banner" ad-theme="white"></ad>
Video patch advertising 、 You can write it directly on the page or use the template method to import
<video unit-id=""></video>
Native template advertising 、 You can write it directly on the page or use the template method to import
<ad-custom unit-id=""></ad-custom>
That's all for this chapter , For more quality content, please pay attention to the official account :Java Programmer Inn .
边栏推荐
- 颜色渐变梯度颜色集合
- JDBC —— 数据库连接
- Intensive reading of thinking about markdown
- 中低速航空航天电子总线概述
- D omit parameter name
- Ott marketing is booming. How should businesses invest?
- In the past 5 years, from "Diandian" to the current test development, my success is worth learning from.
- Use of JMeter
- One way 和two way ANOVA分析的区别是啥,以及如何使用SPSS或者prism进行统计分析
- Collective example
猜你喜欢
@mysql

Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform

Use and click of multitypeadapter in recycleview

Design scheme of authority management of fusion model
Alternative to log4j

Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud

Eye gaze estimation using webcam
Modstart: embrace new technologies and take the lead in supporting laravel 9.0

Tutorial details | how to edit and set the navigation function in the coolman system?

Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
随机推荐
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了
linux 系统redis常用命令
Collective例子
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
C program design topic 15-16 final exam exercise solutions (Part 1)
Use of navigation and navigationui
有趣的checkbox计数器
[figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
im即时通讯开发应用保活之进程防杀
How can I persuade leaders to use DDD to construct the liver project?
Svg line animation background JS effect
Hibernate learning 3 - custom SQL
DO280OpenShift访问控制--加密和ConfigMap
Jar package merging using Apache ant
从数字化过渡到智能制造
Ott marketing is booming. How should businesses invest?
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
Use of JMeter easynmon
∞ symbol line animation canvasjs special effect