iOS launcher and open ad map, similar to NetEase
The launch diagram is an essential part of the iOS development process, and many apps will have a customized open-screen ad map after the launch, and clicking on the ad can jump to the page that the ad map corresponds to. Today, share with you how to add this ad map and click on the jump of the ad map. This ad map is implemented by adding Uiimageview to the UIWindow.
I. Adding a local launch map
1. Prepare local images
2. Locate the Images.xcassets file in the project, open the Launchimage, and drag the picture to the corresponding place, such as:
3. Turn off the use as Launch screen option on the right side of the Launchscreen.storyboard file (do not select this)
4. Select Launch Images source as Launchimage, and then empty the launch screen file.
5. You can set the time of the start chart stay in Appdelegate
Start page stays for 1 seconds
[Nsthread sleepfortimeinterval:1];
6. Run the project to see the set up diagram (if not, you can delete the app first, then run it again)
Two. Add an open-screen ad map
The implementation of this ad map is based on the author Zhou Yiqiang's app to start loading Ads page ideas, and then added some small additions. Because ads are time-sensitive, not every time you start to display this ad page. So the principle here is that the data returned in the background includes the ad image, the URL for the ad image, and the deadline for the ad. When you run the program, the ad map data is requested and the data is saved locally. Run again from the local save picture taken out, at the same time to determine whether the expiration date of the image is valid, if the picture exists and within the validity period, then the ad map is displayed; Request the ad map data again to avoid new ad data.
1. Create the custom Splashscreenview. When I added the ImageView to the window, I encountered an unsuccessful addition, and when I printed the window, I found that the hidden property of window was yes, so it succeeded after setting it to No.
/** Display Ad Page method */
-(void) Showsplashscreenwithtime: (Nsinteger) Adshowtime;
/** the display time of the ad map * *
@property (nonatomic, assign) Nsinteger adshowtime;
/** Picture Path */
@property (nonatomic, copy) NSString *imgfilepath;
URL address for/** image */
@property (nonatomic, copy) NSString *imglinkurl;
/** the effective time of the ad map * *
@property (nonatomic, copy) NSString *imgdeadline;
2. Implement the method of displaying the ad page, which will be displayed according to the cut-off time of the saved ad map and whether there is a picture. Comparing the dates, you can convert the current date and the saved ad map to the same date format, compare the two, and judge the date by 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";
Gets the time of the current system and transforms it with the appropriate format
[Dataformatter stringfromdate:[nsdate Date]];
NSString *currentdaystr = [dataformatter stringfromdate:[nsdate Date]];
NSDate *currentdate = [Dataformatter datefromstring:currentdaystr];
Ad cut-off time, also in the same format to convert
NSString * timestampstring = self.imgdeadline;
Date format conversion method for timestamp
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:%@ Due Date:%@", Currentdaystr, timestampstring);
Nscomparisonresult result;
result = [Deadlinedate compare:currentdate];
/**
* Compare the saved date to the current date, if the current date is less than the saved time, then you can display the ad page, or vice versa
*/
if (result = = nsorderedascending) {
[Self dismiss];
}else{
[Self starttimer];
UIWindow *window = [[uiapplication sharedapplication].delegate window];
Window.hidden = NO;
[Window addsubview:self];
}
}
3. Request AD data. When requesting data, first determine if the picture exists, if it does not exist, first delete the old picture, and then go to download the new picture. Delete on the photos need to be aware of, first to determine the name of the saved image and the local sandbox in the picture is consistent, if not consistent, the picture is updated, delete the old picture in the sandbox, if the same, you do not need to delete the operation, otherwise cannot 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;
Stitching the Sandbox path
NSString *filepath = [self getfilepathwithimagename:imagename];
BOOL isexist = [self isfileexistwithfilepath:filepath];
if (!isexist) {//If the picture does not exist, delete the old picture, download the new picture
[Self Downloadadimagewithurl:imageurl imagename:imagename imglinkurl:imglinkurl imgdeadline:imgdeadline];
}
}
/**
* Download a new image
*/
+ (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]; Save the name of the file
[UIImageJPEGRepresentation (image, 0) Writetofile:filepath Atomically:yes];
if ([UIImageJPEGRepresentation (image, 0) Writetofile:filepath Atomically:yes]) {
Saved successfully
Determine the name of the saved image and the local sandbox in the picture is consistent, if not consistent, the picture has been updated, the first delete the old picture in the sandbox, if the consistent description is to delete the cache after downloading again, then do not need to delete the operation, or cannot find the saved picture
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 pictures
*/
+ (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
Demo Address
iOS launcher and open ad map, similar to NetEase