Behdad
Behdad

Reputation: 1629

UIActivityController to share URL file

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.

Upvotes: 6

Views: 24127

Answers (2)

budiDino
budiDino

Reputation: 13557

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)

Upvotes: -1

Sanjeet Verma
Sanjeet Verma

Reputation: 571

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)

}

Upvotes: 19

Related Questions