I've created a function that will allow the user to share content via message, email, etc. However, I've only been able to include an image and a URL as separate objects. I would like to include a small image that has the url embedded, so the user can just tap the image. Is there any way to do this?
func shareApp() {
let textToShare = "Checkout this app!"
let imageToShare = UIImage(named: “ProductIcon”)
if let myWebsite = URL(string: "http://www.productwebsite.ca")
{
let objectsToShare = [textToShare, imageToShare, myWebsite] as [Any]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.view.backgroundColor = UIColor.clear
//Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivity.ActivityType.airDrop, UIActivity.ActivityType.addToReadingList]
self.present(activityVC, animated: true, completion: nil)
}
}
Related
I have a situation. I launch an activity controller to share files but I want my application not to appear in the view.
if let url = FileManager.filePath(forKey: documentModel.localUrl ?? "", directory: "file") {
let documento = NSData(contentsOf: url)
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType(rawValue: "mx.com.myapp.txt")]
activityViewController.popoverPresentationController?.sourceView=self.view
present(activityViewController, animated: true, completion: nil)
I have an array of contacts fetched from a contact list. I want to take these contacts and use them as recipients of my UIActivityController.
var contactLocations = [URL]() // this should be filled with the contact URLs
let textToShare = "Hello, this is the link:"
var objectsToShare = [textToShare, url] as [Any]
objectsToShare.append(self.contactLocations)
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = sender.customView
self.present(activityVC, animated: true, completion: nil)
You can't specify recipients but just the data you want to share with UIActivityViewController.
I want to share an URL file that has been created in the app and a text. But it seems it only can share text and not any data like URL or UIImage.
The code I am using:
let sharedVideo = Video(Title: _data[1], VideoID: _data[0], Duration: _data[3], ViewCount: _data[2])
let sharedURL = VideoManager.exportData(video: sharedVideo)
let shareItems:Array = [sharedURL,"check this out baby!"] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Also I use an UIImage object instead of sharedURL to see If it has problem with URL or not.
It doesn't work even with an image file. When I click on share button inside UIActivityViewController, It works just for text, no URL nor image.
I am using Swift 3 in Xcode 8.
Thanks.
PS: I am sure about sharedURL object that it isn't nil nor undefined.
Try this:
#IBAction func btnExport(sender: AnyObject)
{
let someText:String = "Hello want to share text also"
let objectsToShare:URL = URL(string: "http://www.google.com")!
let sharedObjects:[AnyObject] = [objectsToShare as AnyObject,someText as AnyObject]
let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook,UIActivityType.postToTwitter,UIActivityType.mail]
self.present(activityViewController, animated: true, completion: nil)
}
Simplified solution working with Swift 5+
let url = URL(string: shareUrlString)!
let text = "Some text that you want shared"
let activity = UIActivityViewController(activityItems: [url, text], applicationActivities: nil)
present(activity, animated: true)
#IBAction func shareButtonClicked(sender: UIButton) {
let textToShare = "Swift is awesome! Check out this website about it!"
if let myWebsite = NSURL(string: "http://www.codingexplorer.com/") {
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = sender
self.presentViewController(activityVC, animated: true, completion: nil)
}
}
Hi, I add sms sharing to my app, everything works fine except I'd like to be able to CANCEL an SMS after I started it.
No cancel Button is displayed in the uinavigationbar for SMS while it is present for Mail, Twitter or Facebook.
Thanks for your help
I want to share an URL scheme link to social app so that when the user open social page on safari, when they click on the link, the URL scheme will call to the app I set. But when I share, it only share the text. How to share the link instead of the text.
Here is the code:
#IBAction func shareButtonAction(sender: AnyObject) {
print("share")
let myShare = "TestSafari://"
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
}
You probably need to put a text first then the url, in that particular order. Try this:
let string: String = ...
let URL: NSURL = ...
let shareVC = UIActivityViewController(activityItems: [string, URL])
let activityViewController = UIActivityViewController(
activityItems: [image],
applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeMessage,UIActivityTypeMail,UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop];
presentViewController(activityViewController, animated: true, completion: nil)
use this code for Present ActivityViewController only change activityItems to your string.