2

I have been trying to access a file with my share extension, if the file is from photos app, I can access it, the path for the file is:

file:///var/mobile/Media/PhotoData/OutgoingTemp/11485686-0D4A-4637-AA50-B14D83E18B01/RenderedPhoto/IMG_4807.JPG

but when I try to access a file on the "private" path, I cannot access it, like an MS word file:

file:///private/var/mobile/Containers/Data/Application/196D7E75-87B6-4C69-B97A-74E58DF4AD65/tmp/ShareAttachments/%7BE73A233D-D10E-814D-AEB1-0C8C8AC9BFAA%7D/Document%20(7).docx

So my question is, how to access the file? apps like whatsApp or slack can have access to the file and attach it...

How to access this file? are they using "app groups"? how do they get the id for the app group? or how to access the file?

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
  • What happens when you try to access `docx` file? The file doesn't exist or it throws any errors? It's better if you can provide where is `docx` file kept in – trungduc Jun 18 '18 at 08:31
  • @trungduc says it doesn’t exist. Xx.docx is on path as above on private/var thanks – manuelBetancurt Jun 18 '18 at 08:44
  • Actually, if it's a problem with `private/`, you can remove it from url by using `URLByResolvingSymlinksInPath` – trungduc Jun 18 '18 at 08:53
  • Or maybe this one http://easynativeextensions.com/sharing-files-between-an-air-app-and-an-ios-extension/#step_3 – trungduc Jun 18 '18 at 09:12
  • What do you mean by "I cannot access it"? Is there an error? What are your NSExtensionActivationRule settings in your extension Info.plist? This would be expected to be a problem if you did not declare something like: NSExtensionActivationSupportsFileWithMaxCount. – David J Jun 24 '18 at 23:17

3 Answers3

1

For Share Extension, you need to get the file from NSItemProvider, which is passed by the host app to your share extension.

For a file, it may be a Data type or a URL type. If it is data, you need to cast it to the type you need. If it is a url, you can load it.

Owen Zhao
  • 3,205
  • 1
  • 26
  • 43
0

Please follow the given link:- https://developer.apple.com/app-extensions/

Thank you.

Shriram

Shriram Kadam
  • 394
  • 1
  • 4
  • 20
  • Hi, the share extension is working fine already, what I cannot do is to access the MS word file from my extension... I can call my extension from the "Send a copy/ send with another app", on MS word app, but then, the file is not accessed... – manuelBetancurt Jun 21 '18 at 23:21
0

May I please ask, how are you accessing this file? "file:///var/mobile/Media/PhotoData/OutgoingTemp/11485686-0D4A-4637-AA50-B14D83E18B01/RenderedPhoto/IMG_4807.JPG" from your main app.

I am using onOpenUrl:

      .onOpenURL { url in
            // Handle the deep link URL here
            if url.scheme == "PhotoEditor",
               let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
               let shareURLString = components.queryItems?.first(where: { $0.name == "share_url" })?.value
               {
                // Use the shared image URL in your app
                // Example: pass it to a view model or trigger appropriate logic
                let shareURL = URL(fileURLWithPath: shareURLString)
                loadFrom(URLAdress: shareURL)
            }
        }

And this is my loadImage function:

    DispatchQueue.global().async {
        do {
            if URLAdress.startAccessingSecurityScopedResource() {
                let imageData = try Data(contentsOf: URLAdress)
                if let loadedImage = UIImage(data: imageData) {
                      ///My codes
                    }
                }
            }
        } catch {
            print("Error reading image data: \(error)")