Background Fetch / Swift
27 Nov 2018 | swift2018-12-01-BackgroundFetch
Targets - Capabilities에 Background fetch
를 켜줍니다. / Test환경: Xcode 10, swift 4
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
if #available(iOS 10.0 , *){ //10이후에만 작동하므로.
UNUserNotificationCenter.current().requestAuthorization(options: .badge) { (granted, error) in
if error != nil {
// success!
}
}
}
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("haha 실행됨.")
UIApplication.shared.applicationIconBadgeNumber = 20
//completionHandler(.newData)
//completionHandler(.noData)
}
실행하자마자 앱을 내리고( 홈버튼 을 누르고)
Xcode - Debug - Simulate Background Fetch를 선택하면 print 문이 출력되는 것을 확인할 수 있습니다. 또한 다음처럼 아이콘이 바뀝니다.
끝.
참고: http://avilos.codes/mobile/ios-swift/ios-swift-멀티태스킹multitasking-백그라운드background-페치fetch/
Comments