无法读取操作扩展程序

时间:2015-09-01 11:36:01

标签: swift ios-extensions

我有一个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报告过,但是如果有解决方法,我会很乐意使用它。

1 个答案:

答案 0 :(得分: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
}