0

I want to share a video on facebook by taking it from the bundle. I use this facebook SDK for swift, but I can not share the video with ShareDialog.

This is my code:

class ViewController: UIViewController, LoginButtonDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

let bundlePath = Bundle.main.url(forResource: "fish3", withExtension: "mp4")?.path
@IBOutlet weak var loginView: UIView!
let actionCanc = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    //Login Button
    let loginButton = LoginButton(publishPermissions: [.publishActions])
    loginButton.delegate = self
    loginButton.center = loginView.center
    loginView.addSubview(loginButton)
}

//---------------------------------------
//MARK: Login via LoginButton
//---------------------------------------

func loginButtonDidCompleteLogin(_ loginButton: LoginButton, result: LoginResult) {
    print("Did complete login via LoginButton with result \(result)")
}

func loginButtonDidLogOut(_ loginButton: LoginButton) {
    print("Did logout via LoginButton")
}

//---------------------------------------
//MARK: Share Dialog
//---------------------------------------

func showShareDialog<C: ContentProtocol>(_ content: C, mode: ShareDialogMode){
    let shareDialog = ShareDialog(content: content)
    shareDialog.presentingViewController = self
    shareDialog.mode = mode
    do{
        try shareDialog.show()
    } catch(let error){
        let alertController = UIAlertController(title: "Invalid share content", message: "Failed to present share dialog with error \(error)", preferredStyle: .alert)
        alertController.addAction(actionCanc)
        present(alertController, animated: true, completion: nil)
    }
}


//---------------------------------------
//MARK: Video Content
//---------------------------------------


//Share from Bundle
@IBAction func shareVideoFromBundle() {

    let videoUrl = URL(fileURLWithPath: bundlePath!)

    let video = Video(url: videoUrl)

    print("FILE VIDEO")
    print(video)

    let content = VideoShareContent(video: video)
    showShareDialog(content, mode: .automatic)
}

}

When I try to share the video, prints the error in the catch: "Invalid share content"

can someone help me?

Amlach
  • 31
  • 10
  • could you post a fully working example? please read the site guidelines from [here](https://stackoverflow.com/help/how-to-ask) – ramrunner Jul 10 '18 at 15:46

2 Answers2

3

I found a solution from this thread

var assetPlaceholder: PHObjectPlaceholder!
PHPhotoLibrary.shared().performChanges({
    let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL as URL!)
    assetPlaceholder = assetRequest?.placeholderForCreatedAsset
}, completionHandler: { (success, error) in
    if success {
        let localID = assetPlaceholder.localIdentifier
        let assetID = localID.replacingOccurrences(of: "/.*", with: "", options: NSString.CompareOptions.regularExpression, range: nil)
        let ext = "mp4"
        let assetURLStr = "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)"

        // Do what you want with your assetURLStr
    } else {
        if let errorMessage = error?.localizedDescription {
            print(errorMessage)
        }
    }
})
Amlach
  • 31
  • 10
0

Generally its a good idea to copy an item from the bundle to the app cache directory before trying to work with it.

Objective-C

NSURL * cacheDir = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]];  

Swift

let cacheDir = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last!)

The cache directory is a good place to drop things you can discard at a later date.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • Thanks for the reply. I noticed that to share a video I need an url like 'assets-library://assets / ...', but the cache uses a path like 'file:///...' is there a way to convert the path? – Amlach Jul 11 '18 at 10:24
  • Is that what the error is telling you in your "Failed to present share dialog with error...."? message. `'assets-library://` library are for Photos assets. Implies that you need to be using the `PhotoKit` framework. You haven't shared any useful code like the **relevant parts** of your `ShareDialog` – Warren Burton Jul 11 '18 at 11:19