Skip to content

Commit 14f2676

Browse files
authored
fix: crash on screenshot sharing (#99)
When we sharing a screenshot the share extension receives UIImage object not URL. So I handled the case
1 parent 6ab182f commit 14f2676

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

ios/Modules/ShareMenuReactView.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,33 @@ public class ShareMenuReactView: NSObject {
125125
}
126126
} else if (imageProvider != nil) {
127127
imageProvider.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil) { (item, error) in
128-
let url: URL! = item as? URL
129-
130-
callback(url.absoluteString, self.extractMimeType(from: url), nil)
128+
let imageUrl: URL! = item as? URL
129+
130+
if (imageUrl != nil) {
131+
if let imageData = try? Data(contentsOf: imageUrl) {
132+
callback(imageUrl.absoluteString, self.extractMimeType(from: imageUrl), nil)
133+
}
134+
} else {
135+
let image: UIImage! = item as? UIImage
136+
137+
if (image != nil) {
138+
let imageData: Data! = image.pngData();
139+
140+
// Creating temporary URL for image data (UIImage)
141+
guard let imageURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("TemporaryScreenshot.png") else {
142+
return
143+
}
144+
145+
do {
146+
// Writing the image to the URL
147+
try imageData.write(to: imageURL)
148+
149+
callback(imageURL.absoluteString, imageURL.extractMimeType(), nil)
150+
} catch {
151+
callback(nil, nil, NSException(name: NSExceptionName(rawValue: "Error"), reason:"Can't load image", userInfo:nil))
152+
}
153+
}
154+
}
131155
}
132156
} else if (textProvider != nil) {
133157
textProvider.loadItem(forTypeIdentifier: kUTTypeText as String, options: nil) { (item, error) in

0 commit comments

Comments
 (0)