2015-06-18 73 views
2

我需要爲活動類型分享不同的項目。 對於臉書:我必須分享:文字&的網址。 但郵件:我必須分享文字,圖片&的網址。如何與UIActivityViewController共享不同的活動項目?

我看過很多問題,但沒有人回答我的問題。

下面是一段代碼,我是怎麼做到的。

@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) 
} 

我想根據activityType自定義activityItemArray。 任何幫助將不勝感激。 :)通過以下方式

回答

0

的方法是使用UIActivityViewController例如:

@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) 
} 

上面的代碼同時適用於iPhone和iPad,因爲你設置的iOS 8.0中的新popoverPresentationController它適用於iPad的了。

在使用時您需要更換此行的UIBarButtonItem的情況:

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

有了這一個:

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

我希望這可以幫助您。

Source

相關問題