Not able to read from shared containor in app group ios

2.8k Views Asked by At

I have created an app group group.com.example.FoodTracker-qg, In the main view controller of the app I am downloading an image and storing inside the shared container but I am unable to use the same image in image view. I am getting the following error

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=256 "The file “EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg” couldn’t be opened." UserInfo={NSURL=/private/var/mobile/Containers/Shared/AppGroup/EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/stdlib/public/core/ErrorType.swift, line 178

Below is my code for writing and reading from shared container

//  ViewController.swift


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var mealName: UILabel!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let urlString = "https://static.pexels.com/photos/3247/nature-forest-industry-rails.jpg"
        try? Data(contentsOf: URL(string: urlString)!).write(to: getSharedFileUrl("image.jpg"))
        imageView.image = UIImage(data: try! Data(contentsOf: getSharedFileUrl("image.jpg")))                  
    }

    func getSharedFileUrl(_ fileName: String) -> URL {
        let fileManager = FileManager.default
        let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.example.FoodTracker-qg")
        return URL(string: url!.path.appending(fileName))!
    } 
}
4

There are 4 best solutions below

0
On BEST ANSWER

Code seems to be correct, your problem might be due to file name itself.... check url in error

/private/var/mobile/Containers/Shared/AppGroup/EC700C58-E9C9-480D-8EE2-B88A570A5728image.jpg

there's no "/"...it should look like 728/image.jpg

0
On

You append the filename. As a result, you are missing the slash / in between the path components, as JJAAXX44 correctly pinpointed.

Use appendingPathComponentinstead:

func getSharedFileUrl(_ fileName: String) -> URL {
    let fileManager = FileManager.default
    guard let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.com.example.FoodTracker-qg")
        else {
            fatalError() // handle error appropriate to your needs instead
    }

    return url.appendingPathComponent(fileName)
}
0
On

In getSharedFileUrl(_ fileName: String) -> URL why deconstruct the newly retrieved url from containerURL(.. just to create a new one?

You could do something like this instead:

return url!.appending(fileName)

This should fix your problem with the missing /.

I'm not sure I agree with those force wraps, or even with possibly blocking the Main Thread with Data(contentsOf: ..) though!

0
On

Your write action is not correct.So you can not be read you image form your shared container.

let appGroupIdentifier = "group.mark3"

extension Data {

    func writeToGroupContainer(filePath: String, completeHandle: @escaping (Bool, String) -> Void) {

        let url = Data.appGroupContainerURL(filePath: filePath)

        let result = FileManager.default.createFile(atPath: url.path, contents: self, attributes: nil)
        completeHandle(result, url.path)
    }

    static func appGroupContainerURL(filePath: String) -> URL {

        let fileManager = FileManager.default
        let groupURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)
        let url = groupURL!.appendingPathComponent(filePath)
        try? FileManager.default.createDirectory(atPath: url.deletingLastPathComponent().path, withIntermediateDirectories: true, attributes: nil)
        return url
    }

}

us this extension to write a data to your disk correct.

override func viewDidLoad() {
    super.viewDidLoad()

    let filePath = "images/group/mark3/avatar.png"
    let data: Data = UIImagePNGRepresentation(UIImage(named: "image.png")!)!
    data.writeToGroupContainer(filePath: filePath) { (done, file) in
        print(filePath, done)
    }

}