Sharing Rich Links in Swift - ios

I am trying to implement rich link sharing on posts within my app. Sharing a link in iMessage should show the video or image if there's one associated with the post. This swift implementation is not working, not sure if its my code or and issue with the website meta data
What do I need in my website to make this work? Is the current swift implementation
static func addURLPreview(urlString: String) {
let metaDataProvider = LPMetadataProvider()
let url = URL(string: urlString)
metaDataProvider.startFetchingMetadata(for: url!) { (metaData, error) in
guard let _ = error, let metaData = metaData else {
return
}
let vc = UIActivityViewController(activityItems: [metaData], applicationActivities: nil)
UIApplication.shared.getKeyWindow()?.rootViewController?.present(vc, animated: true)
}
}

Related

Add custom message with the invitation link to download the app - Swift

I would like to know how you can add a custom message through the applications like Messages, Mail, Whatsapp, Snapchat... when a user shares the link to download the app (to invite a friend). My current code is :
#objc private func inviteButtonTapped() {
guard let url = URL(string: "https://www.google.com") else {
return
}
let shareSheetVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)
present(shareSheetVC, animated: true)
}
I'm offering to share the link but when they select "Messages" for example, there's no custom message. Any idea ?

Share Image to Telegram on iOS

I am trying to share a picture from my app to Telegram. I want to do this without UIActivityViewController if possible. I have bellow the code that almost does exactly what I want. It opens telegram, it lets me choose who to send to, but it doesn't share the picture, only a url path.
This is the code that I've wrote:
In the following code, url is address to the image on the phone that I'm trying to share.
let telegramUrlString = "tg://msg_url?url=\(url)"
if let urlString = telegramUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let telegramUrl = URL(string: urlString) {
if UIApplication.shared.canOpenURL(telegramUrl) {
UIApplication.shared.open(telegramUrl, options: [:], completionHandler: nil)
} else {
completion(ErrorCode.CANNOT_SHARE_ON_TELEGRAM, nil)
}
}
}

How to share video on WhatsApp without using UIActivityViewController?

I am currently trying to enable the functionality to share a video on WhatsApp but without using UIActivityViewController, as Tik Tok does for example. When you click on "share" on WhatsApp, it directly redirects you to WhatsApp and contacts pop up so that you can share the video.
I kinda succeeded in sharing it but with ActivityViewController, this way:
let path = Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!.relativePath
let fileUrl = NSURL(fileURLWithPath: path)
controller = UIDocumentInteractionController(url: fileUrl as URL)
controller.uti = "net.whatsapp.movie"
controller.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
I would be glad to know someone can help me out.
Try using UIApplication's canOpenURL(_:) and open(_:options:completionHandler:),
let urlString = "https://stackoverflow.com/questions/60282744/how-to-share-video-on-whatsapp-without-using-uiactivityviewcontroller"
let shareString = "whatsapp://send?text=\(urlString)"
if let url = URL(string: shareString), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:]) { (completed) in
print(completed)
}
}

How to make iMessage to open my app with a custom document type

EDIT: As requested added info.pslist and code.
I have a custom Document Type and I have registered the UTIs and a new MIME type. I basically followed the steps by this tutorial. I am using a Codable object that it is no more than a JSON file with a custom extension and a custom icon. Honestly it looks pretty cool to me. The app I am doing is is a Grocery list app that makes a lot of sense being able to share it by a note or iMessage.
Like the finalised app in the tutorial I followed it opens in mail and even in notes!!! but iMessage does not recognise the extension and shows a folder icon and does not open it.
My question is how can I tell iMessage that this file is meant to be opened by my App. Do I need an iMessage extension? I am pretty new to iOS. info.pslist:
And now code:
func exportToUrl() -> URL? {
let contents = try? JSONEncoder().encode(shoppingList)
guard let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return nil
}
let saveUrl = path.appendingPathComponent("/list.grabgrocerieslist")
try? contents?.write(to: saveUrl, options: .atomic)
return saveUrl
}
#IBAction func sharedTapped(_ sender: UIBarButtonItem) {
guard let url = exportToUrl() else {
return
}
let activityController = UIActivityViewController(activityItems: ["Shopping List", url], applicationActivities: nil)
activityController.excludedActivityTypes = [.assignToContact, .saveToCameraRoll, .postToFacebook ]
activityController.popoverPresentationController?.barButtonItem = sender
self.present(activityController, animated: true, completion: nil)
}
Many Thanks,

IOS FacebookShare Error 'reserved' being returned

I've tried searching but could not find an answer. I have written an app and I am trying to share content to facebook. Basically I would like to share a URL and maybe a quote or title.
I keep getting an error called 'reserved' but I am not sure what it means or how to fix it. Any help would be great!
func fbClick() {
let content = LinkShareContent(url: URL(string: "www.google.com")!)
showShareDialog(content, mode: .native)
}
func showShareDialog<C: ContentProtocol> (_ content: C, mode: ShareDialogMode = .automatic) {
let dialog = ShareDialog(content: content)
dialog.presentingViewController = self
dialog.mode = mode
do {
try dialog.show()
} catch (let error) {
self.view.makeToast("Invalid share content. Failed to present share dialog with error \(error)", duration: 3.0, position: .top)
}
}
Figured it out.
This line...
let content = LinkShareContent(url: URL(string: "www.google.com")!)
Should have been like this...
let content = LinkShareContent(url: NSURL(string: "https://www.google.com")! as URL)
or like this
let content = LinkShareContent(url: NSURL(string: "https://www.google.com")! as URL, quote: quote)
Had the same reserved error but while using VideoShareContent. Spent 5 hours to find the issue and finally found. Really hope someone finds this helpful too.
Solution: when you are retrieving the url of your video from info param from the UIImagePickerController delegate method make sure you use the key "UIImagePickerControllerReferenceURL".
Example:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
picker.dismiss(animated: true)
if let videoURL = info["UIImagePickerControllerReferenceURL"] as? URL {
let video = Video(url: videoURL)
let content = VideoShareContent(video: video)
do {
try ShareDialog.show(from: self, content: content)
} catch {
print(error)
}
}
}
Additional info: initially I did not use this key "UIImagePickerControllerReferenceURL". Why: it's deprecated. According to Apple, you should use UIImagePickerControllerPHAsset instead. But the url from there also returns reserved error. Another try was to use key "UIImagePickerControllerMediaURL", but it also didn't succeed.

Resources