react-native-receive-sharing-intent icon indicating copy to clipboard operation
react-native-receive-sharing-intent copied to clipboard

Images are shared correctly, while screenshots are not

Open valerioleo opened this issue 4 years ago • 3 comments

Hi @ajith-ab, thanks for this very helpful package.

I have successfully implemented it into my app so that I can share photos and do some manipulation to them, and this works totally fine. What I am struggling with is managing to share also screenshots on iOS that, for some reason, aren't opening the app as it happens with photos coming from Camera Roll.

I have also tried to debug it in Xcode by putting breakpoints into the ShareViewController.swift file and they are correctly hit when sharing form Camera Roll while nothing happens when sharing a screenshot.

I recorded a tiny video where you can see that when trying to share a screenshot, I get a greyed screen for a second like it is actually attempting to do something, but without success. No errors or logs in console whatsoever.

I hope this can shed some light!

https://user-images.githubusercontent.com/13155741/106790509-f5609b80-6653-11eb-878d-c6ecb53fd5e7.mov

Context:

"react-native": "0.62.2",
"react-native-receive-sharing-intent": "^1.0.3",

iOS: 14.4

valerioleo avatar Feb 03 '21 18:02 valerioleo

Hi @ajith-ab, thanks for this very helpful package.

I have successfully implemented it into my app so that I can share photos and do some manipulation to them, and this works totally fine. What I am struggling with is managing to share also screenshots on iOS that, for some reason, aren't opening the app as it happens with photos coming from Camera Roll.

I have also tried to debug it in Xcode by putting breakpoints into the ShareViewController.swift file and they are correctly hit when sharing form Camera Roll while nothing happens when sharing a screenshot.

I recorded a tiny video where you can see that when trying to share a screenshot, I get a greyed screen for a second like it is actually attempting to do something, but without success. No errors or logs in console whatsoever.

I hope this can shed some light!

share-screenshot.mov Context:

"react-native": "0.62.2",
"react-native-receive-sharing-intent": "^1.0.3",

iOS: 14.4

Check the guide for iOS here (ShareViewController.swift) I have modified the ShareViewController.swift to share a screenshot from the iOS editor.

Screen Shot 2021-04-06 at 18 02 31 Screen Shot 2021-04-06 at 18 02 57

ccvlad avatar Apr 06 '21 15:04 ccvlad

Thanks @ccvlad, I will give it a try now!

valerioleo avatar Apr 07 '21 14:04 valerioleo

The above answer is in the form of an image. I will post the answer in code below.

private func handleImages (content: NSExtensionItem, attachment: NSItemProvider, index: Int) {


   attachment.loadItem(forTypeIdentifier: imageContentType, options: nil) { [weak self] data, error in
     
     if error == nil, let this = self {

 
       var url: URL? = nil 


       if let dataURL = data as? URL { url = dataURL }
       else if let imageData = data as? UIImage { url = this.saveScreenshot(imageData) }

       let fileExtension = this.getExtension(from: url!, type: .image)
       let newName = UUID().uuidString
       let newPath = FileManager.default
         .containerURL(forSecurityApplicationGroupIdentifier: "group.\(this.hostAppBundleIdentifier)")!
         .appendingPathComponent("\(newName).\(fileExtension)")
       let copied = this.copyFile(at: url!, to: newPath)

       if(copied) {
         this.sharedMedia.append(SharedMediaFile(path: newPath.absoluteString, thumbnail: nil, duration: nil, type: .image))
       }
       
       // If this is the last item, save imagesData in userDefaults and redirect to host app
       if index == (content.attachments?.count)! - 1 {
         let userDefaults = UserDefaults(suiteName: "group.\(this.hostAppBundleIdentifier)")
         userDefaults?.set(this.toData(data: this.sharedMedia), forKey: this.sharedKey)
         userDefaults?.synchronize()
         this.redirectToHostApp(type: .media)
       }
       
     } else {
       self?.dismissWithError()
     }
   }
 }
 

  private func documentDirectoryPath () -> URL?  {
    
    let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return path.first
  }

  private func saveScreenshot(_ image: UIImage) -> URL?
  {
    var screenshotURL: URL? = nil
    if
    let screenshotData = image.pngData(),
    let screenshotPath = documentDirectoryPath()?.appendingPathComponent("Screenshot.png")
    {
   try? screenshotData.write(to: screenshotPath)
   screenshotURL = screenshotPath
    }
    return screenshotURL
} 

Gautham495 avatar Jul 27 '23 03:07 Gautham495