当前位置:网站首页>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 .
边栏推荐
- C program design topic 18-19 final exam exercise solutions (Part 2)
- vim使用命令
- ∞ symbol line animation canvasjs special effect
- 【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
- Hyperledger Fabric 2. X dynamic update smart contract
- Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model
- Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
- Use of JMeter
- [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
- [leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
猜你喜欢

Use and click of multitypeadapter in recycleview

wx小程序跳转页面

UE4 WebBrowser图表不能显示问题
How to use promise Race() and promise any() ?

Collective例子

Time unified system
![[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching](/img/bd/b176e93ee6fa2125f021bcad3025c2.png)
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching

Adding, deleting, modifying and checking in low build code

Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar

Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
随机推荐
微搭低代码中实现增删改查
Domain Driven Design and coding
What are the advantages of VR panoramic production? Why is it favored?
JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many
时间统一系统
Approaching harvest moon:moonbeam DFI Carnival
Basic summary of MySQL database knowledge
C program design topic 18-19 final exam exercise solutions (Part 2)
Analysis report on development mode and investment direction of sodium lauriminodipropionate in the world and China 2022 ~ 2028
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
What is test development? Can you find a job at this stage?
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
December 6, 2019 what happens after the browser enters a URL
A small program written this week
@mysql
Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
水库大坝安全监测
D does not require opapply() as a domain
Transition from digitalization to intelligent manufacturing