2015-01-13 5 views
3

안녕하세요 저는 신속한 IOS 프로그래밍에 익숙하지 않습니다. 하루 중 시간에 따라 하루에 6 번 사용자에게 알리는 6 가지 알림을 설정했습니다. 알림이 작동하지만 앱이 처음 시작될 때 어떤 이유로 6 개의 알림이 모두 알림 센터에 동시에 표시됩니다. 어떤 도움이라도 대단히 감사하겠습니다. 화재 날짜에 상관없이 앱을로드하자마자 UILocalNotification이 항상 표시됩니다.

이는 여섯 개 가지 통지
func prayerAlert (prayerName : String, prayHour : Int, prayMinute : Int) { 


     dateComp.year = Int(currentDate.year) 
     dateComp.month = Int(currentDate.month) 
     dateComp.day = Int(currentDate.day) 
     dateComp.hour = prayHour 
     dateComp.minute = prayMinute 
     dateComp.timeZone = NSTimeZone.systemTimeZone() 

     var calender : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! 
     var date : NSDate = calender.dateFromComponents(dateComp)! 

     var notification : UILocalNotification = UILocalNotification() 
     notification.alertBody = prayerName 
     notification.fireDate = date 



     UIApplication.sharedApplication().scheduleLocalNotification(notification) 
    } 

내 함수 인 AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Override point for customization after application launch. 



     let notificationTypes : UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge 
     let notificationSetting : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil) 
     UIApplication.sharedApplication().registerUserNotificationSettings(notificationSetting) 

     return true 
    } 

의 코드이며, 내가있는 viewDidLoad

에서 함수를 호출하고있어 곳이다
override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     pray.setTimeFormat(0) 


     self.locationManager.requestAlwaysAuthorization() 
     self.locationManager.requestWhenInUseAuthorization() 


     if CLLocationManager.locationServicesEnabled() { 
      self.locationManager.delegate = self 
      self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
      self.locationManager.startUpdatingLocation() 

     } 



     var timesArray = pray.getDatePrayerTimes(currentDate.year, andMonth: currentDate.month, andDay: currentDate.day, andLatitude: locationManager.location.coordinate.latitude, andLongitude: locationManager.location.coordinate.longitude, andtimeZone: pray.timeZone) 


      var convertedTime = convertPrayArray(timesArray as NSMutableArray) 

      prayerAlert("Time for Fajr", prayHour: convertedTime.hourArray[0], prayMinute: convertedTime.minuteArray[0]) 
      prayerAlert("Time for SunRise", prayHour: convertedTime.hourArray[1], prayMinute: convertedTime.minuteArray[1]) 
      prayerAlert("Time for Dhuhr", prayHour: convertedTime.hourArray[2], prayMinute: convertedTime.minuteArray[2]) 
      prayerAlert("Time for Asr", prayHour: convertedTime.hourArray[3], prayMinute: convertedTime.minuteArray[3]) 
      prayerAlert("Time for Maghrib", prayHour: convertedTime.hourArray[5], prayMinute: convertedTime.minuteArray[5]) 
      prayerAlert("Time for Isha", prayHour: convertedTime.hourArray[6], prayMinute: convertedTime.minuteArray[6]) 


    } 

답변

4

업데이트 : Xcode 7.1 • Swift 2.1

문제는 통지 대신 날짜 구성 요소를 localTime 영역으로 설정한다는 것입니다.

추가이 확장있는 NSDate이있는 NSDate에서 원하는 구성 요소를()

extension NSDate { 
    var minute: Int { return NSCalendar.currentCalendar().component(.Minute, fromDate: self)} 
    var hour: Int { return NSCalendar.currentCalendar().component(.Hour, fromDate: self)} 
    var day: Int { return NSCalendar.currentCalendar().component(.Day, fromDate: self)} 
    var month: Int { return NSCalendar.currentCalendar().component(.Month, fromDate: self)} 
    var year: Int { return NSCalendar.currentCalendar().component(.Year, fromDate: self)} 
    func fireDateAt(hr: Int, min: Int) -> NSDate { 
     let date = NSDate() 
     return NSCalendar.currentCalendar().dateWithEra(1, 
      year: year, 
      month: month, 
      day: { hr > date.hour || (hr == date.hour && min > date.minute) ? day : day + 1 }(), 
      hour: hr, 
      minute: min, 
      second: 0, 
      nanosecond: 0 
     )! 
    } 
} 

timeZone가 재산권

통지의 화재 날짜의 시간대 돌아갑니다.

fireDate에 지정된 날짜가이 속성의 값에 따라 해석됩니다. nil (기본값)을 지정하면 발화 일이 으로 절대 GMT 시간으로 해석되어 카운트 다운 타이머와 같은 에 적합합니다. 이 속성에 유효한 NSTimeZone 개체를 할당하면 발의 날짜는 월 표준 시간대가 변경 될 때 이 자동으로 조정되는 월 클럭 시간으로 해석됩니다. 이 경우에 적합한 예제는 알람 시계입니다.

func prayerAlert (prayerName : String, prayHour : Int, prayMinute : Int) { 
    var notification = UILocalNotification() 
    notification.timeZone = NSTimeZone.localTimeZone() 
    notification.alertBody = prayerName 
    notification.fireDate = NSDate().fireDateAt(prayHour, min: prayMinute) 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 
} 
+0

방금 ​​시도한 것과 동일한 동작입니다. 아무것도 변경되지 않았습니다. – gnm1978

+0

여전히 정확한 동작을 나타냅니다. – gnm1978

+0

아니요 동일한 동작이 아닙니다. 당신이 생각하고있는 바로 그것입니다. –

관련 문제