2017-01-08 1 views
-1

내 앱에 백그라운드 프로세스가 있습니다. 코드는 다음과 같습니다.ios9의 배경 타이머

@available(iOS 10.0, *) 
func startTimer() { 
    timer2?.invalidate() 
    timer2 = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (t) in 
     if UIApplication.shared.applicationState == .background { 
      NSLog("tic background \(UIApplication.shared.backgroundTimeRemaining)") 

      if UIApplication.shared.backgroundTimeRemaining < 10 { 

      } 

     } 

    }) 
    timer2?.fire() 
} 

그러나 iOS10에서만 작동합니다. 이전 버전의 iOS 용으로 다시 쓸 수있는 방법이 있습니까?

+1

iOS9의 문제가 무엇입니까? – shallowThought

+0

'scheduledTimer (withTimerInterva : repeatedats : 블록)'은 iOS 10 이상에서만 사용 가능합니다. –

+0

참조. http://stackoverflow.com/a/24007862/1457385 – shallowThought

답변

2

ios9 이하에는 블록 기반 타이머 API가 없습니다.

블록 대신 콜백 메소드를 사용하거나 제 3 자 솔루션을 사용하십시오. BlocksKit

이드하는 방법으로 이동 (수백이있다) :

func startTimer() { 
    timer2?.invalidate() 
    timer2 = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(ViewController.timerFired(_:)), userInfo: nil, repeats: true) 
    timer2?.fire() 
} 

@objc 
func timerFired(_ timer: Timer) { 
    NSLog("tic") 

    if UIApplication.shared.applicationState == .background { 
     NSLog("tic background \(UIApplication.shared.backgroundTimeRemaining)") 

     if UIApplication.shared.backgroundTimeRemaining < 10 { 

     } 

    } 
} 
+0

안녕하세요. 대답 해줘서 고마워. 이것은 올바른 해결책이라고 생각합니다. 그러나, 나는 콜백과 작업 코드를 작성할 수 없습니다. 제 경우에 대해 더 자세히 알려 주실 수 있습니까? 감사합니다 –

+0

안녕 안톤. 편집했습니다. 샘플을 추가했습니다 –

+0

큰 감사! 나는 그것이 작동하도록 고투하고있어. 방금 해결책을 줬어! #selector에서이 부분의 의미 (_ :)를 물어볼 수 있습니까? –