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?
Related
I have an app with a large number of ViewControllers. There is also a collection of functions that return UIAlertControllers informing the user regarding events related to his account.
Here is an example of one such funtion:
func signInSuccessAlert() -> UIAlertController {
//signInSuccessAlert
let signInSuccessAlert = UIAlertController(title: "Success", message: "You have been successfully signed in!", preferredStyle: .alert)
signInSuccessAlert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
return signInSuccessAlert
}
My goal is to then be able to display these UIAlertControllers in one line. Like so:
present(signInSuccessAlert(), animated: true, completion: nil)
What is the best way to make these functions available only to the ViewControllers that need them? As opposed to declaring them globally.
You could create an extension of UIViewController and declare all those functions there.
Something like this:
extension UIViewController
{
func signInSuccessAlert() -> UIAlertController {
//signInSuccessAlert
let signInSuccessAlert = UIAlertController(title: "Success", message: "You have been successfully signed in!", preferredStyle: .alert)
signInSuccessAlert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
return signInSuccessAlert
}
}
This way all your viewcontrollers will have access to these functions. If you want want to give access only to some viewcontrollers you will have to use protocols like AgRizzo suggested in the comment.
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
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.
I have just gone through adding all the required Facebook frameworks, adjusting my info.plist, and adding methods in my app delegate to allow users to sign in using Facebook. I have even tested and verified this new data on my parse database. Yay.
Now, I'm trying to add a button that shares info to the user's newsfeed if they logged onto my app with Facebook. Here's what I have:
#IBAction func tellFriends(sender: AnyObject) {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) { // USER LOGGED IN USING FACEBOOK
let facebookSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Share on Facebook")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else { // USER DID NOT USE FB TO LOG IN
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)
}
}
The only result I get is the else and my alert controller pops up. I verified I am logged into the app with Facebook. What am I missing? Thanks in advance.
Just kidding, this code works, just not in the simulator. Once I plugged in my phone and tried it on there it worked fine. Much like with location services, notifications and message UIs; they work on an actual iPhone, and not in the simulator.