App crashing when UIAlertController is displayed - uialertview

I have just changed my deployment target from iOS 8 to 7 so I am testing my app on iOS 7 for the first time.
I am having an issue with the UIAlertController crashing my app when it is called. It works absolutely fine on iOS 8 however doesn't work at all on iOS 7.
Here is the code I am using:
#IBAction func resetAllButton(sender : AnyObject) {
var alert = UIAlertController(title: "Start Over", message: "Are you sure you want to start over? This will erase your budget and all transactions.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "I'm sure!", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
self.resetView()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}

UIAlertController is only available on iOS 8 or higher. You need to use a UIAlertView to present the alert on iOS 7.

Related

Using alertview in ios 9 with swift

When I user alertview in ios 9 with swift, compiler shows this warning message:
UIAlertView' was deprecated in iOS 9.0: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
try using this code
let alert = UIAlertController(title: "TITLE", message: "MESSAGE", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title:"OK", style: UIAlertActionStyle.Cancel, handler: { (action) -> Void in
})
alert.addAction(okAction)
dispatch_async(dispatch_get_main_queue(),{
self.presentViewController(alert, animated: true, completion: nil)
})

UIAlertController Showing With Delay

I am having a lot of delay with my UIAlert, whenever it loads I have to wait before I can click and then again for it to fully disappear. I have looked at other answers that recommended using dispatch_async(dispatch_get_main_queue(), {}). I have tried using this but to no avail. The alert pops up during the render loop for a scene kit game I am making (the scene is just a cube and when I pause the scene it is still delayed). Shouldn't rendering be done in the render thread anyway. I looked the through the time profile log to see if something was blocking the Main Thread but I did not see anything that caught my attention (I fairly new to the instruments).
Here is my code where I am creating the alert:
func share(){
print("share funciton")
let alert = UIAlertController(title: "Share", message: "Where do you want to share?", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Twitter", style: .Default, handler: {(alert: UIAlertAction!) in
print("twitter")
self.showTwitter()
}))
self.presentViewController(alert, animated: false, completion: nil)
}
private func showTwitter() {
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
let tweetShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
self.presentViewController(tweetShare, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Accounts", message: "Please login to a Twitter account to tweet.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
Then I call share() when in the render loop with a dispatch_asych. I've been working at this bug for three days but have no idea what's causing the delay.
Try turning slow animations off. Open simulator -> debug -> slow animations

Alert makes app crash on phone but not in simulator

I am using the following function to display an alert with an "ok" and a "cancel" Button:
func displayModalDialog(#title: String, message: String, yesHandler: ((UIAlertAction!) -> Void)?, noHandler: ((UIAlertAction!) -> Void)?) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: yesHandler))
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: noHandler))
self.presentViewController(alert, animated: true, completion: nil)
}
This function is part of view controller which is the parent class of most view controllers in my app. When I start my app in the Simulator everything works fine and the alert is displayed with the specified parameters. However when I try to run it on a phone (4s with iOS7 and 5 with iOS8 tested) it crashes on the line:
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
giving me a EXC_BAD_ACCESS error. Using exceptionBreakpoints did not reveal any additional information.
Hope anyone has help - thanks!

UIAlertView is crashing app on iOS 7

I have just released my app on the App Store however I have just been emailed and told that there is issues with crashing.
The issue is with Alert Views which crash my app when they are supposed to appear (only in iOS 7). I had some code in place that was supposed to fix this issue, however it doesn't seem to work in certain versions of iOS 7.
Here is my current code:
#IBAction func resetAllButton(sender : AnyObject) {
//If statement to check whether the modern style alert is available. Prior to iOS 8 it is not.
if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {
println("UIAlertController can be instantiated")
var alert = UIAlertController(title: "Start Over", message: "Are you sure you want to start over? This will erase your budget and all transactions.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "I'm sure!", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
self.resetView()
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
else {
println("UIAlertController can NOT be instantiated")
var alertView = UIAlertView()
alertView.delegate = self
alertView.title = "Start Over"
alertView.message = "Are you sure you want to start over? This will erase your budget and all transactions."
alertView.addButtonWithTitle("I'm sure!")
alertView.addButtonWithTitle("Cancel")
alertView.show()
}
}
What can I do to ensure that my app doesn't crash on any version of iOS 7 or 8?
I had the same problem in the relese build.
It seems an internal bug of the swift compiler (using Xcode 6.0.1 (6A317) )
I solved actually with a objC helper class with this method:
+ (BOOL) checkIfClassExists:(NSString *)className {
id c = objc_getClass([className cStringUsingEncoding:NSASCIIStringEncoding]);
if (c != nil) {
return YES;
} else {
return NO;
}
}
called in my swift code with a bridge header
if ObjCFix.checkIfClassExists("UIAlertController") {
//iOS 8 code here
} else {
//iOS 7 code here
}
Here is my drag and drop swift solution:
//Alerts change in iOS8, this method is to cover iOS7 devices
func CozAlert(title: String, message: String, action: String, sender: UIViewController){
if respondsToSelector("UIAlertController"){
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: action, style: UIAlertActionStyle.Default, handler:nil))
sender.presentViewController(alert, animated: true, completion: nil)
}
else {
var alert = UIAlertView(title: title, message: message, delegate: sender, cancelButtonTitle:action)
alert.show()
}
}
Call like this:
CozAlert("reportTitle", message: "reportText", action: "reportButton", sender: self)
Beware this is only for the most basic alerts, you might need additional code for advanced stuff.

Xcode 6 or iOS 8 Bug: UIAlertController Not Showing Message?

In my view controller I have the following code to create a UIAlertController:
var avc = UIAlertController(title: "Location", message: "Please Enter A Location", preferredStyle: UIAlertControllerStyle.Alert)
self.navigationController!.presentViewController(avc, animated: true, completion: nil)
Running it in the simulator, I see
Pretty sure this is a bug in Xcode 6 beta 7 or swift? Ideas?
Found the answer, I'm supposed to add an action to the UIAlertController for the message to be displayed:
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))