ios App Start Loading Advertising Page Ideas

Keywords: network JSON iOS

demand

Many apps (such as Taobao, Mei Tuan, etc.) display several seconds of advertisements after loading the startup map. Generally, there is a skip button to skip the advertisement. Some apps also enter an advertisement page after clicking on the advertisement page, and then click back to the home page. Although this ad page is not very good for the user experience, if there is such a need, we still have to find ways to develop it, at least it is much friendlier than embedded advertising. Today we are going to develop an advertising page, the effect is as follows.

thinking

1. Advertising page loading ideas. The content of the advertisement page should be displayed in real time, and it can not be delayed loading without network status or slow network speed, or it can be reloaded when the home page appears. So here I do not use the network request advertising interface to get the picture address, and then load the picture, but first download the picture asynchronously to the local, and save the picture name. Each time I open app, I first search for the existence of the picture in the sandbox according to the image name stored locally, and if it exists, display the advertisement page.

2. Determine whether the advertisement page is updated. Whether or not there are advertisement pictures in the locality, every time it starts, it needs to call the advertisement interface again to judge whether the advertisement is updated according to the image name or image id. If the acquired image name or image id is inconsistent with the local storage, it needs to download the new picture again and delete the old picture.

3. Click on the advertisement page. If clicking on an advertisement requires a jump to the details page of the advertisement, then the link address of the advertisement also needs to be stored in NSUser Defaults. Note: The Advertising Details page is push ed in from the home page.

4. The display code of the advertisement page can be placed in AppDeleat or in the controller of the home page. If the code is in AppDelegate, you can push the home page to the Advertising Details page by sending a notification.

5. The bottom of the advertising page and the bottom of the startup map are generally the same, giving us the feeling that after loading the startup map, the advertising map is placed on the startup map, and there can be no deviation, such as the following Taobao startup screen. Artists should pay attention to this when making advertisement drawings.

6. Studied Taobao's advertising display mechanism. After deleting Taobao, reopen the non-display advertisement pictures, and the second time open it, it will be displayed. Metro's ad maps sometimes don't show, so when developing the ad api, the background can add a field to judge whether the ad is enabled or not. If the background closes the ad, delete the picture in the sandbox.


Asynchronous downloading of pictures

  1. NSString *filePath = [self getFilePathWithImageName:[kUserDefaults valueForKey:adImageName]];  
  2. BOOL isExist = [self isFileExistWithFilePath:filePath];  
  3. if (isExist) {//Picture Existence  
  4. AdvertiseView *advertiseView = [[AdvertiseView alloc] initWithFrame:self.window.bounds];  
  5. advertiseView.filePath = filePath;  
  6. [advertiseView show];  
  7. }  

2. Whether or not there are advertisement pictures in sandbox, we need to call the access interface again to determine whether the advertisement is updated or not.

  1. AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];  
  2. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil nil];  
  3. [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {  
  4. NSArray *dataArray = responseObject[@"data"];  
  5. NSString *imageUrl = dataArray[0][@"imageUrl"];  
  6. NSArray *stringArr = [imageUrl componentsSeparatedByString:@"/"];  
  7. NSString *imageName = stringArr.lastObject;  
  8. NSString *filePath = [self getFilePathWithImageName:imageName];  
  9. BOOL isExist = [self isFileExistWithFilePath:filePath];  
  10. if (!isExist){//If the image does not exist, download the new image and delete the old one  
  11. [self downloadAdImageWithUrl:imageUrl imageName:imageName];  
  12. }  
  13. } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {  
  14. }];  

Asynchronous downloading of pictures

  1. /** 
  2. *  Download new pictures 
  3. */  
  4. - (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName  
  5. {  
  6. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
  7. NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];  
  8. UIImage *image = [UIImage imageWithData:data];  
  9. NSString *filePath = [self getFilePathWithImageName:imageName]; //Name of the saved file  
  10. if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {//Successful preservation  
  11. NSLog(@"Successful Preservation");  
  12. [self deleteOldImage];//Delete old pictures after successful saving  
  13. [kUserDefaults setValue:imageName forKey:adImageName];  
  14. [kUserDefaults synchronize];  
  15. //If there is an advertisement link, you need to save the advertisement link as well.  
  16. }else{  
  17. NSLog(@"Save failed");  
  18. }  
  19. });  
  20. }  
  21. /** 
  22. *  Delete old pictures 
  23. */  
  24. - (void)deleteOldImage  
  25. {  
  26. NSString *imageName = [kUserDefaults valueForKey:adImageName];  
  27. if (imageName) {  
  28. NSString *filePath = [self getFilePathWithImageName:imageName];  
  29. NSFileManager *fileManager = [NSFileManager defaultManager];  
  30. [fileManager removeItemAtPath:filePath error:nil];  
  31. }  
  32. }  


3. The countdown function of skip button of advertisement page can be realized by timer or GCD.
  1. //GCD countdown  
  2. - (void)startCoundown  
  3. {  
  4. __block int timeout = showtime + 1//Countdown time + 1  
  5. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
  6. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 00,queue);  
  7. dispatch_source_set_timer(_timer,dispatch_walltime(NULL0),1.0 * NSEC_PER_SEC, 0); //Execution per second  
  8. dispatch_source_set_event_handler(_timer, ^{  
  9. if(timeout <= 0){ //Close the countdown  
  10. dispatch_source_cancel(_timer);  
  11. dispatch_async(dispatch_get_main_queue(), ^{  
  12. [self dismiss];  
  13. });  
  14. }else{  
  15. dispatch_async(dispatch_get_main_queue(), ^{  
  16. [_countBtn setTitle:[NSString stringWithFormat:@"skip%d",timeout] forState:UIControlStateNormal];  
  17. });  
  18. timeout--;  
  19. }  
  20. });  
  21. dispatch_resume(_timer);  
  22. }  

4. Add a click gesture to the ad page and jump to the ad page.
  1. <pre name="code" class="objc" style="font-size: 14px;">//AdvertiseView.m  
  2. - (void)pushToAd{  
  3. [self dismiss];  
  4. [[NSNotificationCenter defaultCenter] postNotificationName:@"pushtoad" object:nil userInfo:nil];  
  5. }  
  6. // ViewController.m  
  7. - (void)viewDidLoad {  
  8. [super viewDidLoad];  
  9. self.title = @"home page";  
  10. self.view.backgroundColor = [UIColor orangeColor];  
  11. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToAd) name:@"pushtoad" object:nil];  
  12. }  
  13. - (void)pushToAd {  
  14. AdvertiseViewController *adVc = [[AdvertiseViewController alloc] init];  
  15. [self.navigationController pushViewController:adVc animated:YES];  
  16. }  
  1.   
Original address: http://www.cocoachina.com/ios/20160614/16671.html

Posted by thedotproduct on Wed, 17 Jul 2019 19:26:18 -0700