Link OK button to settings panel to add an account - ios

I've got a picture that can be shared through Facebook and Twitter.
If Facebook or Twitter isn't connected with your phone, a UIAlertAction pops up saying you have to connect.
When I click the OK button, I would want to navigate to the settings panel from the iPhone itself and once done it should go back to the app.
How can I do this?
This is the code I have once there has been clicked on the Twitter button to share the picture.
func twitterClicked( sender:UIBarButtonItem ){
if ( SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)) {
let twitterVC = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
presentViewController(twitterVC, animated: true, completion: {() -> Void in})
twitterVC.setInitialText("Picture shared through code made in Native Development")
twitterVC.addImage(theView.imageView.image)
twitterVC.addURL(NSURL(string: "http://www.devine.be"))
} else {
println("connecteer je twitter")
let alert = UIAlertController(title: "Connect", message: "Connect your twitter", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
println("Ok geklikt")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in
println("Cancel geklikt")
}
alert.addAction(okAction)
alert.addAction(cancelAction)
}

Here is how you can invoke opening Settings:
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (action) -> Void in
let url = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(url!)
}
After that the user will have to manually return to the application.

Related

Change UIImage Picker Button Label Text

The default text on the button for a user to confirm a picture when using the .Camera option for the UIImagePicker is "Use Picture".
Is there any way to change this to some other string?
func chooseAlert(){
let alert = UIAlertController(title: "Upload Image", message: "Choose Source For Image.", preferredStyle: .ActionSheet)
let cameraAction = UIAlertAction(title: "Take Photo", style: .Default, handler: { (action:UIAlertAction) -> Void in
dispatch_async(dispatch_get_main_queue())
{
// self.performSelector(#selector(self.useCamera), withObject: nil, afterDelay: 1.0)
self.useCamera()
}
})
let cameraRollAction = UIAlertAction(title: "Use Photo", style: .Default, handler: { (action:UIAlertAction) -> Void in
dispatch_async(dispatch_get_main_queue())
{
self.useCameraRoll()
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel){
(action) in
print(action)
}
alert.addAction(cameraAction)
alert.addAction(cameraRollAction)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)
}
In the IBAction of the Button call this function which will bring an alert on the screen. In the alert actions, change the title as per your wish. useCamera() and useCameraRoll() are user defined functions.

How to make a UIAlert button press advance to next Storyboard?

I'm quite new to Swift (and coding in general) and to this website.
I'm having a little bit of an issue now. In my app, I have an alert when a timer reaches 0. In that alert, there are 2 buttons. One says "Share" and the other says "Continue". I want to make it such that when a user taps "Continue", the next Storyboard will be shown to them. As of now, whatever button I press will dismiss the alert, but stay on the same Storyboard. (It also prints to the console which button I pressed, but of course that's just for me to see).
How do I go about doing this? Here is my code, in case anyone wants to know.
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button one")
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("You pressed button two")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
presentViewController(alert, animated: true, completion:nil)
Try with this:
// Create the alert controller
var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
// Create the actions
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
//do whatever you want here
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
If you haven't already set your ViewControllerIdentifier:
You need to set the Identifier value that you can find on the right column
Here is the code that might help you. When you create a AlertController with actions, you can provide the actions and style of the button when you define them. In code below action is in the closure (Block) and style is defined as .Default or .Cancel
let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
// Action when you press button goes here
print("Here you show next storyboard item.")
// Code to push new ViewController. For example :
self.navigationController?.pushViewController(newVCInstance, animated: true)
}
let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
print("Here you share things.")
// Code to share things.
}
let thirdAction = UIAlertAction(title: "Cancel", style: . Cancel) { (alert: UIAlertAction!) -> Void in
print("Just dismissing the Alert Controller.")
}
alert.addAction(firstAction)
alert.addAction(secondAction)
alert.addAction(thirdAction)
presentViewController(alert, animated: true, completion:nil)

Is it possible to have an AlertViewController be presented after another?

