Alert makes app crash on phone but not in simulator - swift

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!

Related

Error in console using UIAlertAction Xcode 7.3 beta

I am trying to print the error to the user using alertview.
Here is my code :::
if error != nil{
let DisplayAlert = UIAlertController(title: "Error!!", message: error?.description, preferredStyle: .Alert)
DisplayAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
}
When I run the code I see the following in the console and I do not see any alert displayed to user.
"Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior"
What should i do to fix this issue.? Pls help.
Thanks !!!
Looks like you have forgot to present the alert
if error != nil{
let DisplayAlert = UIAlertController(title: "Error!!", message: error?.description, preferredStyle: .Alert)
DisplayAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(DisplayAlert, 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

Show alert view when tap on phone number in web view with Swift

I'm having a web view that displays a company's address and phone number and email. The current behavior is when the phone number is tapped it is then dialled immediately.
Is there a way for me to prompt an alert view to let user confirm the call? Instead of calling it immediately?
Here is the function I use to initialize an alert:
func displayAlert(title: String, message: String) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (ACTION) -> Void in
// self.dismissViewControllerAnimated(true, completion: nil)
alert.hidesBottomBarWhenPushed = true
}))
print("running Activityindicator code")
self.presentViewController(alert, animated: true, completion: nil)
}
and I call it like so:
self.displayAlert("Alert title", message: "alert message")
hope this helps!

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

App crashing when UIAlertController is displayed

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.