2016-07-04 4 views
1

오후 9시에 발생하고 일주일에 한 번 반복하도록 로컬 알림을 예약하려고합니다. 다음 코드가 있는데 "NSDateComponents 형식의 값을 .fireDate에 할당 할 수 없습니다 .fireDate는 NSDate 형식 만 수락하지만 시간 변수와 분 변수를 설정하는 방법을 찾을 수는 없습니다."라는 오류 메시지가 계속 나타납니다. 있는 NSDate 유형에 대해 나는이 작업을 수행 할 수 있다고 생각하지 않기 때문에특정 시간에 대한 로컬 알림 설정

func Notification(){ 

    let dateComponents = NSDateComponents() 
    dateComponents.day = 4 
    dateComponents.month = 7 
    dateComponents.year = 2016 
    dateComponents.hour = 21 
    dateComponents.minute = 00 

    let Notification = UILocalNotification() 

    Notification.alertAction = "Go Back To App" 
    Notification.alertBody = "Did you complete your HH and QC?" 
    Notification.repeatInterval = NSCalendarUnit.WeekOfYear 
    Notification.timeZone = NSTimeZone.defaultTimeZone() 

    Notification.fireDate = dateComponents 
    UIApplication.sharedApplication().scheduleLocalNotification(Notification) 
} 
+0

구글 캘린더 방법 dateFromComponents 에 근무 –

답변

1

는 당신이 그것을하는 방법이다

을.

F ully 참고 스위프트 2.0에

//을 테스트 나는이 기능은 화재 날짜를 생성에 도움이 될 것입니다 날짜 선택

func scheduleLocalNotification() { 
     let localNotification = UILocalNotification() 

     localNotification.fireDate = fixNotificationDate(datePicker.date) 
     localNotification.alertBody = "Hey, you must go shopping, remember?" 
     localNotification.alertAction = "View List" // Change depending on your action 

     localNotification.category = "reminderCategory" 

     UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 
    } 

//에서 날짜 시간을 촬영했다.

func fixNotificationDate(dateToFix: NSDate) -> NSDate { 
    let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: dateToFix) 

    dateComponets.second = 0 

    let fixedDate: NSDate! = NSCalendar.currentCalendar().dateFromComponents(dateComponets) 

    return fixedDate 
} 

푸시 알림에 대한 권한을 요청하는 것을 잊지 마십시오. 내 프로젝트에

링크는 내가 https://drive.google.com/open?id=0B2csGr9uKp1DR0ViZFZMMEZSdms

0

당신은 같은 날짜 구성 요소에서 날짜를 얻을해야합니다.

여기
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)! 
calendar.timeZone = NSTimeZone.localTimeZone() 
calendar.locale = NSLocale.currentLocale() 
let date = calendar.dateFromComponents(dateComponents)! 
Notification.fireDate = date