3

我有一个 Safari 的操作扩展,它需要读取在我的应用程序组中共享的文件。代码如下所示:

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.
}

在模拟器上运行它非常有效。但是,当在具有我正在寻找的文件的真实设备上运行它时,单击操作扩展名并返回以下日志时没有任何反应:

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

这是一个已知的错误?我已经向 Apple 报告了它,但如果有解决方法,我会很乐意使用它。

4

1 回答 1

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
}
于 2018-10-17T20:11:25.383 回答