I am trying to send a music file from my app to WhatsApp using Apple Share sheet which is UIActivityViewController.
I am able to share the music file but the problem is I cannot add metadata to this. I mean I can share audio with url and it can be open and listen in WhatsApp but I cannot figure out how to add metadata to this url, I want to add title, description and image to my url as Apple Music do, then when shared in WhatsApp you can see some data with the file url, not only the audio file. Is It possible ?
When I open the share sheet with my code I can set metadata image, title and description in the header as you can see in the following image : https://i.stack.imgur.com/Ymzec.jpg
This following image is when I share an audio music file from my app to WhatsApp, you can see I want to share metadata too but I cannot add these datas : https://i.stack.imgur.com/3qHK9.jpg
The following image it is almost what I want, it is shared from Apple Music app to WhatsApp, you can see the metadata (image, title and some description text) : https://i.stack.imgur.com/WuVus.jpg
Is it possible to do the same thing as the third image in my app please ?
Here is my current code :
func openShareSheet(shareItem: ShareFile, completion: @escaping (String?) -> Void) {
var item: ShareItem = ShareItem(title: shareItem.title, artist: shareItem.artist, url: shareItem.url, coverImage: UIImage(named: "defaultImage"))
getImage(shareItem.coverImage) { image in
if let image = image {
item.coverImage = image
let itemToShare: [Any] = [
item
]
let activityViewController = UIActivityViewController(activityItems: itemToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.navigationController?.navigationBar
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.assignToContact]
activityViewController.completionWithItemsHandler = { (activityType, completed: Bool, returnedItems: [Any]?, error: Error?) in
if completed {
doSomething()
}
self.deleteFile(filePath: shareItem.url)
completion(nil)
}
self.present(activityViewController, animated: true, completion: nil)
} else {
completion("Image error")
}
}
}
import LinkPresentation
class ShareFile {
var title: String
var artist: String
var url: URL
var coverImage: String
init(title: String, artist: String, url: URL, coverImage: String) {
self.title = title
self.artist = artist
self.url = url
self.coverImage = coverImage
}
}
class ShareItem: UIActivityItemProvider {
var title: String
var artist: String
var url: URL
var coverImage: UIImage?
init(title: String, artist: String, url: URL, coverImage: UIImage?) {
self.title = title
self.artist = artist
self.url = url
self.coverImage = coverImage
super.init(placeholderItem: "")
}
override var item: Any {
return url
}
override func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return url
}
override func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return url
}
override func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return title
}
@available(iOS 13.0, *)
override func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata: LPLinkMetadata = LPLinkMetadata()
metadata.title = title
metadata.originalURL = URL(fileURLWithPath: artist)
metadata.url = url
metadata.imageProvider = NSItemProvider(object: coverImage ?? UIImage())
return metadata
}
}