|
import Share from 'react-native-share'; |
|
import {CameraRoll} from '@react-native-camera-roll/camera-roll'; |
|
|
|
const shareImage = async (platform = 'default',uri) => { |
|
try { |
|
const shareOptions = { |
|
title: 'title', |
|
message: `message`, |
|
url: uri, |
|
filename: 'yourImage', |
|
type: 'image/*', |
|
}; |
|
|
|
if (platform === 'default') { |
|
if (Platform.OS === 'ios') { |
|
delete shareOptions.message; |
|
} |
|
await Share.open(shareOptions); |
|
return; |
|
} else if (platform === 'twitter') { |
|
const isTwitterInstalled = await Linking.canOpenURL( |
|
'twitter://user?screen_name=mshivam0019', |
|
); |
|
if (!isTwitterInstalled) { |
|
return; |
|
} |
|
shareOptions.social = Share.Social.TWITTER; |
|
} else if (platform === 'instagram') { |
|
if (Platform.OS === 'ios') { |
|
const fd = await CameraRoll.save(uri, {type: 'photo'}); |
|
const last = await CameraRoll.getPhotos({ |
|
first: 1, |
|
assetType: 'All', |
|
}); |
|
shareOptions.url = last.edges[0].node.image.uri; |
|
} |
|
const isInstagramInstalled = await Linking.canOpenURL( |
|
'instagram://user?username=mshivam019', |
|
); |
|
if (!isInstagramInstalled) { |
|
return; |
|
} |
|
shareOptions.social = Share.Social.INSTAGRAM; |
|
} else if (platform === 'whatsapp') { |
|
const isWhatsappInstalled = await Linking.canOpenURL( |
|
'whatsapp://send?text=hello', |
|
); |
|
if (!isWhatsappInstalled) { |
|
return; |
|
} |
|
shareOptions.social = Share.Social.WHATSAPP; |
|
} else if (platform === 'download') { |
|
// Save to camera roll |
|
await CameraRoll.save(uri, {type: 'photo'}); |
|
//You can trigger a toast or alert to show the user that the image has been saved to camera roll |
|
return; |
|
} |
|
await Share.shareSingle(shareOptions); |
|
} catch (e) { |
|
console.log(e); |
|
} |
|
}; |