Переглянути джерело

Add support for App Extension target

tripleCC 9 роки тому
батько
коміт
a9194219b1
1 змінених файлів з 16 додано та 3 видалено
  1. 16 3
      Sources/ImageCache.swift

+ 16 - 3
Sources/ImageCache.swift

@@ -511,15 +511,17 @@ extension ImageCache {
     It will be called automatically when `UIApplicationDidEnterBackgroundNotification` received.
     It will be called automatically when `UIApplicationDidEnterBackgroundNotification` received.
     */
     */
     @objc public func backgroundCleanExpiredDiskCache() {
     @objc public func backgroundCleanExpiredDiskCache() {
-        
+        // if 'sharedApplication()' is unavailable, then return
+        guard let sharedApplication = UIApplication.kf_sharedApplication() else { return }
+
         func endBackgroundTask(inout task: UIBackgroundTaskIdentifier) {
         func endBackgroundTask(inout task: UIBackgroundTaskIdentifier) {
-            UIApplication.sharedApplication().endBackgroundTask(task)
+            sharedApplication.endBackgroundTask(task)
             task = UIBackgroundTaskInvalid
             task = UIBackgroundTaskInvalid
         }
         }
         
         
         var backgroundTask: UIBackgroundTaskIdentifier!
         var backgroundTask: UIBackgroundTaskIdentifier!
         
         
-        backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
+        backgroundTask = sharedApplication.beginBackgroundTaskWithExpirationHandler { () -> Void in
             endBackgroundTask(&backgroundTask!)
             endBackgroundTask(&backgroundTask!)
         }
         }
         
         
@@ -660,3 +662,14 @@ extension Dictionary {
         return Array(self).sort{ isOrderedBefore($0.1, $1.1) }.map{ $0.0 }
         return Array(self).sort{ isOrderedBefore($0.1, $1.1) }.map{ $0.0 }
     }
     }
 }
 }
+
+#if !os(OSX) && !os(watchOS)
+// MARK: - For App Extensions
+extension UIApplication {
+    public static func kf_sharedApplication() -> UIApplication? {
+        let selector = NSSelectorFromString("sharedApplication")
+        guard respondsToSelector(selector) else { return nil }
+        return performSelector(selector).takeRetainedValue() as? UIApplication
+    }
+}
+#endif