App store cancel button not working - ios

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.

Related

Present View: Swift 3

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.

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

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?

Possible to create UIAlertController using Apple's recommended UX guidelines?

Apple's iOS UX guidelines show the following example:
...where:
When the most likely button performs a nondestructive action, it
should be on the right in a two-button alert. The button that cancels
this action should be on the left.
When the most likely button performs a destructive action, it should
be on the left in a two-button alert. The button that cancels this
action should be on the right.
Source: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/Alerts.html
However, it does not seem possible to implement this guidance with the current iOS APIs (testing on iOS 9 with Swift). Here's one attempt. But I've tried varying the order of adding the alert actions, and changing which action is .Default vs. .Cancel style. No combination appears to work.
#IBAction func showAlertAction(sender: AnyObject)
{
let title = "An Alert that Offers Two Alternatives Is Easy for People to Use"
let alertViewController = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (alert :UIAlertAction!) -> Void in
print("'Cancel' tapped")
})
alertViewController.addAction(cancelAction)
let safeChoiceAction = UIAlertAction(title: "Safe Choice", style: UIAlertActionStyle.Default, handler: { (alert :UIAlertAction!) -> Void in
print("'Safe Choice' tapped")
})
alertViewController.addAction(safeChoiceAction)
self.presentViewController(alertViewController, animated: true, completion: nil)
}
Result:

Issues with displaying Alerts in Swift (After clicking "Ok", it unwinds segue

I wrote the following function below to display alerts through my project. Every time i check for an error in a form i display the alert when the user clicks submit. I want to simply show the alert, but not unwind the pervious segue. I want to stay at the current screen and give the user the opportunity to complete the form. Right now when the user clicks submit, it displays the alert ..but when i click the ok button to dismiss the alert it immediately unwinds the segue to the previous screen.
I have included the UIAlertViewDelegate in the class.... any ideas why this might be happening?
func displayAlert(title:String, error:String) {
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
Never mind.
func displayAlert(title:String, error:String) {
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
//comment out this line and it will still work. It will not segue back to the previous screen.
//self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}

Resources