How do i add this sharing button? IOS8 with swift

前端 未结 1 1082
深忆病人
深忆病人 2020-12-15 14:52

I want there to be a button in my app, that when it is pressed, this(See image below) Pops up. How do i do that? I don\'t want to create a custom sharing extion, i just want

相关标签:
1条回答
  • 2020-12-15 15:13

    The way is using UIActivityViewController for example in the following way :

    @IBAction func shareSheet(sender: AnyObject) {
    
        let firstActivityItem = "Text you want"
        let secondActivityItem : NSURL = NSURL(string: "http//:urlyouwant")!
        // If you want to put an image
        let image : UIImage = UIImage(named: "image.jpg")!
    
        let activityViewController : UIActivityViewController = UIActivityViewController(
            activityItems: [firstActivityItem, secondActivityItem, image], applicationActivities: nil)
    
        // This lines is for the popover you need to show in iPad 
        activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)
    
        // This line remove the arrow of the popover to show in iPad
        activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
        activityViewController.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0)
    
        // Anything you want to exclude
        activityViewController.excludedActivityTypes = [
            UIActivityTypePostToWeibo,
            UIActivityTypePrint,
            UIActivityTypeAssignToContact,
            UIActivityTypeSaveToCameraRoll,
            UIActivityTypeAddToReadingList,
            UIActivityTypePostToFlickr,
            UIActivityTypePostToVimeo,
            UIActivityTypePostToTencentWeibo
        ]
    
        self.presentViewController(activityViewController, animated: true, completion: nil)
    }
    

    The above code works for both iPhone and iPad because in you set the new popoverPresentationController in iOS 8 it works for iPad too.

    In the case of use an UIBarButtonItem you need to replace this line:

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

    With this one:

    activityViewController.popoverPresentationController?.barButtonItem = (sender as! UIBarButtonItem)
    

    I hope this help you.

    0 讨论(0)
提交回复
热议问题