2016-09-22 48 views
7

我試圖在swift中共享圖像和文本。但是當我選擇通過Facebook,Messenger或WhatsApp共享時,它只會提供文本(圖像不共享)。我正在使用UIActivityViewController進行共享。如何在swift中共享圖像和文本?

這裏是我的代碼:

func displayShareSheet(latitude:NSString?, longitude:NSString?, image:UIImage?, address:NSString?) { 
    let activityViewController = UIActivityViewController(activityItems: [(latitude as NSString?)!, (longitude as NSString?)!, (image as UIImage?)!, (address as NSString?)!], applicationActivities: nil) 
    presentViewController(activityViewController, animated: true, completion: {} 
) 
} 
+0

你可以試試這個http://stackoverflow.com/questions/35931946/basic-example-for-sharing-text-or-image-with-uiactivityviewcontroller-in-swift –

+0

這將共享圖像或文本。我希望這兩件事能夠共享。 @Sanjeetverma – hussain

+0

看到我的答案在下面! –

回答

2

以下是UIActivityViewController代碼適用於我。還附加了兩種方法的屏幕截圖。

func shareImage() { 
      let img = UIImage(named: "SoSampleImage") 
      let messageStr = "Ketan SO" 
      let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [img!, messageStr], applicationActivities: nil) 
      activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo] 
      self.presentViewController(activityViewController, animated: true, completion: nil) 
     } 

截屏UIActivityViewController例如:

enter image description here

替代使用SLComposeViewController:

func share(){ 
     let img = UIImage(named: "SoSampleImage") 
     let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 
     composeSheet.setInitialText("Hello, Ketan!") 
     composeSheet.addImage(img) 
     self.presentViewController(composeSheet, animated: true, completion: nil) 
    } 

截屏SLComposeViewController例如:

enter image description here

希望它會幫助你..

做讓我知道,如果您有任何疑問。

+0

這隻適用於Facebook嗎?或一切@Ketan P – hussain

+0

@hussain你的問題是爲FB,所以我只準備FB的代碼...但我認爲它應該適用於所有支持圖像和文本共享的選項... –

+0

好的謝謝,讓我試試並讓你知道 – hussain

0

試試這個這是爲我工作!

@IBAction func btnExport(sender: AnyObject) 
{ 

    print("Export") 
    let someText:String = "Hello want to share text also" 
    let objectsToShare:UIImage = self.imgView.image! 
    let sharedObjects:[AnyObject] = [objectsToShare,someText] 
    let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil) 
    activityViewController.popoverPresentationController?.sourceView = self.view 

    activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook,UIActivityTypePostToTwitter] 

    self.presentViewController(activityViewController, animated: true, completion: nil) 
} 
+0

它不適用於facebook,whatsapp aan messenger。 如果你想發送郵件或文本這個作品。 – hussain

相關問題