UIActivityController to share URL file

16,276

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)

}
Share:
16,276
Behdad
Author by

Behdad

Updated on July 02, 2022

Comments

  • Behdad
    Behdad almost 2 years

    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.

  • Behdad
    Behdad over 7 years
    At first I tried the code you using image to share,and It works perfectly. Now I'm gonna try your edited code to see If it works for URL or not. Thank you so much.
  • Sanjeet Verma
    Sanjeet Verma over 7 years
    @BehdadAhmadi i am glad to help you if my answer is acceptable then upvote me.
  • Behdad
    Behdad over 7 years
    Thank you buddy,your answer works,how ever again failure with my own customized URL. My url consists of a NSDictionary that holds 4 string,1 base64encoded string and an Data object. I think its something wrong with my customized URL. However Thank you for sharing your time with me.
  • Sanjeet Verma
    Sanjeet Verma over 7 years
    @BehdadAhmadi Happy coding:)
  • Behdad
    Behdad over 7 years
    Thank you so much!