“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

originally

Recently, in the development project, we need to add the requirement of launching advertisement. The advertisement is arranged by the background itself, not the access third-party SDK. Then start the advertisement to realize by yourself, rough thought is not particularly difficult, should be quite simple, not before the launch of an AD, and then switch rootViewController to the home page after the AD is completed. But in the actual operation, there are a lot of small details, so write it down, and later on you can do related things, come back to see.

Train of thought

  1. Display of advertising

The display logic of the AD is pretty simple. Create an adController in AppDeleate, and then set the adController to the Controller to be displayed. The rest of the logic is handled in the adController.

All advertisements are time-limited, so we need a function of countdown, and all have the function of skipping, directly skip the countdown to the home page.

The bottom of the adController should be aligned with the startup page, so that the user appears oblivious and has a better experience.

  1. Advertising data

Advertising data is the main play, we use the local cache way to carry out

APP first start page does not display advertising directly into the home page, but at the time of the first time you start with the server requests advertising data, and then save to a local, the best way is to drop resource files (pictures, GIF, video) has been cached, so that the next startup can directly read data, Better user experience

When the APP was started for the second time, it began to display the advertising data we had cached. Since all the data and resource files were cached last time, there was no request for data when the APP was started for the second time. All the data and resource files were taken locally.

And there’s a little bit of processing that goes into caching data,

We can’t just save this AD for the next display, so the cached AD should be deleted? This is not very good. If the background operation personnel want to display the ads of the first day on the third day, the local data has been deleted, so they need to download the cache again, causing unnecessary overhead. Therefore, we can cache as much data as possible to the local

However, there is another problem. If the data is cached all the time, there will be a lot of useless expired data, and the APP does not know which one is useful and which one should be deleted. A better way is to use the cache and set an expiration date. When the expiration date is reached, the file will be deleted automatically.

So the data part is over, personally feel or quite simple, may write more disorderly, forgive me

conclusion

The launch advertisement is only a small function, but the associated function behind it is not a small function. In work, if you want to schedule projects according to the list of functions, if possible, try to take a little more time, or there are many big functions behind a small function, and it will be too late to even cry