如何在Instagram分享图像?Swift

对不起,没有代码的问题,但我没有find任何地方寻找。 我想在Instagram分享图像与标题? 我怎样才能做到这一点?

任何帮助将是伟大的

class viewController: UIViewController, UIDocumentInteractionControllerDelegate { var yourImage: UIImage? var documentController: UIDocumentInteractionController! func shareToInstagram() { let instagramURL = NSURL(string: "instagram://app") if (UIApplication.sharedApplication().canOpenURL(instagramURL!)) { let imageData = UIImageJPEGRepresentation(yourImage!, 100) let captionString = "caption" let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo") 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.exlusivegram" self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption") self.documentController.presentOpenInMenuFromRect(self.view.frame, inView: self.view, animated: true) } } else { print(" Instagram isn't installed ") } } } } 

现在,这仍然不适用于iOS 9,所以你将不得不去你的应用程序info.plist,添加“LSApplicationQueriesSchemes”types:数组,并添加在这种情况下,“url计划”的instagram。

如果你不想使用UIDocumentInteractionController

 import Photos ... func postImageToInstagram(image: UIImage) { UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil) } func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) { if error != nil { print(error) } let fetchOptions = PHFetchOptions() fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions) if let lastAsset = fetchResult.firstObject as? PHAsset { let localIdentifier = lastAsset.localIdentifier let u = "instagram://library?LocalIdentifier=" + localIdentifier let url = NSURL(string: u)! if UIApplication.sharedApplication().canOpenURL(url) { UIApplication.sharedApplication().openURL(NSURL(string: u)!) } else { let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .Alert) alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) self.presentViewController(alertController, animated: true, completion: nil) } } } 

Swift 3.0版本:

  @IBAction func shareInstagram(_ sender: Any) { DispatchQueue.main.async { //Share To Instagrma: let instagramURL = URL(string: "instagram://app") if UIApplication.shared.canOpenURL(instagramURL) { let imageData = UIImageJPEGRepresentation(<YOURIMAGE>!, 100) let writePath = (NSTemporaryDirectory() as NSString).appendingPathComponent("instagram.igo") do { try imageData?.write(to: URL(fileURLWithPath: writePath), options: .atomic) } catch { print(error) } let fileURL = URL(fileURLWithPath: writePath) self.documentController = UIDocumentInteractionController(url: fileURL) self.documentController.delegate = self self.documentController.uti = "com.instagram.exlusivegram" if UIDevice.current.userInterfaceIdiom == .phone { self.documentController.presentOpenInMenu(from: self.view.bounds, in: self.view, animated: true) } else { self.documentController.presentOpenInMenu(from: self.IGBarButton, animated: true) } } else { print(" Instagram is not installed ") } } } 

这里试试这个代码

  @IBAction func shareContent(sender: UIButton) { let image = UIImage(named: "imageName") let objectsToShare: [AnyObject] = [ image! ] let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ] self.presentViewController(activityViewController, animated: true, completion: nil) } }