How to make URL String with linkable when sharing it - ios

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)

Related

ios - format the information shared with share sheet

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.

using the activityController to share label

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().

Sending WhatsApp messages with multiple media types in Swift

I am trying to send both an audio clip and a url in one message via whatsapp.
I am using the following code to send audio:
let activityItem = URL.init(fileURLWithPath: Bundle.main.path(forResource: “Audio1”, ofType: “mp3")!)
let activityVC = UIActivityViewController(activityItems: [activityItem],applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view
self.present(activityVC, animated: true, completion: nil)
and this code to send a url:
let urlString = "Visit this website: http://www.google.com"
let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let url = NSURL(string: "whatsapp://send?text=\(urlStringEncoded)")
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}
I have tried a number of things to send them together but haven't been able to figure it out. Does anybody know if I can send both at once and if so, how would I do it?

UIActivityViewController and UIImage preview

I'm trying to share an images using UIActivityViewController (on iMessage, Telegram, WhatsApp and other).
I'm doing:
let image = ... // an image
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
But the image is cropped very badly. I've also tried to resize the image before the user share action, using UIActivityItemSource, but the result change depending the user device and the image
How can I achieve a perfect preview on all device and apps? Any advice or reference?
Here an example of the final result:
The first image is the image shared as Sticker in iMessage and the second using UIActivityViewController
Try to compress image before use UIActivityViewController
var compressedImage: Data? = UIImageJPEGRepresentation(yourImage, 0.8)
var docsPath: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as? String ?? ""
var imagePath: String = URL(fileURLWithPath: docsPath).appendingPathComponent("image.jpg").absoluteString
var imageUrl = URL.init(fileURLWithPath: imagePath)
do {
try compressedImage?.write(to: imageUrl, options: .atomic)
} catch {
print(error)
}
// save the file
var activityViewController = UIActivityViewController(activityItems: ["Check this image", imageUrl], applicationActivities: nil)

How Can I share text and Image Together on Whatsapp

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

Resources