Tuesday, July 30, 2024
HomeiOS Developmentswift - Sharing video to Instagram from my iOS app

swift – Sharing video to Instagram from my iOS app


I’ve see few folks already touched this topic, however I do not assume anybody recommended 100% reply that can work.

Mainly, I would like to have the ability to share video from my IOS app to Instagram WITHOUT partaking UIActivityViewController. Since modifying UI of UIActivityViewController shouldn’t be an possibility, I must implement Instagram sharing possibility from scratch, and that’s to name Instagram app from my app.

What I came upon right here on Stackoverflow, is that video must be saved to the Gallery after which it may be shared through Insta. Right here is the code I ended up utilizing:

func requestAuthorization(completion: @escaping () -> Void) {
        if PHPhotoLibrary.authorizationStatus() == .notDetermined {
            DispatchQueue.predominant.async {
                completion()
            }
        } else if PHPhotoLibrary.authorizationStatus() == .approved {
            completion()
        }
    }
    
    func saveVideoToAlbum(_ outputURL: URL, _ completion: ((String, Error?) -> Void)?) {
        
        var localIdentifier: String = ""
        requestAuthorization {
            PHPhotoLibrary.shared().performChanges({
                let request = PHAssetCreationRequest.forAsset()
                localIdentifier = request.placeholderForCreatedAsset?.localIdentifier ?? ""
                request.addResource(with: .video, fileURL: outputURL, choices: nil)
            }) { (end result, error) in
                DispatchQueue.predominant.async { [weak self] in
                    
                    completion?(localIdentifier, error)
                }
            }
        }
    }
    
    func testInsta(_ videoUrl: URL) {
        
        saveVideoToAlbum(videoUrl) { localIdentifier, err in
            
            let u = "instagram://library?LocalIdentifier=" + localIdentifier
            let url = NSURL(string: u)!
            if UIApplication.shared.canOpenURL(url as URL) {
                UIApplication.shared.open(URL(string: u)!, choices: [:], completionHandler: nil)
            } else {
                
                let urlStr = "https://itunes.apple.com/in/app/instagram/id389801252?mt=8"
                if #obtainable(iOS 10.0, *) {
                    UIApplication.shared.open(URL(string: urlStr)!, choices: [:], completionHandler: nil)
                    
                } else {
                    UIApplication.shared.openURL(URL(string: urlStr)!)
                }
            }
        }
    }

Though this code would not open this gorgeous UI within the scope of my app (like UIActivityViewController does), it nonetheless does the job. It opens the instagram with my video in it prepared for enhancing. BUT, it opens my video solely in case if Insta has a full entry to my Gallery. In any other case, it simply exhibits me the final asset from my Gallery it has entry to.

I’m 100% it’s doable to do what I need to do, however I’m struggling to discover a correct documentation that can clarify obtain what I would like. If somebody already solved this problem, please kindly share your findings. Thanks upfront!



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments