Expensify / react-native-share-menu

A module for React Native that adds your app to the share menu of the device
MIT License
645 stars 235 forks source link

Crash when sharing image data on iOS #215

Open iqqmuT opened 2 years ago

iqqmuT commented 2 years ago

There seems to be a bug that causes a crash on iOS at least when sharing from Medium app.

The fix: imageUrl should be imageURL in ios/Modules/ShareMenuReactView.swift.

diff --git a/ios/Modules/ShareMenuReactView.swift b/ios/Modules/ShareMenuReactView.swift
index e290cce..7380ee4 100644
--- a/ios/Modules/ShareMenuReactView.swift
+++ b/ios/Modules/ShareMenuReactView.swift
@@ -147,7 +147,7 @@ public class ShareMenuReactView: NSObject {
                                         // Writing the image to the URL
                                         try imageData.write(to: imageURL)

-                                        results.append([DATA_KEY: imageUrl.absoluteString, MIME_TYPE_KEY: imageURL.extractMimeType()])
+                                        results.append([DATA_KEY: imageURL.absoluteString, MIME_TYPE_KEY: imageURL.extractMimeType()])
                                     } catch {
                                         callback(nil, NSException(name: NSExceptionName(rawValue: "Error"), reason:"Can't load image", userInfo:nil))
                                     }
decode33 commented 1 year ago

Great. Thanks.

JesusTectronic commented 9 months ago

In the ShareMenuReactView class, I noticed a variable name inconsistency in the extractDataFromContext function.

The issue is in this block of code:

let imageData: Data! = image.pngData();

// Creating a temporary URL for image data (UIImage)
guard let imageURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("TemporaryScreenshot.png") else {
  return
}

do {
  // Writing the image to the URL
  try imageData.write(to: imageURL)

  results.append([DATA_KEY: imageUrl.absoluteString, MIME_TYPE_KEY: imageURL.extractMimeType()])
} catch {
  callback(nil, NSException(name: NSExceptionName(rawValue: "Error"), reason:"Can't load image", userInfo:nil))
}

The temporary variable defined for "TemporaryScreenshot.png" is imageURL, but it is incorrectly used as imageUrl here:

results.append([DATA_KEY: imageUrl.absoluteString, MIME_TYPE_KEY: imageURL.extractMimeType()])

To fix this issue, you should change it to:

results.append([DATA_KEY: imageURL.absoluteString, MIME_TYPE_KEY: imageURL.extractMimeType()])

Add this in your ShareViewController in the storeFile function ShareViewController

if let uiImage = data as? UIImage {
  let fileExtension = "png"
  let fileName = UUID().uuidString
  let filePath = groupFileManagerContainer
                  .appendingPathComponent("\(fileName).\(fileExtension)")

  guard let rawData: Data = uiImage.pngData() else {
     self.exit(withError: "Error while getting raw data")
     return
  }

  guard FileManager.default.createFile(atPath: filePath.path, contents: rawData) else {
      self.exit(withError: "Error while createFile")
      return
  }

  self.sharedItems.append([DATA_KEY: filePath.absoluteString, MIME_TYPE_KEY: "image/png"])
  semaphore.signal()
  return
}