2015-06-19 41 views
0

我試圖用標準UIActivityViewController共享圖像,它的罰款,對Facebook的Twitter的保存圖像共享使用此代碼:添加Instagram的到UIActivityViewController

let firstActivityItem = "foo text" 
let secondActivityItem : UIImage = image! 

let activityViewController : UIActivityViewController = UIActivityViewController(
    activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil) 

activityViewController.excludedActivityTypes = [ 
    UIActivityTypePostToWeibo, 
    UIActivityTypePrint, 
    UIActivityTypeAssignToContact, 
    UIActivityTypeAddToReadingList, 
    UIActivityTypePostToVimeo, 
    UIActivityTypePostToTencentWeibo 
] 

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

我需要一個還有一點,Instagram的

If UIApplication.sharedApplication().canOpenURL(instagramURL!) { 
      // Success 
      var img = image! 

      var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.igo") 
      UIImageJPEGRepresentation(img, 1).writeToFile(savePath, atomically: true) 
      var imgURL = NSURL(string: NSString(format: "file://%@", savePath) as! String) 
      docController = UIDocumentInteractionController(URL: imgURL!) // 1 
      docController.UTI = "com.instagram.exclusivegram" // 2 
      docController.delegate = self 
      docController.annotation = ["InstagramCaption":"testsss"] // 3 
      docController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true) // 4 
     } else { 
      // Error 
     }  

這兩個代碼禾rk罰款單獨,我怎麼能加InstagramUIActivityViewController?它有可能嗎?

+0

你有沒有在UIActivityViewController中添加Instagram的解決方案? –

+0

@EktaPadaliya是的,檢查標記的答案 – Maysam

+0

但這就是「UIDocumentInteractionController」。我想用「UIActivityController」 –

回答

2

我認爲將其他社交分享添加到您爲Instagram編寫的代碼會更容易。 「.igo」擴展名僅適用於Instagram,所以其他應用不支持。只要改變從「.igo」這個擴展「.ig」和其他應用程序會讀取它:

var savePath: String = NSHomeDirectory().stringByAppendingPathComponent("Documents/Test.ig") 

但Instagram的也有一個專屬UTI避免其他應用程序出現在同一文檔交互視圖。所以,你還需要將它從「exclusivegram」到「照片」改變:

docController.UTI = "com.instagram.photo" 

我有一個類似的功能的應用程序,這是我的原代碼:

@IBAction func shareOnIntagram(sender: UIButton) { 

     let finalImage: UIImage = UIImage.imageWithView(photoView) 
     let instagramURL = NSURL(string: "instagram://app") 
     if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) { 

      let imageData = UIImageJPEGRepresentation(finalImage, 1) 
      let captionString = "caption" 
      let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig") 
      if imageData?.writeToFile(writePath, atomically: true) == false { 

       return 

      } else { 

       let fileURL = NSURL(fileURLWithPath: writePath) 
       self.documentController = UIDocumentInteractionController(URL: fileURL) 
       self.documentController.delegate = self 
       self.documentController.UTI = "com.instagram.photo" 
       self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption") 
       self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true) 
      } 

     } else { 
      print(" Instagram is not installed ") 
     } 
    } 

爲了使這個代碼工作時,不要忘記在UIViewController類中添加UIDocumentInteractionControllerDelegate

+1

只是把文件://在writePath之前..它爲我工作後把writePath =「file:/ /「+ writePath –

0

看來這是不可能的,因爲.igo擴展是Instagram所需要的。

相關問題