ask notification when open lib

This commit is contained in:
Chris Li 2016-09-19 17:31:16 -04:00
parent ff6626320a
commit 368bdbbfaa
7 changed files with 59 additions and 5 deletions

View File

@ -23,6 +23,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Preference.activeUseHistory.append(NSDate()) Preference.activeUseHistory.append(NSDate())
} }
func registerNotification() {
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
// MARK: - // MARK: -
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
@ -30,8 +35,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Network.shared Network.shared
// Register notification // Register notification
// let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) // Here are the notification permission the app wants if let _ = Preference.libraryLastRefreshTime { registerNotification() }
// application.registerUserNotificationSettings(settings)
// Set background refresh interval // Set background refresh interval
application.setMinimumBackgroundFetchInterval(86400) application.setMinimumBackgroundFetchInterval(86400)
@ -140,6 +144,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationDidEnterBackground(application: UIApplication) { func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
if #available(iOS 10, *) {
let notification = UILocalNotification()
notification.alertTitle = NSLocalizedString("Book download finished", comment: "Notification: Book download finished")
notification.alertBody = NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished")
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
} }
func applicationWillEnterForeground(application: UIApplication) { func applicationWillEnterForeground(application: UIApplication) {
@ -147,7 +159,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) { func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {
// Network.shared.rejoinSessionWithIdentifier(identifier, completionHandler: completionHandler) Network.shared.rejoinSessionWithIdentifier(identifier, completionHandler: completionHandler)
} }
// MARK: Background Refresh // MARK: Background Refresh

View File

@ -49,7 +49,7 @@
</dict> </dict>
</array> </array>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1.8.859</string> <string>1.8.871</string>
<key>ITSAppUsesNonExemptEncryption</key> <key>ITSAppUsesNonExemptEncryption</key>
<false/> <false/>
<key>LSRequiresIPhoneOS</key> <key>LSRequiresIPhoneOS</key>

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1.8.862</string> <string>1.8.874</string>
<key>NSExtension</key> <key>NSExtension</key>
<dict> <dict>
<key>NSExtensionMainStoryboard</key> <key>NSExtensionMainStoryboard</key>

View File

@ -19,5 +19,21 @@
landmarkType = "5"> landmarkType = "5">
</BreakpointContent> </BreakpointContent>
</BreakpointProxy> </BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Kiwix-iOS/AppDelegate.swift"
timestampString = "496013317.729722"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "149"
endingLineNumber = "149"
landmarkName = "applicationDidEnterBackground(application:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints> </Breakpoints>
</Bucket> </Bucket>

View File

@ -16,6 +16,9 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
let context = NSManagedObjectContext.mainQueueContext let context = NSManagedObjectContext.mainQueueContext
private(set) var operations = [String: DownloadBookOperation]() private(set) var operations = [String: DownloadBookOperation]()
private var downloadedBookTitle = [String]()
private var completionHandler: (()-> Void)?
private override init() { private override init() {
super.init() super.init()
queue.delegate = self queue.delegate = self
@ -29,6 +32,11 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
return NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil) return NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}() }()
func rejoinSessionWithIdentifier(identifier: String, completionHandler: ()-> Void) {
guard identifier == session.configuration.identifier else {return}
self.completionHandler = completionHandler
}
// MARK: - OperationQueueDelegate // MARK: - OperationQueueDelegate
func operationQueue(queue: OperationQueue, willAddOperation operation: NSOperation) { func operationQueue(queue: OperationQueue, willAddOperation operation: NSOperation) {
@ -46,6 +54,19 @@ class Network: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSe
func operationQueue(queue: OperationQueue, willProduceOperation operation: NSOperation) {} func operationQueue(queue: OperationQueue, willProduceOperation operation: NSOperation) {}
// MARK: - NSURLSessionDelegate
func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
NSOperationQueue.mainQueue().addOperationWithBlock {
self.completionHandler?()
let notification = UILocalNotification()
notification.alertTitle = NSLocalizedString("Book download finished", comment: "Notification: Book download finished")
notification.alertBody = NSLocalizedString("All download tasks are finished.", comment: "Notification: Book download finished")
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
}
// MARK: - NSURLSessionTaskDelegate // MARK: - NSURLSessionTaskDelegate
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {

View File

@ -20,6 +20,10 @@ class RefreshLibraryOperation: GroupOperation {
process.injectResultFromDependency(retrive) process.injectResultFromDependency(retrive)
super.init(operations: [retrive, process]) super.init(operations: [retrive, process])
addObserver(WillExecuteObserver { _ in
(UIApplication.sharedApplication().delegate as! AppDelegate).registerNotification()
})
process.addObserver(DidFinishObserver { [unowned self] (operation, errors) in process.addObserver(DidFinishObserver { [unowned self] (operation, errors) in
guard let operation = operation as? Process else {return} guard let operation = operation as? Process else {return}
self.hasUpdate = operation.hasUpdate self.hasUpdate = operation.hasUpdate

View File

@ -1,3 +1,4 @@
// //
// ZimReader.m // ZimReader.m
// KiwixTest // KiwixTest