ios 如何在Instagram中分享图片?Swift

inb24sb2  于 2023-01-06  发布在  iOS
关注(0)|答案(4)|浏览(278)

很抱歉没有代码的问题。但我没有找到任何地方寻找。我想分享图片与标题在Instagram的?我该怎么做呢?
"任何帮助都是最好的"

btqmn9zl

btqmn9zl1#

如果不想使用UIDocumentInteractionController

SWIFT 5更新

import Photos
...

func postImageToInstagram(image: UIImage) {
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if error != nil {
            print(error)
        }

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

        let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)

        if let lastAsset = fetchResult.firstObject as? PHAsset {

            let url = URL(string: "instagram://library?LocalIdentifier=\(lastAsset.localIdentifier)")!

            if UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
            } else {
                let alertController = UIAlertController(title: "Error", message: "Instagram is not installed", preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                self.present(alertController, animated: true, completion: nil)
            }

        }
}
gr8qqesn

gr8qqesn2#

Swift 3.0版本:

@IBAction func shareInstagram(_ sender: Any) {

        DispatchQueue.main.async {

            //Share To Instagram:
            let instagramURL = URL(string: "instagram://app")
            if UIApplication.shared.canOpenURL(instagramURL!) {

                let imageData = UIImageJPEGRepresentation(image, 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 ")
            }
        }
    }
abithluo

abithluo3#

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”类型:数组,并添加URL方案,在本例中为“instagram”。

rxztt3cl

rxztt3cl4#

这里试试这个代码

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

相关问题