Exit application using a UIAlertController - ios

If a user opens up the application without an internet connection, a window pops up that says a connection is required, and there is an ok button. I want to the ok button to exit the application. Here is what I have:
if !isConnectedToNetwork(){
let alert = UIAlertController(title: "No Internet", message: "You need an internet connection to use this app", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
I am going to use this to exit the app:
UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)
I just don't know how to connect it to the OK button above.

Don't. Apple will reject this (if they see it).
Simply inform the user and add a 'retry' button. The retry button should obviously check the connection again.
To actually answer the question, you have currently set the handler: nil on the button action, instead you can actually set a handler and use it to call whatever logic you like.

You can handle when user press OK by the following code
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
handler: { (action:UIAlertAction!) -> Void in
//after user press ok, the following code will be execute
NSLog("User pressed OK!!")
}))

Related

iOS - how to add action in new line in UIAlertConroller swift

I need to add action in new line just like: alertActionsWithNewLines
I'm trying this code:
let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .alert)
uncheckInAlert.setTitleImage(UIImage(named: "fail_Icon"))
uncheckInAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) -> Void in
self.userUncheckIn()
}))
uncheckInAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(uncheckInAlert, animated: true, completion: nil)
But as result I have something like: alertActionsInOneLine.
If action title is large then action automatically carried over on new line. With small action title, actions is grouped in one line. Is there any a ways to do this?
There is no flag you can set in order to change how Apple lays out its alert buttons. The UIAlertController with style alert will, however, change it's button layout automatically to vertical when it cannot properly fit the entire string of the button's text in a horizontal layout. So if your button's text is long enough, it will change the layout.
Though this is a solution to your question, I don't recommend adding arbitrary spaces or unnecessarily long strings to the button's text just so Apple will layout the buttons vertically in a production app. It's misusing the standard components and could easily start breaking down once strings become localized to other languages. In order to naturally achieve the desired behavior I would suggest creating your own, custom view to display buttons vertically.
Changing these lines from
uncheckInAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
To
uncheckInAlert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .alert)
To
let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .actionSheet)
This will show cancel option grouped with other options.
Try change style form .alert to .actionSheet
Like in this code
let uncheckInAlert = UIAlertController(title: "Are you sure you want to uncheck-in?", message: "", preferredStyle: .actionSheet)

How do I create a loop checking app connectivity

I'm trying to create a loop on my app to check Connectivity. So, when the app loads for the first time, the user must be connected to the internet, otherwise he cannot proceed on using the app.
However, I'm trying to create a function that while the user has no internet, the UIAlerView persists on the screen until find an internet connection, then it should be dismissed and another method be lunched. What would be the best way for me to do that?
let alert = UIAlertController(title: "Primeiro Uso", message: "Voce precisa estar conectado na internet", preferredStyle: UIAlertControllerStyle.alert)
// add the actions (buttons)
alert.addAction(UIAlertAction(title: "Estou Conectado", style: .default, handler: { action in
if Connectivity.isConnectedToInternet != true {
//How do I create a loop here until find a connection?
//I'd like to persist the UIAlerView until find a connection
}
//Once the user is connected, this code below should be
//lunched
getApiData { (cerveja) in
arrCerveja = cerveja
//Backup
do{
let data = try JSONEncoder().encode(arrCerveja)
UserDefaults.standard.set(data, forKey: "backupSaved")
//
self.tableView.reloadData()
}catch{print(error)
}
}}))
alert.addAction(UIAlertAction(title: "Cancelar", style: UIAlertActionStyle.cancel, handler: nil))
// show the alert
self.present(alert, animated: true, completion: nil)
}
you can use this answer
https://stackoverflow.com/a/3597085/2621857
in the appDelegate level of you application.
i think creating loop for checking internet, somehow will pressure the processor to memory leak so i suggest not using a kind of loop to check the internet.
i hope this will work for you .

How to check if user is signed in into iCloud before saving data

Before saving data into CloudKit, how do I check that user has an account and is signed in?
You need to use accountStatusWithCompletionHandler from the CKContainer class and check the accountStatus returned.
This is described in the CloudKit Quick Start documentation.
Here is the Swift version of the example:
CKContainer.defaultContainer().accountStatusWithCompletionHandler { accountStatus, error in
if accountStatus == .NoAccount {
let alert = UIAlertController(title: "Sign in to iCloud", message: "Sign in to your iCloud account to write records. On the Home screen, launch Settings, tap iCloud, and enter your Apple ID. Turn iCloud Drive on. If you don't have an iCloud account, tap Create a new Apple ID.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
// Code if user has account here...
}
}

How to display a dialog with (multiselect listview) before displaying "app would like to send you push notifications" dialog box

I would like to display a pop-up where I'll ask the user about their preferences related to the push notifications and for that one, I want to display a list of options to the user. User can select more than one options.
I think that I'll have to display a tableview inside the UIAlertView, but it is deprecated now. So, how can I display a pop (with some small message + multiple select list ) before the APN system permissions dialog in Swift.
Any help will be appreciated.
You can use this code:
let alert = UIAlertController(title: title, message:message, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in
acceptNotification = true //code to execute when the user taps that OK
}
alert.addAction(action)
//you can add more actions
self.presentViewController(alert, animated: true){ // this part if provided, will be invoked after the dismissed controller's viewDidDisappear: callback is invoked.
}

App Crashing after changing Photo Settings

I've got an app that needs access to the Photos on your device. I check to see the device status, and if they deny access I trigger a modal which will warn them that they did not provide the necessary access, and then gives them the option to go to their settings and correct the choice.
When this happens, my app crashes with the following error:
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
Here is my code for triggering the redirect. Any ideas what could be causing this or suggestions on how I should do this better?
let title = "You didn't allow us to view your photos!"
let message = "Without access, we cannot help you add photos from your device."
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "I know!", style: UIAlertActionStyle.Cancel, handler: nil))
alertController.addAction(UIAlertAction(title: "Settings (Required)", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) -> Void in
let settingsURL = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(settingsURL!)
}))
self.presentViewController(alertController, animated: true, completion: nil)
This code is called within a method that I call from the following spot:
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) -> Void in
if(status == .Authorized){
self.getPhonePhotos()
}else{
self.showDeniedPhotosPopup()
}
})
Thanks in advance.
UPDATE
I just realized something I didn't earlier. The code only crashes if I activate the "Photos" switch in the settings. The navigation itself doesn't cause the app to crash, its changing the photo settings configuration while the app is still running. To test my theory, I never triggered the popup, and simply went to the settings, and activated the photos switch and the app crashed. So the crash is definitely sourcing from the change in photo settings.
From the error it appears you are presenting your view controller on a thread which is not main thread leading to implementation of UI stuff (auto layout etc.) on your new view controller on a background thread.
Try encapsulating your view controller presentation code on main queue. Something like this:
weak var aBlockSelf = self
dispatch_async(dispatch_get_main_queue(), {
aBlockSelf.presentViewController(alertController, animated: true, completion: nil)
})
If it still crashes, profile your application to find out the exact culprit.

Resources