-2

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

Rakesh Patel
  • 1,673
  • 10
  • 27

1 Answers1

1

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 :

Copy/Paste Demo

  • I still dont know how to make it :( I copy code you sent me but it still doesnt work. Should I rename function on my "showLoginAlertViewWithTitle", right? – Timco Vanco Aug 01 '18 at 07:28
  • yes, you have to configure it according to your code. You need to add your actions (whatever you're doing with `ok` or `cancel`). – Kamaldeep singh Bhatia Aug 01 '18 at 07:29
  • Thanks, but it still doesnt do anything. I renamed function, and I am calling it, but nothing appears. It cant be done easier? I just need to allow copy/paste for users, is it impossible to do that like with one command? :D – Timco Vanco Aug 01 '18 at 07:39
  • See.. It isn't difficult may be your code is breaking somewhere. Here I have attached the output also. – Kamaldeep singh Bhatia Aug 01 '18 at 07:46
  • ouu! I really want this, but my code work like this - https://imgur.com/a/9dRUfgI – Timco Vanco Aug 01 '18 at 07:48
  • Show your code where you're calling it and how you're using it. Your alert isn't showing here. Are you getting any error in logs? – Kamaldeep singh Bhatia Aug 01 '18 at 07:51
  • inside `showLoginAlertViewWithTitle` you have added above code, right? I think you don't need `clickButtonAtIndex` here. your code for specific button should be placed inside the block. – Kamaldeep singh Bhatia Aug 01 '18 at 08:01
  • even when I delete that function - clickedButtonIndex - your code still doesnt working, I dont know where could be mistake....Is it possible to use PasteBoard for paste date from other app? – Timco Vanco Aug 01 '18 at 08:11
  • Can I know where you're presenting this alert? I mean is it on an existing `viewController` or you're using it as standalone alert? – Kamaldeep singh Bhatia Aug 01 '18 at 08:18
  • This Alert shows every-time, when user is logging. So user start App and then this alert shows. And I need, if user has some other app, for example lastpass where he copy password and then he want it to paste it in my app. – Timco Vanco Aug 01 '18 at 08:35
  • You need a `view controller` say `mainViewController` then in `viewDidAppear` you can call this alert OR you can try this https://stackoverflow.com/a/35672915/5622566 for this you need to replace `self` in `[self presentViewController:alertController animated:YES completion:nil];` with `rootcontroller` of app. – Kamaldeep singh Bhatia Aug 01 '18 at 08:43
  • Related to copy paste, Yes you can use `PasteBoard` but it is also deprecated in iOS 10 and from then it'll be handled implicitly so I don't think you need this now – Kamaldeep singh Bhatia Aug 01 '18 at 08:44
  • RootController is not working - it is showing me an error. – Timco Vanco Aug 01 '18 at 08:49
  • what will change if I change first row to this - "- (void) showLoginAlertViewWithTitle:(NSString *)title" – Timco Vanco Aug 01 '18 at 09:00
  • today I made some changes in code and now options like - copy, cut and paste - are shown me. But when I click on paste or copy, nothing happens. It seems me, like that buttons are blocked or cant do what they should... Please do you know, what it could be? – Timco Vanco Aug 02 '18 at 12:11
  • Try viewHierarchy debugger to debug more... Not sure why it's happening. :| – Kamaldeep singh Bhatia Aug 02 '18 at 12:32