2015-05-29 60 views
3

我在iPhone應用程序開發方面有點新。所以如果這是一個蹩腳的問題,我首先抱歉。現在我的問題是。在我的應用程序用戶有時鐘撥號,他將選擇小時和分鐘。使用該小時和分鐘,我想根據報警系統設置用戶報警後,它會爲本地通知設置,我的本地通知工作只有鬧鐘時間顯示wrong.So到目前爲止,我有做過這樣的我在iOS中收到錯誤的通知時間?

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
calendar.timeZone = [NSTimeZone defaultTimeZone]; 
calendar.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]; 

NSDateComponents *currentTimeComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:[NSDate date]]; 
currentTimeComponents.hour = [_hourlLabel.text intValue]; 
currentTimeComponents.minute = [_minuteLabel.text intValue]; 
currentTimeComponents.second = 00; 
NSDate *date = [calendar dateFromComponents:currentTimeComponents]; 
NSLog(@"User set date: %@",date); 


UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.repeatInterval = NSCalendarUnitDay; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

// Schedule the notification 
localNotification.fireDate = date; 
localNotification.alertBody = @"It has notification from xxxx"; 
localNotification.alertAction = @"Show me the item"; 
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
localNotification.soundName = @"clockalarmfrog.mp3"; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
NSLog(@"User setup local notifications: %@",localNotification); 

NSTimeInterval secs = [localNotification.fireDate timeIntervalSinceDate:[NSDate date]]; 
NSLog(@"animation time duration in NSTimeInterval: %f",secs); 

現在假設用戶已經爲下一個鬧鐘時間設置了5:51 PM,當前時間是5:20 PM.Now使用我的代碼我得到了這個。

2015-05-29 17:20:11.626 xxxx[651:12248] 17:20:11 PM 
2015-05-29 17:21:05.988 xxxx[651:12248] User set date: 2015-05-28 23:51:00 +0000 
2015-05-29 17:21:06.099 xxxx[651:12248] User setup local notifications: <UIConcreteLocalNotification: 0x7fd9a8c7de60>{fire date = Friday, May 29, 2015 at 5:51:00 AM Bangladesh Standard Time, time zone = Asia/Dhaka (GMT+6) offset 21600, repeat interval = NSCalendarUnitDay, repeat count =  UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, May 30, 2015 at 5:51:00 AM Bangladesh Standard Time, user info = (null)} 
2015-05-29 17:21:06.099 xxxx[651:12248] animation time duration in NSTimeInterval: -41406.099645 
2015-05-29 17:21:06.099 xxxx[651:12248] animation reverse time duration in NSTimeInterval: 44993.900355 
2015-05-29 17:21:06.168 xxxx[651:12248] duration: 44993.900355 

回答

1

變化

localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

localNotification.timeZone = [NSTimeZone localTimeZone]; 
+0

我也試過。 (它不工作,其實AM/PM上的問題是設定PM/AM,但它總是計算AM。 – Emon

1

您需要設置帶AM/PM到本地通知fireDate產權當前時間格式,以便在當前時間火災報警。

下面的方法用於將您的小時,分​​鍾和週期(AM/PM)轉換爲當前日期格式..因此它會在指定的時間觸發鬧鐘。

//添加這個方法

- (NSDate *)dateModifiedWithHours:(NSString *)hours 
          minutes:(NSString *)minutes 
         andPeriod:(NSString *)period 
{ 
    NSDate *dateModified = NSDate.date; 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSMinuteCalendarUnit fromDate:dateModified]; 

    [components setMinute:minutes.intValue]; 

    int hour = 0; 

    if ([period.uppercaseString isEqualToString:@"AM"]) { 

     if (hours.intValue == 12) { 
      hour = 0; 
     } 
     else { 
      hour = hours.intValue; 
     } 
    } 
    else if ([period.uppercaseString isEqualToString:@"PM"]) { 

     if (hours.intValue != 12) { 
      hour = hours.intValue + 12; 
     } 
     else { 
      hour = 12; 
     } 
    } 
    [components setHour:hour]; 

    dateModified = [calendar dateFromComponents:components]; 

    return dateModified; 
} 

修改代碼如下

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
calendar.timeZone = [NSTimeZone defaultTimeZone]; 
calendar.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]; 

NSDateComponents *currentTimeComponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:[NSDate date]]; 
currentTimeComponents.hour = [_hourlLabel.text intValue]; 
currentTimeComponents.minute = [_minuteLabel.text intValue]; 
currentTimeComponents.second = 00; 
NSDate *date = [calendar dateFromComponents:currentTimeComponents]; 
NSLog(@"User set date: %@",date); 


UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.repeatInterval = NSCalendarUnitDay; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 


NSDate *firedModifiedDate = [self dateModifiedWithHours:@"5" 
               minutes:@"51" 
              andPeriod:@"pm"]; 

// Schedule the notification 
localNotification.fireDate = firedModifiedDate; 
localNotification.alertBody = @"It has notification from xxxx"; 
localNotification.alertAction = @"Show me the item"; 
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
localNotification.soundName = @"clockalarmfrog.mp3"; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
NSLog(@"User setup local notifications: %@",localNotification); 

NSTimeInterval secs = [localNotification.fireDate timeIntervalSinceDate:[NSDate date]]; 
NSLog(@"animation time duration in NSTimeInterval: %f",secs); 

然後您登錄看起來像下面

2015-05-29 17:21:06.099 xxxx[651:12248] User setup local notifications: <UIConcreteLocalNotification: 0x7fd9a8c7de60>{fire date = Friday, May 29, 2015 at 5:51:00 PM Bangladesh Standard Time, time zone = Asia/Dhaka (GMT+6) offset 21600, repeat interval = NSCalendarUnitDay, repeat count =  UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, May 30, 2015 at 5:51:00 AM Bangladesh Standard Time, user info = (null)} 

希望它可以幫助you..to修復。

+0

非常感謝。讓我檢查。 – Emon

+0

優秀的工作人員。非常感謝。 – Emon

+0

我正面臨關於你的答案的另一個問題,如果我使分鐘時間低於當前分鐘,那麼它顯示我錯誤的時間間隔。我能爲此做些什麼? – Emon

0

[NSDate date]總是返回GMT + 0日期,無論您的時區在哪裏。

你傳遞給的日期UILocalNotification有GMT + 0,而[localNotification.timezone]設置爲defaultTimeZone(在你的情況下是GMT + 6)。

// the time zone to interpret fireDate in. pass nil if fireDate is an absolute GMT time (e.g. for an egg timer). 
// pass a time zone to interpret fireDate as a wall time to be adjusted automatically upon time zone changes (e.g. for an alarm clock). 
@property(nonatomic,copy) NSTimeZone *timeZone; 

爲了獲得正確的預期輸出,可以設置localNotification.timeZone = nil;或相應地將日期更新爲defaultTimeZone(GMT +6)並設置爲fireDate。