I am using the following code to present a share sheet:
let url = "https://somelink.com"
let image = "https://myserver/an_image.png"
let text = "Join now!"
let shareSheetVC = UIActivityViewController(activityItems: [image, url, text], applicationActivities: nil)
shareSheetVC.popoverPresentationController?.sourceView = sender
shareSheetVC.popoverPresentationController?.sourceRect = sender.frame
present(shareSheetVC, animated: true)
What I'd like is something like this:
However, when I share I just get
https://somelink.com
https://myserver/an_image.png
Join now!
How can I format this to accomplish something like the example?
This is the basic example to share Text, Image and URL using UIActivityViewController
let text = "Join Now!"
let image = UIImage(named: "your_image")
let myWebsite = URL(string: "https://somelink.com")
let shareAll = [text , image! , myWebsite]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = sender.frame
self.present(activityViewController, animated: true, completion: nil)
Hope you understand.
Alternatively, you could approach this from a different angle and try to fix your webpage by adding:
<meta property="og:image" content="https://myserver/an_image.png">
<title>Join now!</title>
Those tags will be picked up by default from the UIActivityViewController.
Related
Trying to share image and link in UIActivityController, But its only share link not the image
code snippet-
let model = self.memeFeedDataList[sender.tag]
let imageURL = NSURL(string: NetWorkingConstants.baseImageURL+model.imageUrl!) //https://xyz.png
let imagedData = NSData(contentsOf: imageURL! as URL)!
let img = [UIImage(data: imagedData as Data)]
let linkURL = NSURL(string:"https://stackoverflow.com/")
let shareAll = [img , linkURL! ] as [Any]
let activity = UIActivityViewController(activityItems: shareAll, applicationActivities: nil);
self.present(activity, animated: true, completion: nil)
Output screenshot-
When sharing URL String via email WhatsApp other sharable component its url formate is not linkable with underline
let encodedStr = dict!.base64EncodedString()
let appLink = "https://companyName.com/account/signupbyinvite?\(encodedStr)"
When sharing via email message box the url formate is not clickable and underline below like is not visible.
convert the url string in to url
guard let appUrl = URL(string : appLink) else {return}
then share with uiactivitycontroller link will auto clickable once its shared. like this
let activityViewController = UIActivityViewController(activityItems: [appUrl], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
I have the following code
let textToShare = "Check out the test original for \(String(describing: bookTitle.text))"
if let myWebsite = URL(string: "X") {//Enter link to your app here
let objectsToShare = [textToShare, myWebsite] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
//Excluded Activities
activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop, UIActivity.ActivityType.addToReadingList]
activityVC.popoverPresentationController?.sourceView = sender
self.present(activityVC, animated: true, completion: nil)
}
When i try to share the label text bookTitle.text, I get the output Optional("Test"). Is there a way to share UILabel text using the UIActivityViewController?
I get the output Optional("Test")
Actually, you're in the right way. Except you need to "unwrap" that optional property .text. You can do it in so many ways. One way is to force unwrap it (not recommended most of the time!) Like so:
let textToShare = "Check out the test original for \(bookTitle.text!)"
Or one more way is to use nil coalescing. Like so:
let textToShare = "Check out the test original for \(bookTitle.text ?? "")"
More about Optional Chaining https://docs.swift.org/swift-book/LanguageGuide/OptionalChaining.html
P.S no need to use String(describing:). You use it usually in print().
I want to share image and text together with swift.How Can I do That ?
let url = NSURL(string: "whatsapp://app")
let shareText = "Beni Bons'da ekle : \(PFUser.currentUser()!.username!). http://apple.co/29RMfNn"
let bonsCardImage = UIImage.renderUIViewToImage(bonsCardView)
let activityItems = [bonsCardImage]
let vc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
presentViewController(vc, animated: true, completion: nil)
Thanks
In my app, I want to share the current song and artist followed by the album artwork. Here is my code:
var sharingItems = [AnyObject]()
let currentSong = musicPlayer.nowPlayingItem?.title
let currentArtist = musicPlayer.nowPlayingItem?.artist
let artwork = musicPlayer.nowPlayingItem?.artwork
let artworkImage = artwork?.imageWithSize(CGSizeMake(100, 100))
let shareText = "Now playing \(currentSong!) by \(currentArtist!) via Ryan's awesome app"
sharingItems.append(shareText) //text first, then image
sharingItems.append(artworkImage!)
let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeOpenInIBooks, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll]
self.presentViewController(activityViewController, animated: true, completion: nil)
When I choose a share option, like Messges, the image is before the text in the TextField. Why is the image first if it's last in the array?