I have an alert that pops up when the user runs out of time. Right now it just vibrates but I also want it to play an alarm sound. Is there a way to play whatever the user has set as the system default alarm sound is? Also, is it possible to have the alarm keep going until the user clicks the dismiss button?
Here is my code so far:
func dismissAlert(alert:UIAlertAction!)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
let outOfTimeAlert = UIAlertController(title: "Out Of Time", message: "Better luck next time", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAlert: UIAlertAction = UIAlertAction(title: "Dismiss", style: .Cancel, handler: dismissAlert)
outOfTimeAlert.addAction(cancelAlert)
presentViewController(outOfTimeAlert, animated: true, completion: nil)
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
Thanks for the help!
Related
Using this question I ask the user to decide whether they want to use the camera or choose an image from their phone:
//Show alert to select the media source type.
private func showAlert() {
let alert = UIAlertController(title: "Image Selection", message: "From where you want to pick this image?", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action: UIAlertAction) in
self.imagePicker.sourceType = .camera
}))
alert.addAction(UIAlertAction(title: "Photo Album", style: .default, handler: {(action: UIAlertAction) in
self.imagePicker.sourceType = .photoLibrary
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
self.present(alert, animated: true, completion: nil)
}
Which I envoke in viewDidLoad as such:
override func viewDidLoad() {
super.viewDidLoad()
firstTextField.delegate = self
showAlert()
present(imagePicker, animated: true, completion: nil)
imagePicker.delegate = self
firstImageView.layer.cornerRadius = 8
}
However when I test this the alert pops up and I choose the photo library, but the library does not appear. I have tried using viewDidAppear but that failed to work as well. No errors appear, it just hides the alert and shows the current view controller.
You may be imagining that code comes to a stop when an alert appears. But it doesn't! And you cannot show two presented view controllers simultaneously. But that is what you are trying to do.
You are saying showAlert(), so now your alert is up, and then you are immediately saying present(imagePicker), which you cannot do because your alert is still up.
Use the action handlers of the showAlert alert to present the image picker. That way, the alert closes and the image picker opens.
I am quite new to Swift and IOS, but I don't think that I should be stuck in the alert "popup". I can see the following but when pressing Restart, nothing happens.Alert popup
If I use a closure instead of nil to handler nothing happened.
I wrote the following in Swift5 using Xcode 10.3:
let alert = UIAlertController(title: title, message: message,
preferredStyle: .alert)
let action = UIAlertAction(title: "Restart", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
You need to dismiss the alert inside the completion handler of the ok action.
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:
I have a button in a simple view with on click function :
#IBAction func watchAlert(sender: AnyObject) {
showAlert()
for i in 1...2{
AudioServicesPlaySystemSound (1005);
sleep(2)
}
}
func showAlert(){
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: dismissAlert ))
}
func dismissAlert(alertView: UIAlertAction!)
{
println("User click ok button")
}
Even though I am trying to call showAlert() before playing the audio, I could only see the alert getting displayed only after sound gets played. I want the alert to be present first and then sound should play.
Any help is appreciated.
Take out the sleep command (you must never sleep the main thread - the Watchdog process will kill you dead, and besides, you are wrongly freezing the whole interface). Instead: show the alert, then use delayed performance (as in my delay utility here) to play a sound.
(There are other problems with your code; in particular, you will also need to find a new way to play the sound twice, since you must not use sleep; this, too, can be accomplished using delay - nested.)
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)
}