我正在开发iPhone应用程序,其中我一度陷入困境,
我想做的是,
当用户第一次在一天内启动应用时,我想向他显示一些警报。当他在同一天第二次打开应用程序时,它将不会收到警报。 (他只会在第一天发布应用程序时收到警报。)
第二天,如果他再次第一次打开应用程序,他会得到警报,第二次他不会收到警报。
简而言之:用户应该在每天首次发布时获得提醒。
任何想法,我应该如何实现这一目标? 提前致谢。
答案 0 :(得分:1)
我有NSDate
类的有用类别,其中包括以下方法:
- (BOOL)isOnTheSameDayAs:(NSDate *)date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components1 = [calendar components:(NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:self];
NSDateComponents *components2 = [calendar components:NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit fromDate:date];
return ((components1.year == components2.year) &&
(components1.month == components2.month) &&
(components1.day == components2.day));
}
- (BOOL)isToday
{
return [self isOnTheSameDayAs:[NSDate date]];
}
做最必要的事情。稍后在app委托中,我会放置字段NSDate * lastReloadDate
,我将用“零日”初始化。最后一个动作:
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (![self.lastLoadDate isToday]) {
// Show error here and don't forget to hide previous alert view or use singleton for UIALertView.
self.lastLoadDate = [NSDate date];
}
}
- (NSDate *)lastLoadDate {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *d = [defaults objectForKey:@"LastReload"];
return d == nil ? [NSDate dateWithTimeIntervalSince1970:0] : d;
}
- (void)setLastLoadDate:(NSDate *)d {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:d forKey:@"LastReload"];
}
答案 1 :(得分:0)
首先,您需要测试应用程序是否在一天内第一次打开。如果你能做到这一点,那么只能在当天只显示一次警报很容易
//Check for new day
if (condition) {
//For every new day you will clear the user default value
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AlertShownToday"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//Below code will check wether the alert is shown for the day or not and accordingly will show alert
if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"AlertShownToday"] boolValue]) {
//Show Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test" message:@"Message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
//Save in userdefault that alert has been shown
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AlertShownToday"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
在didFinishLaunchingWithOptions方法
中使用上述代码希望这会对你有所帮助。快乐编码:)
答案 2 :(得分:-1)
当用户启动应用程序时,您可以将currentDate
存储在NSUserDefaults
中。现在,只要用户启动应用程序,请将currentDate
与storedDate
中的NSUserDefaults
进行比较。
if (currentDate != [[NSUserDefaults standardUserDefaults] objectForKey:@"LastDate"]) {
currentDate = [NSString stringWithFormat:@"%f",[NSDate date]];
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Day Changed" message:@"Welcome Back!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
希望这有帮助!!!