2017-02-09 29 views
3

有沒有辦法從其Notification-Service-Extension實例中獲取保存在iOS應用的沙箱中的數據?在傳遞內容之前,我想從數據庫中獲取一些數據。iOS:如何從UNNotificationServiceExtension中獲取應用程序的保存數據?

我從

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { 
      ... 
      print("NotificationService didReceive UNNotificationRequest, contentHandler: \(contentHandler)") 

      let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] 
      let filePath = "\(documentsPath)/Database.db" 

      print(" documentsPath: \(filePath)") 

      let fileManager = FileManager.default 
      var isDir : ObjCBool = false 
      if fileManager.fileExists(atPath: filePath, isDirectory:&isDir) { 
       if isDir.boolValue { 
        print(" file exists and is a directory") 

       } else { 
        print(" file exists and is a NOT a directory") 
       } 
      } else { 
       print("file does not exist") 
      }... 

內部測試,對我來說,它看起來像擴展有它自己的沙盒有自己的文件夾。

該應用程序在'/ var/mobile/Containers/Data /'內使用'Application'文件夾,而擴展名使用'PluginKitPlugin'文件夾。

UPDATE:似乎無法訪問應用程序沙箱容器。

回答

3

從應用程序擴展編程指南,應用程序擴展可以​​使用應用程序組。

閱讀:

if let defaults = UserDefaults(suiteName: "my.app.group") { 
    if let value = defaults.value(forKey: "key") { 
     NSLog("\(value)") 
    } 
} 

寫:

if let defaults = UserDefaults(suiteName: "my.app.group") { 
    defaults.set("value", forKey: "key") 
} 

另外

  • MMWormhole:消息iOS應用和擴展之間傳遞。
  • Wormhole:iOS應用程序和擴展之間消息傳遞的更優雅方式。
+0

這很有趣謝謝。但是當我想要訪問我的應用程序數據庫時,我需要將其複製到共享容器中,而不是我想要的解決方案。但確實很有趣。 – headkit

+0

所以當你編輯你的答案並添加'不可能'時,我可以將其標記爲已接受。 – headkit

相關問題