IOS startup map and open screen advertising map, similar to Netease

Keywords: iOS Attribute less

Boot map is an essential part in the development of iOS. Many app s will have a customized open screen ad map after the boot map. Click the ad map to jump to the corresponding page of the ad map. Today, I'd like to share with you how to add this ad map and click the jump of ad map. This ad map is implemented by adding UIImageView to UIWindow.

 

1, Add local boot map

 

1. Prepare local pictures

2. Find the Images.xcassets File, open LaunchImage, and drag the picture to the corresponding place, as shown below:

 

 

3. Will LaunchScreen.storyboard The Use as Launch Screen option on the right side of the file is turned off (do not select this option), as shown in the figure below:

 

 

4. Select Launch Images Source as launch image, and then leave Launch Screen File blank. As shown in the figure:

 

 

5. You can set the dwell time of startup graph in Appdelegate

 

// Start page stays for 1 second

[NSThread sleepForTimeInterval:1];

 

6. Run the project to see the set startup diagram (if not, delete the app first, and then run it again)

 

2, Add open screen ad map

 

The realization of this ad map is based on the author Zhou huanqiang Idea of App starting to load ad page , and then added some small supplements. Because advertising is time-effective, not every time it starts, it will display this ad page. So the principle here is that the data returned by the background includes the advertisement image, the URL corresponding to the advertisement image, and the deadline of the advertisement. When running the program, it will request ad map data and save the data locally. When running again, take out the saved image from the local, and judge whether the deadline of the image is valid. If the image exists and is within the validity period, display the ad map; request the ad map data again to avoid new ad data.

 

1. Create a customized SplashScreenView. When I add the ImageView to the window, I encounter an unsuccessful addition. When I print the window later, I find that the hidden attribute of the window is YES, so I set it to NO and it succeeds.

 

/** Display ad page method*/

- (void)showSplashScreenWithTime:(NSInteger )ADShowTime;

/** Display time of ad map*/

@property (nonatomic, assign) NSInteger ADShowTime;

/** Picture path*/

@property (nonatomic, copy) NSString *imgFilePath;

/** url address of the image*/

@property (nonatomic, copy) NSString *imgLinkUrl;

/** Effective time of ad map*/

@property (nonatomic, copy) NSString *imgDeadline;

 

2. Realize the method of displaying the advertisement page, which will be displayed according to the cut-off time of the saved advertisement map and whether there is the picture. To compare the date sequence, you can first convert the current date and the saved ad map into the same date format, compare the two, and determine the date sequence according to the ascending or descending order of the comparison results.

 

- (void)showSplashScreenWithTime:(NSInteger)ADShowTime

{

_ADShowTime = ADShowTime;

[_countButton setTitle:[NSString stringWithFormat:@"skip%ld",ADShowTime] forState:UIControlStateNormal];

//

NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];

dataFormatter.dateFormat = @"MM/dd/yyyy HH:mm";

//Get the time of the current system and convert it with the corresponding format

[dataFormatter stringFromDate:[NSDate date]];

NSString *currentDayStr = [dataFormatter stringFromDate:[NSDate date]];

NSDate *currentDate = [dataFormatter dateFromString:currentDayStr];

//The cut-off time of advertisements is also converted in the same format

NSString * timeStampString = self.imgDeadline;

//Date format conversion method of time stamp

//    NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;

//    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];

//    NSString *deadlineStr = [dataFormatter stringFromDate:date];

//    NSDate *dateA = [dataFormatter dateFromString:deadlineStr];

//     NSDate *deadlineDate = [dataFormatter dateFromString:_imgDeadline];

NSDate *deadlineDate = [dataFormatter dateFromString:_imgDeadline];

NSLog(@"Current date:%@ Save to:%@", currentDayStr, timeStampString);

NSComparisonResult result;

result = [deadlineDate compare:currentDate];

/**

*  Compare the saved date with the current date. If the current date is less than the saved date, the ad page can be displayed. Otherwise, the ad page will not be displayed

*/

if (result == NSOrderedAscending) {

[self dismiss];

}else{

[self startTimer];

UIWindow *window = [[UIApplication sharedApplication].delegate window];

window.hidden = NO;

[window addSubview:self];

}

}

 

3. Request advertising data. When requesting data, first determine whether the image exists, if not, delete the old image, and then download the new image. When deleting photos, you need to pay attention to the following: first, judge whether the saved picture name is consistent with the picture in the local sandbox. If it is inconsistent, it means that the picture has been updated. At this time, delete the old picture in the sandbox. If it is consistent, you don't need to delete it. Otherwise, you can't find the saved picture.

 

+ (void)getAdvertisingImageData

{

NSArray *imageArray = @[@"http://img1.126.net/channel6/2016/022471/0805/2.jpg?dpi=6401136", @"http://image.woshipm.com/wp-files/2016/08/555670852352118156.jpg"];

NSString *imageUrl = imageArray[0];

NSString  *imgLinkUrl = @"http://www.jianshu.com/users/e4c63b354a77/latest_articles";

NSString  *imgDeadline =  @"08/30/2016 14:25";

// Get picture name

NSArray *stringArr = [imageUrl componentsSeparatedByString:@"/"];

NSString *imageName = stringArr.lastObject;

// Splicing sandbox path

NSString *filePath = [self getFilePathWithImageName:imageName];

BOOL isExist = [self isFileExistWithFilePath:filePath];

if (!isExist){// If the picture does not exist, delete the old picture and download the new picture

[self downloadAdImageWithUrl:imageUrl imageName:imageName imgLinkUrl:imgLinkUrl imgDeadline:imgDeadline];

}

}

/**

*  Download new pictures

*/

+ (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName imgLinkUrl:(NSString *)imgLinkUrl imgDeadline:(NSString *)imgDeadline

{

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

UIImage *image = [UIImage imageWithData:data];

NSString *filePath = [self getFilePathWithImageName:imageName]; // Name of the saved file

[UIImageJPEGRepresentation(image, 0) writeToFile:filePath atomically:YES];

if ([UIImageJPEGRepresentation(image, 0) writeToFile:filePath atomically:YES]) {

// Saved successfully

//Judge whether the saved picture name is consistent with the picture in the local sandbox. If it is inconsistent, it means that the picture is updated. At this time, delete the old picture in the sandbox first. If it is consistent, delete the cache and download again. At this time, delete is not necessary, otherwise the saved picture cannot be found

if (![imageName isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:adImageName] ]) {

[self deleteOldImage];

}

[[NSUserDefaults standardUserDefaults] setValue:imageName forKey:adImageName];

[[NSUserDefaults standardUserDefaults] setValue:imgLinkUrl forKey:adUrl];

[[NSUserDefaults standardUserDefaults] setValue:imgDeadline forKey:adDeadline];

[[NSUserDefaults standardUserDefaults] synchronize];

}else{

NSLog(@"Save failed");

}

});

}

/**

*  Delete old picture

*/

+ (void)deleteOldImage

{

NSString *imageName = [[NSUserDefaults standardUserDefaults] valueForKey:adImageName];

if (imageName) {

NSString *filePath = [self getFilePathWithImageName:imageName];

NSFileManager *fileManager = [NSFileManager defaultManager];

[fileManager removeItemAtPath:filePath error:nil];

[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:adImageName];

[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:adUrl];

[[NSUserDefaults standardUserDefaults] setValue:@"" forKey:adDeadline];

[[NSUserDefaults standardUserDefaults] synchronize];

}

}

 

The final effect is as follows:

 

 

demo address

Posted by gutogouveia on Thu, 04 Jun 2020 09:38:43 -0700