2016-04-04 3 views
2

私が取り組んでいるiOS Xcode 7 Swift 2プロジェクトがあります。使用FacebookTwitterへのアプリの記事の写真:SLComposeViewControllerに似たSwiftを使ってInstagramにUIImageを投稿してください

var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 

var shareToTwitter: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter) 

愛どのようにちょうどこれら二つのソーシャルメディアに写真を投稿するための簡単でシンプルな。私はそれらのAPIを必要とせず、必要としませんでした。

Instagramと同様の簡単な操作をしたいと思います。 Instagram APIに対処することなくこれを行う最善の方法は何ですか?それとも私には選択肢がないのですか?

私の写真dataNSCodingで保存され、DocumentDirectoryでは保存されません。私は統合のためにhereを見たが、これは写真に保存されているdirectoryです。

FacebookやTwitter用に既に持っているもののような単純なもの。

+0

UIDocumentInteractionControllerを使用して、選択した写真とキャプションでInstagramアプリを起動します。私は、このようにすることで LSApplicationQueriesSchemes Instagramの

答えて

5

私はhereを見て、解決策を見つけた:

私はNSObjectを作成しました:

import UIKit 
import Foundation 

class InstagramManager: NSObject, UIDocumentInteractionControllerDelegate { 

    private let kInstagramURL = "instagram://app" 
    private let kUTI = "com.instagram.exclusivegram" 
    private let kfileNameExtension = "instagram.igo" 
    private let kAlertViewTitle = "Error" 
    private let kAlertViewMessage = "Please install the Instagram application" 

    var documentInteractionController = UIDocumentInteractionController() 

    // singleton manager 
    class var sharedManager: InstagramManager { 
     struct Singleton { 
      static let instance = InstagramManager() 
     } 
     return Singleton.instance 
    } 

    func postImageToInstagramWithCaption(imageInstagram: UIImage, instagramCaption: String, view: UIView) { 
     // called to post image with caption to the instagram application 

     let instagramURL = NSURL(string: kInstagramURL) 
     if UIApplication.sharedApplication().canOpenURL(instagramURL!) { 
      let jpgPath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(kfileNameExtension) 
      UIImageJPEGRepresentation(imageInstagram, 1.0)!.writeToFile(jpgPath, atomically: true) 
      let rect = CGRectMake(0,0,612,612) 
      let fileURL = NSURL.fileURLWithPath(jpgPath) 
      documentInteractionController.URL = fileURL 
      documentInteractionController.delegate = self 
      documentInteractionController.UTI = kUTI 

      // adding caption for the image 
      documentInteractionController.annotation = ["InstagramCaption": instagramCaption] 
      documentInteractionController.presentOpenInMenuFromRect(rect, inView: view, animated: true) 
     } else { 

      // alert displayed when the instagram application is not available in the device 
      UIAlertView(title: kAlertViewTitle, message: kAlertViewMessage, delegate:nil, cancelButtonTitle:"Ok").show() 
     } 
    } 

} 

その後、私の@IBActionに私が使用:

let image = self.photoImageView.image 
InstagramManager.sharedManager.postImageToInstagramWithCaption(image!, instagramCaption: "\(self.description)", view: self.view) 

を。これは、とメニューを上に持って開きます'open-in'スタイルで、ユーザーはInstagram(インストールされている場合)でアプリを開くことができます。

+2

を(ユーザーは、しかし、Instagramのアプリ内で一度キャプションを編集することができます)、しかしios9のためには、次のplistファイルに追加する必要がありますアラートシートのラベルが表示されているだけです: "コピーする図表"私はそれを共有するためにタップする必要があります。アプリ内で写真を共有する直接の方法はありますか? –

+4

:作品 – TomSawyer

5
Instagramのが

インストールされている場合、これは「オープンイン」機能をオープンに...

InstagramManager.sharedManager.postImageToInstagramWithCaption(imageInstagram: <YOUR IMAGE>, instagramCaption: shareText, view: self.view) 

はスウィフト3用

import UIKit 
import Foundation 

class InstagramManager: NSObject, UIDocumentInteractionControllerDelegate { 

    private let kInstagramURL = "instagram://app" 
    private let kUTI = "com.instagram.exclusivegram" 
    private let kfileNameExtension = "instagram.igo" 
    private let kAlertViewTitle = "Error" 
    private let kAlertViewMessage = "Please install the Instagram application" 

    var documentInteractionController = UIDocumentInteractionController() 

    // singleton manager 
    class var sharedManager: InstagramManager { 
     struct Singleton { 
      static let instance = InstagramManager() 
     } 
     return Singleton.instance 
    } 

    func postImageToInstagramWithCaption(imageInstagram: UIImage, instagramCaption: String, view: UIView) { 
     // called to post image with caption to the instagram application 

     let instagramURL = NSURL(string: kInstagramURL) 

     if UIApplication.shared.canOpenURL(instagramURL! as URL) { 

      let jpgPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(kfileNameExtension) 

      do { 
       try UIImageJPEGRepresentation(imageInstagram, 1.0)?.write(to: URL(fileURLWithPath: jpgPath), options: .atomic) 

      } catch { 

       print(error) 
      } 

      let rect = CGRect(x: 0, y: 0, width: 612, height: 612) 
      let fileURL = NSURL.fileURL(withPath: jpgPath) 
      documentInteractionController.url = fileURL 
      documentInteractionController.delegate = self 
      documentInteractionController.uti = kUTI 

      // adding caption for the image 
      documentInteractionController.annotation = ["InstagramCaption": instagramCaption] 
      documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true) 
     } else { 

      /// display some message about how it didn't work 
      /// Like: UIAlertController(title: kAlertViewTitle, message: kAlertViewMessage, preferredStyle: .alert) 
     } 
    } 
} 

を更新し、その後のViewControllerにあなたがそれを呼びたいです

関連する問題