2012-01-22 2 views
1

사용자가 날짜 선택 도구에서 날짜를 입력 할 수있는 앱을 작성한 후 버튼을 탭하면 로컬 알림을 예약합니다. 유일한 문제는 단추를 누를 때 바로 알림이 시작된다는 것입니다. 어떤 도움을 많이 주시면 감사하겠습니다! 당신은 조금 나중에 알림을 예약하는 방법에 대한 전체 코드를 찾고 있다면 여기 (추천 초 말한다)iOS : 간단한 지역 알림 3 일 전

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDate *currentDate = [self.datePicker date]; 

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
[dateComponents setDay:-3]; 

NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0]; 

[dateComponents release]; 

... 

답변

4

: 여기 내 코드입니다 전체 코드 :

참고 : 앱 내부에있는 경우 화면 상단에 UIApplication 대리인을 통해 처리해야 할 수도있는 메시지 상자가 표시되지 않습니다.

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDate *currentDate = [[NSDate alloc] init]; 

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
[dateComponents setSecond: 3 ]; 

NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0]; 

localNotification.fireDate = targetDate; 

localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

localNotification.alertBody = @"Notified"; 
localNotification.alertAction = @"Show"; 
localNotification.soundName = UILocalNotificationDefaultSoundName; 
localNotification.applicationIconBadgeNumber = 1; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
+1

모든 dateComponents 항목을 해당 코드로 바꾸고 targetDate를 사용하여 로컬 알림을 설정하십시오. –

+1

또한 부수적으로,'targetDate'가 현재 날짜보다 늦은 지 확인하는 것이 좋습니다. 과거에 로컬 알림을 시도하고 예약 할 때 어떤 동작인지 모르겠습니다. –

+0

감사합니다. 알림이 실행되지 않았지만 현재 코드로 OP를 업데이트해야합니까? – John

1

: 당신은 대신 [NSCalendar dateByAddingComponents:toDate:options:]를 사용할 필요가

- (IBAction)scheduleNotifButton:(id)sender { 
     NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDate *currentDate = [self.datePicker date]; 

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
[dateComponents setDay:-3]; 

NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0]; 

     UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
     if (localNotif == nil) 
      return; 
     localNotif.fireDate = targetDate; 
     localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

     localNotif.alertBody = @"Event is in 3 days!"; 
     localNotif.alertAction = nil; 

     localNotif.soundName = UILocalNotificationDefaultSoundName; 
     localNotif.applicationIconBadgeNumber = 0; 

     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

    }