我有一个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
这是已知的错误吗?我已经向苹果报告过了,但如果有解决办法,我会很乐意使用的。
最佳答案
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
}
关于swift - 无法读取操作扩展中的共享文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32330599/