Search code examples
iphoneswiftios8sharingios-app-extension

Sharing Extensions in IOS Apps


I am new to the overall iOS Programming - Swift. I am wondering that if I create a project in xCode6 and then I would like to share an image that I took from my project for example to evernote, google drive, messages, email, facebook, twitter,so on and so on.

So, how would I do that? Is it something, I need to enable from xcode6 for each sharing extension or I need to implement swift code to enable these sharing extensions?

What kind of configurations I might needs to change to get sharing extensions (mentioned above) enabled? Or what kind of code changes in a project are possible? Are there any examples or samples out there with the instructions that I can look into and see how they implement into xcode6.

I never work with the sharing extensions before, so any knowledge that can be shared will be a great resource for me. Thanks in advance!


Solution

  • Use the Xcode Share Template.

    There is a lot of tutorials online to know how to make share extensions using Swift, here are some of them and some good articles about extensions you should know too :

    Even you could do it in code too in the following way:

    @IBAction func shareSheet(sender: AnyObject){
    
        let firstActivityItem = "Hey, check out this mediocre site that sometimes posts about Swift!"
    
        let secondActivityItem : NSURL = NSURL(fileURLWithPath: "http://www.dvdowns.com/")!
    
        let activityViewController : UIActivityViewController = UIActivityViewController(
        activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
    
        activityViewController.excludedActivityTypes = [
          UIActivityTypePostToWeibo,
          UIActivityTypePrint,
          UIActivityTypeAssignToContact,
          UIActivityTypeSaveToCameraRoll,
          UIActivityTypeAddToReadingList,
          UIActivityTypePostToFlickr,
          UIActivityTypePostToVimeo,
          UIActivityTypePostToTencentWeibo
        ]
    
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }
    

    The above code only works for iPhones because for iPad you need to specify the sourceView and show it as a popover in the following way:

    activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton) 
    

    The above line must be put in just after you init the activityViewController and it should be work.

    I hope this help you.