ExtensionDelegate.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*****************************************************************************
  2. * ExtensionDelegate.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import WatchKit
  13. import WatchConnectivity
  14. import CoreData
  15. import MediaLibraryKit
  16. class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate, NSFileManagerDelegate {
  17. func applicationDidFinishLaunching() {
  18. // Perform any final initialization of your application.
  19. let additionalOptions = [NSReadOnlyPersistentStoreOption : NSNumber(bool: true)]
  20. let library = MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary
  21. library.additionalPersitentStoreOptions = additionalOptions
  22. WCSession.defaultSession().delegate = self;
  23. WCSession.defaultSession().activateSession()
  24. }
  25. func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
  26. let msg = VLCWatchMessage(dictionary: message)
  27. if msg.name == VLCWatchMessageNameNotification, let payloadDict = msg.payload as? [String : AnyObject] {
  28. if let name = payloadDict["name"] as? String {
  29. handleRemoteNotification(name, userInfo: payloadDict["userInfo"] as? [String : AnyObject])
  30. }
  31. }
  32. }
  33. func handleRemoteNotification(name:String, userInfo: [String: AnyObject]?) {
  34. NSNotificationCenter.defaultCenter().postNotificationName(name, object: self, userInfo: userInfo)
  35. }
  36. func session(session: WCSession, didReceiveFile file: WCSessionFile) {
  37. let fileType = file.metadata?["filetype"] as? String ?? ""
  38. if fileType == "coredata" {
  39. dispatch_sync(dispatch_get_main_queue())
  40. {
  41. self.copyUpdatedCoreDataDBFromURL(file.fileURL)
  42. }
  43. }
  44. }
  45. func copyUpdatedCoreDataDBFromURL(url:NSURL) {
  46. let library = MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary
  47. library.overrideLibraryWithLibraryFromURL(url)
  48. NSNotificationCenter.defaultCenter().postNotificationName(VLCDBUpdateNotification, object: self)
  49. }
  50. }