I want to have a UIAlertViewController that presents some text, and when you click "ok" it causes another UIAlertController to pop up and it will present the user with two options to choose from (go to main, restart game). this is the code I wrote and it brings up this error:
"2015-11-16 22:29:21.438 MemoryCardGameTest_01[1917:46708] Warning:
Attempt to present on
which is already
presenting (null)"
func showWinMessage() {
let userMessage = UIAlertController(title: "Victory! you win with... clicks \(ref2.clickCounter) and \(ref2.timeCounter) seconds", message: "", preferredStyle: UIAlertControllerStyle.Alert);
let action = UIAlertAction(title: "ok", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
}
userMessage.addAction(action);
self.presentViewController(userMessage, animated: true, completion: nil);
println("victory achieved");
}
func showUserMessage(){
let newMessage = UIAlertController(title: "whatwouldyouliketodonext?", message: "", preferredStyle: UIAlertControllerStyle.Alert);
let goToUI = UIAlertAction(title: "goToUI", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
println("gotoUI was clicked");
}
let playAgain = UIAlertAction(title: "PlayAgain", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in
println("playAgain was clicked");
}
newMessage.addAction(goToUI);
newMessage.addAction(playAgain);
self.presentViewController(newMessage, animated: true, completion: nil);
}
This roughly does what you want. Just make sure you don't call it in viewDidLoad as you can get an error like you mentioned. Try viewDidAppear or something.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let alertController = UIAlertController(title: "First message", message: "This is the first message", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
let alertController = UIAlertController(title: "Second message", message: "This is the second message", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
}
Essentially I just set the second view to appear in OKAction of the first alertController.

Adding a action to a UIAlert that takes the user to settings

I have a UIAlert that notifies the user that they do not have an internet connection and that they need one in order to use the app. As well as letting them dismiss the alert by tapping the ok action I also want to have a action that when tapped takes the user to the settings app.
func displayAlert(title: String, message: String){
var formEmpty = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
formEmpty.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
})))
Use this code . May be help it.
override func viewDidAppear(animated: Bool) {
var alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .Alert)
var settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.sharedApplication().openURL(url)
}
}
var cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
alertController.addAction(settingsAction)
alertController.addAction(cancelAction)
presentViewController(alertController, animated: true, completion: nil);
}
Please note UIApplicationOpenSettingsURLString is only available on iOS8.0 and after so if your app should support iOS7 you'll have to check for availability of the constant (or if using Swift 2.0 use the #availability keyword).
You can navigate to the setting with this code:
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(settingsUrl!)
After adding this code in your function your function will look like:
func displayAlert(title: String, message: String){
var formEmpty = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
formEmpty.addAction((UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
//This will call when you press ok in your alertview
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(settingsUrl!)
})))
}
For iOS 10, Swift 3:
let alert = UIAlertController(title: "Alert!", message: "your message here", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.shared.open(settingsUrl as! URL, options: [:], completionHandler: nil)
alert.addAction(settingsAction)
present(alert, animated: true, completion: nil)

UIActionSheet crashes on iPad swift

I have the following button action in a toolbar:
#IBAction func share(sender: AnyObject) {
let modifiedURL1 = "http://www.declassifiedandratified.com/search.html?q=\(self.searchBar.text)"
let modifiedURL = modifiedURL1.stringByReplacingOccurrencesOfString(" ", withString: "%20", options: NSStringCompareOptions.LiteralSearch, range: nil)
let alert = UIAlertController(title: "Share", message: "Share your findings", preferredStyle: UIAlertControllerStyle.ActionSheet)
let twBtn = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default) { (alert) -> Void in
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter){
var twitterSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
twitterSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)")
self.presentViewController(twitterSheet, animated: true, completion: nil)
} else {
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 fbBtn = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default) { (alert) -> Void in
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook){
var facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookSheet.setInitialText("Look what I found on Declassified and Ratified: \(modifiedURL)")
self.presentViewController(facebookSheet, animated: true, completion: nil)
} else {
var 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)
}
}
let safariBtn = UIAlertAction(title: "Open in Safari", style: UIAlertActionStyle.Default) { (alert) -> Void in
let URL = NSURL(string: modifiedURL)
UIApplication.sharedApplication().openURL(URL!)
}
let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alert) -> Void in
println("Cancel Pressed")
}
let textBtn = UIAlertAction(title: "Message", style: UIAlertActionStyle.Default) { (alert) -> Void in
if (MFMessageComposeViewController.canSendText()) {
let controller = MFMessageComposeViewController()
controller.body = "Look what I found on Declassified and Ratified: \(modifiedURL)"
controller.messageComposeDelegate = self
self.presentViewController(controller, animated: true, completion: nil)
}
}
alert.addAction(twBtn)
alert.addAction(fbBtn)
alert.addAction(safariBtn)
alert.addAction(textBtn)
alert.addAction(cancelButton)
self.presentViewController(alert, animated: true, completion: nil)
}
However, this code crashes when called on an iPad with a sigbart crash (that is all I can see in the console). I see this is a common problem but the other solutions have not worked for me. I even set the version to the latest iOS and that didn't fix it. Can someone explain?
On an iPad, an action sheet is a popover. Therefore you must give its UIPopoverPresentationController a sourceView and sourceRect, or barButtonItem, so that it has something to attach its arrow to.

Resources