Reputation: 395
I need to share different items for activities type. For facebook:I have to share: text & url. but for Mail:I have to share text,image & url.
I have seen many questions but none answered my queries.
Below is the piece of code, How I'm doing it.
@IBAction func shareDetails(sender: AnyObject) {
var activityItemsArray:[AnyObject] = [SharingProvider(source: detail)]
if let urlString = dealer.websiteURL {
if let url = NSURL(string:urlString) {
activityItemsArray.append(url)
}
}
let shareVC = UIActivityViewController(activityItems: activityItemsArray, applicationActivities: nil)
shareVC.setValue("This is the Subject", forKey: "subject")
shareVC.completionWithItemsHandler = { (activityType:String!, completed:Bool, returnedItems:[AnyObject]!, error:NSError!) -> Void in
if !completed {
return
}
}
self.presentViewController(shareVC, animated: true, completion: nil)
}
I want to customize activityItemArray based on activityType. Any help will be appreciated. :)
Upvotes: 2
Views: 1934
Reputation: 190
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.
Upvotes: 1