When I try to share stuff on Facebook from my app, the "Post" button is not working. The cancel button on the left side is working, but the post button on the right is not.
This is my code:
#IBAction func saveToFB(sender: AnyObject) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Share on Facebook")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
So I tried this out...
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
let fbcomposer = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
fbcomposer.setInitalText(text)
fbcomposer.addImage(UIImage(named: myimg.png))
fbcomposer.addURL(NSURL(string: iosLink))
presentViewController(fbcomposer, animated: true, completion: {})
}
The problem is in your code you are only setting the InitialText. There is some issue with fb that does not allow third party apps to set text when posting. In my case, it was adding the image and the link as well. But the text was not added.
Try adding links or images and see if the problem still persists.
Hope this helps. :)
Related
Dears i got an error "Value of type 'GameScene' has no member 'present' "
I want to add a label to share the best-score in facebook the code below works fine in normal ViewController
for touch in touches {
let touchLocation = touch.location(in: self)
if atPoint(touchLocation).name == "shareButtoon" {
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
let facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText(bestScore.text)
self.present(facebookSheet, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to your Facebook account", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
To be able to present such alert you might use the rootViewController, doing so:
UIApplication.shared.keyWindow?.rootViewController?.present(facebookSheet, animated: true, completion: nil)
I created a button in the app to share a status on Facebook with SLComposeViewController. How do I check whether the user really do click to share the status?
My code as below:
#IBAction func fbBtn(_ sender: Any) {
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
self.present(fbShare, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
problem solved TQ
fbShare.completionHandler = { (result:SLComposeViewControllerResult) -> Void in
switch result {
case SLComposeViewControllerResult.cancelled:
print("Cancelled")
break
case SLComposeViewControllerResult.done:
print("Done")
break
}
}
I'm trying to import social framework for twitter and call the view when the social button is pressed. This is my code and I'm getting "Value of type 'GameOverScene' has no member 'present'" where I'm trying to present the view.
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) {
let tweetController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetController?.setInitialText("I Scored \(scoreNumber) on Gone Dots! You can try by downloading Gone Dots from the app store for free")
self.present(tweetController, animated: true, completion: true)
} else {
let alert = UIAlertController(title: "Account", message: "Plese log into Twitter", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
alert.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.default, handler: {
(UIAlertAction) in
let settingsURL = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsURL{
UIApplication.shared.openURL(url as URL)
}
}))
self.present(alert, animated: true, completion: true)
}
The present method is a method of UIViewController, which is why you get the error. To present in a SKScene you can say this.
view?.window?.rootViewController?.present(alert, animated: true, completion: true)
I recommend that you google how to use UIActivityViewController which is the best way to do sharing now. I even believe to have read somewhere that those old Social APIs are deprecated.
UIActivityViewController is the way to go as its lets you share to loads of services, including 3rd party ones and you only need to use 1 API. It also means you only need 1 share button in your app instead of multiple ones.
Hope this helps.
I have a 'share on facebook' button in my app. But it doesn't work like i want it to.
i tried different codes but none of them worked on my phone.
The window pops up and i can share something if i want. But the initialText doesn't show on my iphone, while it does show it on the simulator?
Only on the simulator i get an alert that facebook doesn't work ofcourse.
Anyone know's how to fix this. Or is it even possible?
This is the code i used:
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
let facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("\(antwoordTxt.text!)")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
Apparently you can't make autoText's to Facebook anymore.. So there's my answer... :p
I'm having a homeView which is the initial view to appear when app launches. In its viewWillAppear() method I check if the user is logged in. If not, he's redirected to login page and if he's not yet registered he'll be directed to sign-up page. I get an error when user clicks the sign-up button. The signUPView appears but it gives a warning that Attempt to present loginViewController on signupViewController whose view is not in the window hierarchy. I have show detail segues between sign-up and login views.
I'm performing the segue after uialert is presented.
I got solutions to put the code in viewDidAppear or ViewWillAppear method but I'm presenting the viewControllers upon button press. So I can't use these methods.
I have searched for a lot of solutions but unable to get the right one.
Please help!
Thank you.
segue triggering code from sign-up :
dispatch_async(dispatch_get_main_queue()) {
if self.registerSuccess == true {
let alert = UIAlertController(title: "Registered!", message: "You have registered successfully!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
self.nameTextField.text = ""
self.emailTextField.text = ""
self.passwordTextField.text = ""
self.performSegueWithIdentifier("loginSegue", sender: self)
}))
self.presentViewController(alert, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Registration Failed!", message: "Please try again", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
}
screenshot of my storyboard