如何在ios上分享Instagram?

时间:2017-09-19 14:30:13

标签: 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)
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

你使用了错误的UTI:“com.instagram.photos”应该是"com.instagram.photo" 另外,请不要忘记将URL方案instagram添加到密钥LSApplicationQueriesSchemes的plist中。

Here您可以找到分享到Instagram的示例(方法- (void)send) 那里的主要代码: 目标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];
    }
斯威夫特:

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