Why does Facebook share dialog not show up? - ios

I am currently trying to enable the functionality to share a video through Facebook, however the share dialog does not show up.
Here's the code:
let video = ShareVideo(videoURL: Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!)
let content = ShareVideoContent()
content.video = video
content.contentURL = Bundle.main.url(forResource: "Rap God", withExtension: "mp4")!
let dialog = ShareDialog(fromViewController: self, content: content, delegate: self)
dialog.shareContent = content
dialog.shouldFailOnDataError = true
dialog.mode = .shareSheet
dialog.fromViewController = self
dialog.show()
print(dialog.canShow)
print(dialog.canShow) is here equal to "false". Could please tell me how I could fix this?

Related

Facebook app invitation iOS SDK can't display the Invitation VC

So this's my code for app invite :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}
This code works fine but if I try to add the promotional code like this :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
content.promotionCode = "preview"
content.promotionText = "Use the *preview* code to unlock the app"
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}
The the invite VC is not shown any more (the function is called but nothing is showing). What did I missed here ?
The issue was that I've used special character like * so removing it make the app works fine my final code is like this :
private func inviteFriends() {
let content = FBSDKAppInviteContent()
content.appLinkURL = URL(string: "...")
content.appInvitePreviewImageURL = URL(string: "...")
content.promotionCode = "preview"
content.promotionText = "Use the preview code to unlock the app"
FBSDKAppInviteDialog.show(from: self, with: content, delegate: nil)
}

Deep linking for Facebook shareLinkContent

I'm working with deep link in iOS. I am going to share a link in Facebook using `FBSDKShareLinkContent. I have created deep linking URL in Facebook like https://fb.me/****************.
I have already done AppInviteContent and it works good like this:
let content : FBSDKAppInviteContent = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "https://fb.me/****************")!
content.appInvitePreviewImageURL = NSURL(string: "http://***.***.***.***/shareImage.png" as String)!
FBSDKAppInviteDialog.showWithContent(content, delegate: self)
Now, I am sharing link in Facebook like this:
let shareLinkContent : FBSDKShareLinkContent = FBSDKShareLinkContent()
shareLinkContent.contentURL = NSURL(string: "https://example.com/a2d69835ae")!
shareLinkContent.contentTitle = "App_Name"
shareLinkContent.contentDescription = "Description"
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = shareLinkContent
dialog.mode = FBSDKShareDialogMode.Web
dialog.show()
How to set deep link URL (e.g. https://fb.me/****************) in this shareLinkContent.
A very apt idea is to use Branch framework for the deep linking feature. You can get to know how to use this framework from here https://branch.io/
It can be used to share your app contents to any of the social networking sites. it has the feature on universal linking and deep linking.
Remove this below code, this have been modified in latest pod version.
let dialog : FBSDKShareDialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = shareLinkContent
dialog.mode = FBSDKShareDialogMode.Web
dialog.show()
Add this code will show Facebook Share dialog:
FBSDKShareDialog.show(from: self, with: shareLinkContent, delegate: self)
This will fix your FBSDK share issue.
For APP invite try some thing like this:
{
let content : FBSDKAppInviteContent = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "https://fb.me/****************")!
content.appInvitePreviewImageURL = NSURL(string: "http://***.***.***.***/shareImage.png" as String)!
FBSDKAppInviteDialog.show(from: self, with: content, delegate: self)
}

How to play instagram video using AVPlayer

I am trying to play an instagram video from shared link , I have used following code but it doesn't stream , is there any additional step required ? Any API which will get the source video URL like graph API on Facebook ?
The URL is like this https://instagram.com/p/BOzamYMA-vb/
let videoURL = NSURL(string: "https://instagram.com/p/BOzamYMA-vb/")
let player = AVPlayer(url: videoURL! as URL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
This opens player but doesn't play video
Any help is appreciated.
You are giving a web page URL to AVPlayer, this can't work, you have to use the URL for the video itself.
Using the Fuzi HTML parsing library you can get the video URL from the web page.
The trick is to find the URL in the HTML.
For instagram, we can find it with this xpath: "//meta[#property='og:video']/#content".
Example:
import Fuzi
let link = "https://instagram.com/p/BOzamYMA-vb/"
if let pageURL = URL(string: link),
let data = try? Data(contentsOf: pageURL),
let doc = try? HTMLDocument(data: data)
{
let items = doc.xpath("//meta[#property='og:video']/#content")
if let item = items.first,
let url = URL(string: item.stringValue)
{
print(url)
}
}
This gets the URL from the page, "http://scontent-cdg2-1.cdninstagram.com/t50.2886-16/15872432_382094838793088_926533688240373760_n.mp4", and you can use it to download or stream the video like you would usually do.

Why is my share dialog empty? Facebook SDK

I'm trying to post to facebook, which works fine but none of my strings from my content variables are being added to the share dialog.
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "<INSERT STRING HERE>")
content.contentTitle = "My Title Here"
content.contentDescription = "My description here!"
content.imageURL = NSURL(string: "<INSERT STRING HERE>")
let shareDialog = FBSDKShareDialog()
shareDialog.fromViewController = self
shareDialog.shareContent = content
shareDialog.delegate = self
if !shareDialog.canShow() {
print("cannot show native share dialog")
}
shareDialog.show()
Facebook's policies don't allow you to pre-populate status messages and require all content to be user generated. Here is what I have done and I managed to get the URL in the Facebook popup:
let activityItems = ["My app text", "", "http://google.com"]
let vc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
self.presentViewController(vc, animated: true, completion: nil)
If I open Twitter for example I get "My app text" and the URL, for Facebook I only get the URL.

MPMoviePlayerController doesn't display video

I saw a lot of topics there and always the same problem: it's not displaying at all.
So because I am little stubborn, I want it more
This is my code :
let moviePath = NSBundle.mainBundle().pathForResource("back", ofType:"mp4");
println(moviePath!)
let movieURL = NSURL.fileURLWithPath(moviePath!)
let theMoviePlayer = MPMoviePlayerController(contentURL: movieURL!)
self.view.addSubview(theMoviePlayer.view)
theMoviePlayer.prepareToPlay()
theMoviePlayer.shouldAutoplay = true
theMoviePlayer.fullscreen = true
theMoviePlayer.play()
I checked and I have the movie file in the app's bundle resources.
The video dimension : 640 × 360 pixels
So where it can be from?
Did I forget something important ?
Edit
This new code works but always some problems :
let url = NSBundle.mainBundle().URLForResource("back", withExtension: ".mp4")
let player = MPMoviePlayerViewController(contentURL: url)
presentMoviePlayerViewControllerAnimated(player)
player.moviePlayer.prepareToPlay()
player.moviePlayer.repeatMode = MPMovieRepeatMode.One
player.moviePlayer.shouldAutoplay = true
player.moviePlayer.play()
It show me the video in landscape view and not full screen.
This is how I did it in one of my apps. You might need to call presentMoviePlayerViewControllerAnimated() and pass in the MPMoviePlayerViewController instead of doing addSubview.
let url = NSBundle.mainBundle().URLForResource("back", withExtension: ".mp4")
let player = MPMoviePlayerViewController(contentURL: url)
presentMoviePlayerViewControllerAnimated(player)
player.prepareToPlay()
player.shouldAutoplay = true
player.fullscreen = true
player.moviePlayer.play()
** Resolved **
I resolved addend the line :
player.moviePlayer.view.transform = CGAffineTransformMakeRotation(CGFloat(M_PI / 2))
Of course, it do a rotation of the video. So If it's a simple background animated like clouds or something simple, it's ok.
if there is no issue with the content type of video, call below function. It just works. :)
private func presentVideoVC(){
var url: NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
var moviePlayerVC: MPMoviePlayerViewController! = MPMoviePlayerViewController(contentURL: url)
self.presentMoviePlayerViewControllerAnimated(moviePlayerVC)
}
my two cents:
in case of ios9 use https.. (or add exceptions... not good.)
dont call directly on viewDidload, it does not work, call in async to wait some time, allowing controller to be set:
dispatch_async(dispatch_get_main_queue()){
self.playIt()
}
func playIt() {
let urlString = "https://www.ingconti.com/softysit/Spinelli_short.mp4"
let url: NSURL = NSURL(string: urlString)!
let moviePlayerVC: MPMoviePlayerViewController! = MPMoviePlayerViewController(contentURL: url)
moviePlayerVC.moviePlayer.shouldAutoplay = true
self.presentMoviePlayerViewControllerAnimated(moviePlayerVC)
}
note that now MPMoviePlayerViewController is deprecated.

Resources