简体   繁体   中英

cannot access app group folder in intent handler

I have the following code in an intent handler for a widget I am creating:

extension FileManager {
static var appGroupURLAsText: String {
  `default`.containerURL(forSecurityApplicationGroupIdentifier: "group.com.emojiApp.EmojiWidget")!.absoluteString
}
}

extension IntentHandler: SelectEmojiIntentHandling {
  func provideEmojiOptionsCollection(
    for intent: SelectEmojiIntent,
    with completion: @escaping (INObjectCollection<EmojiINO>?, Error?) -> Void
  ) {
    
    
    print("stuff happening in intent handler")
    print(FileManager.appGroupURLAsText)

    let fm = FileManager.default
    print("after declaring fm")
    var items = [String]()
    do {
      print("inside of do")
        items = try fm.contentsOfDirectory(atPath: FileManager.appGroupURLAsText)
        print("after declaring items")
    } catch {
      print("Unexpected error: \(error).")
        // failed to read directory – bad permissions, perhaps?
    }
    //I don't believe the code below is relevant to the error but I'm including it here in case
    var emojiItems = [EmojiINO]()
    
    for item in items {
      let finalThing = EmojiINO(identifier: item, display: item)
      emojiItems.append(finalThing)
    }
    completion(INObjectCollection(items: emojiItems), nil)
  }
}

When I run the code in an iphone 13 pro simulator with ios 15.5, I get the following output:

stuff happening in intent handler
file:///Users/myname/Library/Developer/CoreSimulator/Devices/86836E5F-4CA8-4288-899F-0CA595F18525/data/Containers/Shared/AppGroup/4AD329B4-32C5-40EE-BEBF-BFC2BDDB34F9/
after declaring fm
inside of do
Unexpected error: Error Domain=NSCocoaErrorDomain Code=260 "The folder “4AD329B4-32C5-40EE-BEBF-BFC2BDDB34F9” doesn’t exist." UserInfo={NSUserStringVariant=(
Folder
),

It never gets to the after declaring items print statement so I know the issue is something with contentsOfDirectory . I know the folder is there though because appGroupUrl is returning a valid folder and I checked in my finder and the folder is there. How do I fix this?

So it turns out I should have used path instead of absoluteString .

So just change line 3 to this:

`default`.containerURL(forSecurityApplicationGroupIdentifier: "group.com.emojiApp.EmojiWidget")!.path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM