Present View: Swift 3 - ios

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.

Related

Share on facebook, text doesn't come up on phone. (Swift)

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

Sharing on FB not working

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. :)

App store cancel button not working

So I have a button that opens the app store page for the app via "UIApllication.sharedApplication().openURL"
What is strange is that instead of popping up with a regular Web browser it opens to the app store page.
The problem is that there is a cancel button on the page it opens. And that button does not seem to work. Any ideas how I might implement that button?
Here is the code: (vc() is just a function that returns the view controller)
static func rate()
{
crashLog("Opening up itunes page")
//https://itunes.apple.com/us/app/toothache-be-the-monster/id1033405285?mt=8
if let v = vc()
{
let url = NSURL(string: "itms://itunes.apple.com/de/app/toothache-be-the-monster/id1033405285?mt=8&uo=4")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
}
neverRateAgain()
}
I testet your code.
The problem is that you are using itms which will open the iTunes Store.
Simpy use itms-apps instead of itms so it will be opened in the App Store rather then the iTunes Store. (Don't forget to change it in you .plist too)
let url = NSURL(string: "itms-apps://itunes.apple.com/de/app/toothache-be-the-monster/id1033405285?mt=8&uo=4")
EDIT: Now that you added more code, let me show you how I achieved what you wanted to achieve.
CODE:
func showRateMe() {
let alert = UIAlertController(title: "Would you like to rate the App?", message: "We hope you enjoy the App !", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Rate App now !", style: UIAlertActionStyle.Default, handler: { alertAction in
UIApplication.sharedApplication().openURL(NSURL(string : "https://itunes.apple.com/us/app/name/id")!)
alert.dismissViewControllerAnimated(true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Later!", style: UIAlertActionStyle.Default, handler: { alertAction in
alert.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "NO !", style: UIAlertActionStyle.Default, handler: { alertAction in
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "neverRate")
alert.dismissViewControllerAnimated(true, completion: nil)
}))
}
SOLUTION (I think):
I use in my code: "https://itunes.apple.com/us/app/name/id"
You use in yours: "itms://itunes.apple.com/de/app/toothache-be-the-monster/id1033405285?mt=8&uo=4"
Yours should be: "https://itunes.apple.com/de/app/toothache-be-the-monster/id1033405285?mt=8&uo=4"
OLD ANSWER THAT APPLIED TO A PREVIOUS VERSION OF THE QUESTION:
If I am not mistaken, this is what you need to do:
1) Create a cancel button with an IBAction.
2) IBAction does this:
self.dismissViewController()
3) Done.
This is how I would "implement the Cancel button" as you wrote in your question.

Calling .presenttoViewController in SKScene in Swift

I'm making a game with Sprite Kit in Swift. When you die in the game it presents the option to share your score on Twitter, Facebook, etc. But since I'm in a SKScene, it doesn't have the member .presentViewController . I need to present the Twitter compose field, is there any way to do this?
Here's the code:
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
let tweetviewcontroller = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetviewcontroller.setInitialText("I Scored \(score) on Cube Falling")
self.presentViewController(tweetviewcontroller, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to your Twitter account", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Okay", 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.sharedApplication().openURL(url)
}
}))
self.presentViewController(alert, animated: true, completion: nil)
}
Here's the error: image of code
Thanks

Twitter sharing, SLComposeViewController

Having set up a Twitter sharing function in an iOS app, I have a question.
Here is the code, it is based on what I have found, browsing the net:
func shareImageOnTwitter() {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
let twit = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
// Do all the necessary local cooking for whatever we want to share ………….
} else {
// In which case are we suppose to be here ?????
var alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
Let me say that it is basically working. But the question is when do we reach the second branch of the if.
That is where my comment (// In which case are we suppose to be here ?????) is.
I have made a few experiments to prevent Twitter from working, I then get other messages, but never the ones in the code above.
So when is exactly this code supposed to execute? In which exact conditions?

Resources