2017-02-09 13 views
3

iOS 앱의 샌드 박스에 저장된 데이터에 Notification-Service-Extension 인스턴스 내부에서 접근 할 수있는 방법이 있습니까? contenhandler를 전달하기 전에 데이터베이스에서 데이터를 가져 오려고합니다. 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 /'안에 'PluginKitPlugin'폴더를 사용하는 동안 앱은 'Application'폴더를 사용합니다.

업데이트 : 앱 샌드 박스 컨테이너에 연결할 수없는 것으로 보입니다.

답변

3

앱 확장 프로그램 가이드에서 앱 확장은 앱 그룹을 사용하여 Sharing Data with Your Containing App 수 있습니다.

읽기 :

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

쓰기 : 또한

  • MMWormhole

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

    : 아이폰 OS 앱과 확장 사이를 통과하는 메시지.

  • Wormhole : iOS 앱과 확장 프로그램간에 메시지를 전달하는보다 세련된 방법입니다.
+0

감사합니다. 하지만 내 애플 리케이션 데이터베이스에 액세스하려면 공유 컨테이너에 복사해야 할 것이다. 그러나 참으로 흥미 롭습니다. – headkit

+0

그래서 답을 수정하고 '불가능'을 추가하면 허용으로 표시 할 수 있습니다. – headkit