I'm trying to create an alert that prompts the user to name a song they have imported to a music sheet display app.
I have created a function for this naming process:
- (NSString *)nameImportedSong {
NSString *songName;
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: #"New Song"
message: #"Choose a song name"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"song name";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
NSString *chosenSongName = [NSString stringWithFormat:#"%#", namefield.text];
}]];
songName = ; // <-------------how do I assign chosenSongName to songName?
[self presentViewController:alertController animated:YES completion:nil];
return songName;
}
How can I assign chosenSongName from the alert into my songName variable, outside the alert?
Use __block keyword for the variable
__block NSString *chosenSongName;
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
chosenSongName = [NSString stringWithFormat:#"%#", namefield.text];
}]];
songName = chosenSongName;
NSLog(#"chosenSongName = %#",chosenSongName);
Related
I want to allow users to copy/cut but mainly PASTE data from other apps into my app. I need to allow them to paste data in UIAlertView, when they are logging to the app. How can i make it?
This is my code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:[NSString stringWithFormat:NSLocalizedString(#"enter_login", nil)]
delegate:self
cancelButtonTitle:#"Ok"
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:1] becomeFirstResponder];
[UserDefaultsServices resetLoginCredentials];
[UserDefaultsServices resetLoginData];
self.alertView = alert;
[alert show];
It does this:
show what my code does, I need to allow users to paste data in password TextField
You can use this answer for swift versions :
Objective C version of shared answer is :
- (IBAction)showAlert:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Add New Name" message:#"Your message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = #"Name";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = #"Password";
textField.secureTextEntry = true;
}];
UIAlertAction *saveAction = [UIAlertAction actionWithTitle:#"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// here you can access your textfields' texts
NSString *textField1 = alertController.textFields[0].text;
NSString *textField2 = alertController.textFields[1].text;
NSLog(#"Saving %# %#", textField1, textField2);
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// here you can access your textfields' texts
NSString *textField1 = alertController.textFields[0].text;
NSString *textField2 = alertController.textFields[1].text;
NSLog(#"Canceled %# %#", textField1, textField2);
}];
[alertController addAction:saveAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Above code work like this :
I am learning Objective-C,and I want to create a tableView.When i touch on a table,an alert view is presented.The alert view will show a name , a description and two textFields which can input the new name and description.How can i change the name and the description by clicking the "Ok" button.here is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Coder *coder = _allCoder[indexPath.row];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Jack" message:#"Jack is a coder" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder =#"name"; }];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"description";}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
UITextField *name = alertController.textFields.firstObject;
coder.name = name.text;
UITextField *desc = alertController.textFields.lastObject;
coder.desc = desc.text;
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
I'm trying to get a textField value from UIAlertController, but whenever I try to enter the value (Name) and display it on label, the output doesn't show anything.
- (IBAction)button:(id)sender {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"Test" message:#"Please Enter your name " preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Person Name ";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.secureTextEntry = NO;
}];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"ENTER, please"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Handle the ENTER button
// how to Display the name in the label
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:#"No, thanks"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Handle no button.
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
}
Quick swift example I pulled from an old project
alert.addAction(UIAlertAction(title: "Send", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
if let textFields = alert.textFields {
if let textField = textFields.first {
}
}
}))
In your application it would like something like this:
UIAlertAction* yesButton = [UIAlertAction actionWithTitle:#"ENTER, please" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
NSArray *textFields = alert.textFields;
// Probably smart to do some nil checking here
NSLogv(#"%#", textFields.first.text);
// add to label like so
label.text = textFields.first.text;
}];
I have a UIAlertController, which contains a textfield and 2 buttons: OK and Cancel. i want to retrieve the text that the user entered in the textfield when he presses OK, but because the textfield is in a block i cant access the textField.text field that is inside the block from the OK button block. how do i access it and use the text entered by the user? see my code below.
- (IBAction)saveItinerary:(id)sender
{
NSString *itineraryNameInput = [[NSString alloc] init];
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Save Itinerary"
message:#"Enter Name for the Itinerary"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Name";
textField.text = itineraryNameInput;
}];
NSLog(#"name = %#", itineraryNameInput);
[self presentViewController:alert animated:YES completion:nil];
}
Use the textFields property of the UIAlertController.
UITextField *textField = alert.textFields.firstObject;
Is there a supported way of launching Google Authenticator on iOS?
I want to make it easier for customers to open the app and copy out the time-based code, before pasting it back into my app.
I've empirically discovered that this (Swift) code will launch the app:
UIApplication.sharedApplication().openURL(NSURL(string: "otpauth://")!)
...but I want to know if there is a better, supported way.
Specifically, is the otpauth:// protocol supported without arguments to simply launch the app?
Looking at the Git repo for the app it does seem like they have registered the Custom URL Schemes for bot otpauth and totp
https://github.com/google/google-authenticator/blob/bd50d15c348a978c314d2b30e586fbc562096223/mobile/ios/OTPAuth-Info.plist#L42
And here
https://github.com/google/google-authenticator/blob/bd50d15c348a978c314d2b30e586fbc562096223/mobile/ios/Classes/OTPAuthURL.h#L23
And here is the documentation on how exactly to build the url:
https://github.com/google/google-authenticator/wiki/Key-Uri-Format
After you form them correctly and get your app and the Google Authenticator app on the same device you would just need to test.
Objective C
if ([[[notification realRequestResults] valueForKey:#"action"] isEqualToString:#"2FA"]) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Two Factor Authentication"
message:#"Please, enter your Google Authenticator 2FA Token."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"Confirm Token"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
#try {
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
[userName text], #"loginName",
[passwordField text], #"password",
#"false", #"rememberMe",
[[alert textFields][0] text], #"tfa",
nil];
[self callWebserviceForIdentifier:AuthRequestInternalLogin
withParameters:parameters
onSuccessSelector:#selector(loginSuccessfulAgain:)
onFailureSelector:#selector(loginFailedAgain:)];
} #catch (NSException *exception) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"login"
message:[NSString stringWithFormat:#"%#", exception.description]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
} #finally {
}
}];
[alert addAction:defaultAction];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.delegate = self;
textField.placeholder = [NSMutableString stringWithString:#"Enter your 2FA token"];
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.font = [UIFont systemFontOfSize:16.0];
textField.textAlignment = NSTextAlignmentCenter;
textField.textColor = UIColor.blackColor;
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"authenticator.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(authenticatorBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeAlways;
textField.rightView = addButton;
}];
[self presentViewController:alert animated:YES completion:nil];
}
else {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Warning"
message:#"Invalid Credentials. Please try again."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
[self stopAnimation];
}
}
-(IBAction)authenticatorBtnClicked:(id)sender{
NSString *AppStoreURL = #"https://apps.apple.com/in/app/google-authenticator/id388497605";
NSString *customAppURL = #"otpauth://";
BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customAppURL]];
NSString *url = canOpenURL ? customAppURL : AppStoreURL;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URL]];
}
In Info.plist File