Share Sheet
The share sheet is a built-in iOS feature that allows users to easily share content via Messages, Mail, social media, and other apps.
Implementation
- Integrate with Your View Controller
- Add the following method to
MainViewController.swift
to handle sharing text and URLs:
- Add the following method to
swift
@objc func shareContent(_ params: [String: String]) {
var itemsToShare: [Any] = []
// Check if text exists in params
if let text = params["text"], !text.isEmpty {
itemsToShare.append(text)
}
// Check if a valid URL exists in params
if let urlString = params["url"], let url = URL(string: urlString) {
itemsToShare.append(url)
}
// Ensure there's something to share
guard !itemsToShare.isEmpty else {
print("Nothing to share!")
return
}
let activityViewController = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
// iPad safety: Set sourceView to avoid crashes
if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
// Present the share sheet
self.present(activityViewController, animated: true)
}
- Trigger the Share Sheet in JavaScript
- To trigger share sheet from JavaScript, call the native
shareContent
method:
- To trigger share sheet from JavaScript, call the native
javascript
enClose({
nativeCall: 'shareContent',
data: {
text: 'TEXT TO SHARE',
/*
(optional)
url: 'https://www.example.com'
*/
}
});