ios - 如何在 ios 中的 instagram 上分享?

标签 ios swift instagram uidocumentinteraction

我必须在 Instagram 上分享带标题的图片,但在 Instagram 上什么也没有。我使用下面的代码在 Instagram 上分享。共享代码是否有任何变化。我还检查了 Instagram 的官方页面,但没有给出代码。 https://www.instagram.com/developer/mobile-sharing/iphone-hooks/

以下代码在 ios10 之前有效,但在 ios11 中它不再有效。

文件已成功写入文档目录,但问题出在 UIDocumentInteractionController 中。它无法从文档目录中读取文件。

    //MARK:
    //MARK: share with instagram
    func shareImageToInstagram(withImagePath imgPath:String,withStrTitle strTitle:String,withView view:UIView,withSender sender:UIButton! = nil) {
        let instagramURL = URL(string: "instagram://app")
        if UIApplication.shared.canOpenURL(instagramURL!) {
            interactionController = UIDocumentInteractionController(url: URL.init(fileURLWithPath: imgPath))
            interactionController?.uti = "com.instagram.photos"
            interactionController?.annotation = NSDictionary.init(dictionaryLiteral: ("InstagramCaption",strTitle))
            interactionController?.presentOpenInMenu(from: CGRect.zero, in: view, animated: true)
            sender.isUserInteractionEnabled = true
        }
    }

    //MARK:
    //MARK: share with instagram
    func downloadUserImageFromURL(withImageUrl imgURl : URL,withView view:UIView,withstrTitle strTitle:String,withSender sender:UIButton! = nil){

        DispatchQueue.global(qos: .userInitiated).async {
            do {
                DispatchQueue.main.async {
                    SINGLETON.startLoadingActivity(view)
                }
                let data = try Data.init(contentsOf: imgURl) //make sure your image in this url does exist, otherwise unwrap in a if let check
                DispatchQueue.main.async {
                    SINGLETON.stopLoadingActivity(view)
                    //create instance of NSFileManager
                    let paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
                    //create an array and store result of our search for the documents directory in it
                    let documentsDirectory: String = paths[0] as? String ?? ""
                    //create NSString object, that holds our exact path to the documents directory
                    let fullPath: String = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("insta.igo").absoluteString
                    //add our image to the path

                    if FileManager.default.fileExists(atPath: fullPath)
                    {
                        do {
                            try FileManager.default.removeItem(at: URL.init(string: fullPath)!)
                        } catch let error as NSError {
                            sender.isUserInteractionEnabled = true
                            print(error.localizedDescription)
                        }
                    }
                    do {
                        try data.write(to: URL.init(string: fullPath)!)
                        self.shareImageToInstagram(withImagePath: fullPath, withStrTitle: strTitle, withView: view,withSender: sender)

                    } catch let error as NSError {
                        sender.isUserInteractionEnabled = true
                        print(error.localizedDescription)
                    }

                }
            }
            catch{
                DispatchQueue.main.async {
                    SINGLETON.stopLoadingActivity(view)
                }
            }
        }
    }

最佳答案

您使用了错误的 UTI:“com.instagram.photos”应该是 "com.instagram.photo" .
也不要忘记将 URL scheme instagram 添加到 plist 的 Key LSApplicationQueriesSchemes 中.

Here您可以找到分享到 Instagram 的示例(方法 - (void)send)。
那里的主要代码: objective-C :

// make a path into documents
    NSString* homePath = [self _getpathToDocuments];
    NSString* basePath = @"integration/instagram";
    NSString* tmpFileName;
    if ([self _isInstagramOnly]) {
        tmpFileName = @"jumpto.igo";
    } else {
        tmpFileName = @"jumpto.ig";
    }

    NSString* dirPath = [NSString stringWithFormat:@"%@/%@", homePath, basePath];
    NSString* docPath = [NSString stringWithFormat:@"%@/%@", dirPath, tmpFileName];

    [[NSFileManager defaultManager] removeItemAtPath:docPath error:nil];
    if ([[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil]) {
        UIImage* tmpImg = [self _imageForSharing];

        if([self _needResizeImage]){
            tmpImg = [self _resizeImage:tmpImg];
        }

        NSData* imgData = [self generateImageData:tmpImg];
        [[NSFileManager defaultManager] createFileAtPath:docPath contents:imgData attributes:nil];
        NSURL* url = [NSURL fileURLWithPath:docPath isDirectory:NO ];

        NSString *UTI  = nil;
        if ([self _isInstagramOnly]) {
            UTI = @"com.instagram.exclusivegram";
        } else {
            UTI = @"com.instagram.photo";
        }

        NSString *captionString = @"Caption message";

        UIDocumentInteractionController* dic = [UIDocumentInteractionController interactionControllerWithURL:documentFileURL];
        dic.UTI = UTI;
        dic.annotation = @{@"InstagramCaption" : captionString};
        dic.delegate = self;

        [self presentOpenInMenuFromRect:[self _getButtonRect] inView:self.view animated:YES];
    }

swift :

//  Converted with Swiftify v1.0.6491 - https://objectivec2swift.com/
// make a path into documents

var homePath: String = _getpathToDocuments()
var basePath = "integration/instagram"
var tmpFileName = ""
if _isInstagramOnly() {
    tmpFileName = "jumpto.igo"
}
else {
    tmpFileName = "jumpto.ig"
}
var dirPath = "\(homePath)/\(basePath)"
var docPath = "\(dirPath)/\(tmpFileName)"
try? FileManager.default.removeItem(atPath: docPath)
if try? FileManager.default.createDirectory(atPath: dirPath, withIntermediateDirectories: true, attributes: nil) != nil {
    var tmpImg: UIImage? = _imageForSharing()
    if _needResizeImage() {
        tmpImg = _resize(tmpImg)
    }
    var imgData = generateImageData(tmpImg)
    FileManager.default.createFile(atPath: docPath, contents: imgData, attributes: nil)
    var url = URL.fileURL(withPath: docPath, isDirectory: false)
    var UTI: String? = nil
    if _isInstagramOnly() {
        UTI = "com.instagram.exclusivegram"
    }
    else {
        UTI = "com.instagram.photo"
    }
    var captionString = "Caption message"
    var dic = UIDocumentInteractionController(url: documentFileURL)
    dic.uti = UTI
    dic.annotation = ["InstagramCaption": captionString]
    dic.delegate = self
    presentOpenInMenu(from: _getButtonRect(), in: view, animated: true)
}

关于ios - 如何在 ios 中的 instagram 上分享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46303120/

相关文章:

ios - 应用程序 您想使用您当前的位置吗!使用 ALAssetsLibrary

swift - 父类(super class)中的选项导致子类初始化程序出现问题

arrays - 无法快速将对象追加到对象数组中

ios - 子 ViewController 不调用父类中声明的委托(delegate)方法

javascript - Jquery 在服务器上显示隐藏问题

ios - 以编程方式从带有动画的 customCell 类的 tableView 中删除一行

iOS UILabel 位置在调用 setText 时重置

iphone - TableView addGesture 到 UIImageView 以了解哪个单元格被点击

javascript - 每次我通过按钮调用ajax时如何重用jsonp url中的jsonp响应变量?

instagram 长期访问 token 在 60 天后到期,启用自动更新 token