IOS to do something in a specific time, there are similar to the needs of the wonderful can be consulted

Source: Internet
Author: User

When we start the project to pop up an advertisement window, a very simple function, the server to determine the satisfaction of the condition, that is, the return data, the client to determine the location of the data null, pop popup window
But the boss said, this will take a short time to pop, every day, 12 o'clock Noon to 2 o'clock in the afternoon can not play out
This, this, a little bit of us, every day to save a key, a little pit it
Think about it, no need ah, this is the test of our operation of time, the code is implemented as follows:

Code implementation
#pragma mark--to determine if a pop-up ad-(BOOL) isloadadvertin{Current time stampNstimeinterval interval = [[NSDate Date] timeIntervalSince1970];Nstimeinterval time00 = [Gddateutil setcurrentlydaysethour:XX Setminute:XX setsec:00];Timestamp of current day 00 pointNstimeinterval time12 = [Gddateutil setcurrentlydaysethour:Setminute:XX setsec:00];Timestamp of current day 12 pointNstimeinterval time14 = [Gddateutil setcurrentlydaysethour:Setminute:XX setsec:00];Timestamp of current day 14 pointNstimeinterval Time24 = [Gddateutil setcurrentlydaysethour:Setminute:XX setsec:00];Timestamp of current day 24 pointif (Interval > time00 && interval < time12) {The time period is between 00-12 pointsSo judging the current time is definitely 00 after interval > time00Interval < time12 as long as it matches, the current time is before 12 o'clock in the morning.if (![ [[Nsuserdefaults Standarduserdefaults] Objectforkey:@ "Kamyes"] isequaltostring:[NSString stringWithFormat:@ "% @Kam", [Gddateutil Stringfromdatesetdateformat:@ "YYMMDD"]]) {The Nsuserdefaults value Kamyes determine whether this morning has been started [[Nsuserdefaults Standarduserdefaults] setobject:[NSString stringWithFormat:@ "% @Kam", [Gddateutil Stringfromdatesetdateformat:@ "YYMMDD"] Forkey:@ "Kamyes"];Meet this condition description is not started, store today's value, then return YesReturnYES; }ReturnNO; }Elseif (Interval > time14 && interval < Time24) {Time is between 12-24 pointsInterval < Time24 The current time is definitely less than 24 points, if it is greater than the other day, because [Gddateutil setcurrentlydaysethour:xx setminute:xx setsec:xx] The method is to get the time of day, so there is no case of Interval>time24Interval > Time14 Description The current time is after 14 pointsif (![ [[nsuserdefaults Standarduserdefaults] objectforkey:@ "KpmYES "] Isequaltostring:[nsstring stringwithformat:@"% @Kpm ", [ Gddateutil stringfromdatesetdateformat:@ "YYMMDD"]]) {// In Nsuserdefaults value Kpmyes Determine whether this afternoon has been launched [[nsuserdefaults Standarduserdefaults] setObject:[ nsstring stringwithformat:@ "% @Kpm", [Gddateutil Stringfromdatesetdateformat:@ "YyMMdd"]] forkey:@ "KpmYES"]; //satisfies this condition stating that it has not been started, storing today's value, returns yes} return NO;} return no;}         

Inside the note is very clear, I may have a class in the code, the code is as follows:

Gets the time stamp of the current number of days in a few minutes

[Gddateutil setcurrentlydaysethour:xx setminute:xx setsec:xx]

/** * Gets the current number of days time stamp of a few minutes * * @return return timestamp */+ (Nstimeinterval) Setcurrentlydaysethour: (Nsinteger) hour Setminute: (Nsinteger) minute Setsec: (Nsinteger) sec{if (hour = =24) {If it is 24 o'clock, then the second day of 00 points, so do fine processing hour =23; minute =59; SEC =59; }Nscalendar *grecalendar = [[Nscalendar Alloc] Initwithcalendaridentifier:Nscalendaridentifiergregorian];Nstimezone *timezone = [[Nstimezone Alloc] Initwithname:@ "Asia/shanghai"]; [Grecalendar Settimezone:timezone];Nsdatecomponents *datecomponents = [Grecalendar components:Nscalendarunityear | Nscalendarunitmonth | nscalendarunitday fromdate:[nsdate Date]]; //Define a Nsdatecomponents object, set a point in time nsdatecomponents *datecomponentsfordate = [[nsdatecomponents alloc] INIT]; [Datecomponentsfordate SetDay:dateComponents.day]; [Datecomponentsfordate SetMonth:dateComponents.month]; [Datecomponentsfordate SetYear:dateComponents.year]; [Datecomponentsfordate Sethour:hour]; [Datecomponentsfordate Setminute:minute]; [Datecomponentsfordate setsecond:sec]; nsdate *datefromdatecomponentsfordate = [Grecalendar datefromcomponents:datecomponentsfordate]; return [Datefromdatecomponentsfordate timeIntervalSince1970];}        
Get time and convert to string

[Gddateutil Stringfromdatesetdateformat:xxx]

/** *  获取时间,并转换为字符串 * *  @return return 返回字符串 */+ (NSString *)stringFromDateSetDateFormat:(NSString *)formatString{    NSDate *date =[NSDate date]; NSDateFormatter *formater = [[NSDateFormatter alloc] init]; [formater setDateFormat:formatString];//设置时间显示的格式,此处使用的formater格式要与字符串格式完全一致,否则转换失败 NSString *dateStr = [formater stringFromDate:date];//将日期转换成字符串 return dateStr;}



Links: Http://www.jianshu.com/p/674bf1b42cd7

Thanks for sharing

IOS to do something in a specific time, there are similar to the needs of the wonderful can be consulted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.