3

I have an action extension for Safari that needs to read a file shared in my app's group. The code looks like this:

func doneWithResults(resultsForJavaScriptFinalizeArg: [NSObject: AnyObject]?) {
    let fileManager = NSFileManager()
    let groupUrl = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.App")
    let sharedContainerPathLocation = groupUrl?.path

    var text = ""
    let textPath = sharedContainerPathLocation! + "/test.txt"
    if let content = fileManager.contentsAtPath(textPath) {
        text = String(data: content, encoding: NSUTF8StringEncoding)!
    }
    // Code to tell that we're complete here, not important for showing the bug.
}

Running this on the simulator perfectly works. However, when running it on a real device that also has the file that I'm looking for nothing happens when clicking the action extension and it returns these logs:

Sep  1 13:29:29 iPhone ReportCrash[16049] <Notice>: Formulating report for process[16048] Action
Sep  1 13:29:29 iPhone MobileSafari[15761] <Warning>: plugin App.Action interrupted
Sep  1 13:29:29 iPhone MobileSafari[15761] <Warning>: plugin App.Action invalidated
Sep  1 13:29:29 iPhone ReportCrash[16049] <Warning>: report not saved because it is non-actionable

Is this a known bug? I already reported it to Apple but if there's a workaround I'll gladly use it.

Armand Grillet
  • 3,229
  • 5
  • 30
  • 60

1 Answers1

0
let sharedContainer = fileManager
    .containerURLForSecurityApplicationGroupIdentifier(
        "<YOUR APP GROUP IDENTIFIER HERE>")

let dirPath = sharedContainer?.path
sharedFilePath = dirPath?.stringByAppendingPathComponent(
        "sharedtext.doc")

if fileManager.fileExistsAtPath(sharedFilePath!) {
    let databuffer = fileManager.contentsAtPath(sharedFilePath!)
    textView.text = NSString(data: databuffer!, 
    encoding: NSUTF8StringEncoding)
}

sharedDefaults = NSUserDefaults(
suiteName: "<YOUR APP GROUP IDENTIFIER HERE>")

let switchSetting = sharedDefaults?.boolForKey("switch")

if let setting = switchSetting {
    mySwitch.on = setting
}