iOS share extension dismiss keyboard - ios

I'm implementing a share extension for my app, so far everything is going good except I can't seem to dismiss the keyboard that automatically opens using the default layout/storyboard.
I'm keeping the default design/layout (SLComposeServiceViewController) which includes the preview image and UITextview, the UITextview automatically gets in focus which opens the keyboard.
Normally this is fine, but if you're not logged in my app I display an UIAlertController saying you need to login to share. The problem is the keyboard opens at the same time as the alert.
I've tried [self.view endEditing:YES]; and [self.textView resignFirstResponder]; in both viewDidLoad, viewDidAppear and viewWillAppear with no luck.

Found the answer! I didn't read the docs very carefully...
I had to do [self.textView resignFirstResponder]; in -(void)presentationAnimationDidFinish

my way is to use UITextViewDelegate
- (void)viewDidLoad {
[super viewDidLoad];
self.textView.delegate = self;
self.canShare = NO;
[self.view setAlpha:0.0];
}
change canShare to YES in your check login logic
- (void)checkLoggedIn {
if ([[ShareAccountManager checkLoggedIn]) {
self.canShare = YES;
[self.view setAlpha:1.0];
}
}
and implement method textViewShouldBeginEditing
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if (self.canShare) {
return YES;
}
return NO;
}

Related

Keyboard for textfield appears after second tap (time to time)

I have one view in my app where there is 1 text field. And I've noticed that keyboard appears after second tap.
But it's interesting that on iPhone it's time to time (some time appears after first tap at once, and some time after second tap only).
On iPad looks like it more ofter appears after second tap only.
I use UITextFieldDelegate
in viewDidLoad I assign the delegate _locationTextField.delegate = self;
and I use delegate methods textFieldDidBeginEditing, textFieldDidEndEditing, textFieldShouldReturn
e.g.:
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_locationNameBeforeManualEdit = _locationTextField.text;
// save the previod city value to compare after did end editing
NSLog(#"textFieldDidBeginEditing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"textFieldDidEndEditing");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self continueButtonPressed:nil];
// [textField resignFirstResponder];
return YES;
}
in storyboard
What could be the problem?
Found solution here on Stackoverflow - solution related with keyboard preload:
- (void)preloadKeyboard {
UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
}
This method should be used in application: didFinishLaunchingWithOptions: method of AppDelegate.

Can not Hide Keyboard with SLComposeServiceViewController

My Share Extension doesn't require any user input aside from configuration in a table view so I am trying to hide the keyboard when the view is presented in Safari. I'm able to run my code in the simulator fine but when I test on my device, I'm the Share Extension doesn't launch and Safari hangs.
I've tried a few ways to prevent the keyboard from launching
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.textView.text = #"\n Place Holder Text";
self.textView.editable = NO;
}
As well, I tried it in loadView since that is where SLComposeServiceViewController
sets the textView and the textView delegate.
-(void)loadView{
[super viewWillAppear:animated];
self.textView.text = #"\n Place Holder Text";
self.textView.editable = NO;
}
And just for fun
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return NO;
}
All of these work on the Simulator but not on my device.
What could be happening?
Is there some kind of Notification or Observer that I'm (or Safari is) missing
You just need to put your code in "viewDidAppear" function.
It works fine for me.
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.textView setText:#" Place Holder Text"];
[self.textView setEditable: NO];
}

Show Custom Keyboard on Launch

I am using the following custom keyboard:
https://github.com/kulpreetchilana/Custom-iOS-Keyboards
How do you make it so that they keyboard is displayed at the beginning without having to touch the text view?
Depending on the version of iOS you're using (not sure if it works on 8), you can do:
- (void)viewDidLoad;
{
[super viewDidLoad];
[self.textField becomeFirstResponder];
}
Or
- (void)viewDidLayoutSubviews;
{
[super viewDidLayoutSubviews];
[self.textField becomeFirstResponder];
}
In your ViewController.
Tapping a text view will call becomeFirstResponder on it.
So you should be able to make the keyboard appear with...
[textView becomeFirstResponder];
I've never used that framework though so not 100% sure.

Hide Keypad on click of textfield IOS

I am devolepeing an app where i am entering data to a textfield from custom view(not input view).Problem is that i don't want the system keypad to popup when i touch the textfield.I have gone through Apple text programming document but was not able to find the solution.How to achieve this.
You can also use gestures,
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:gestureRecognizer];
}
- (void)hideKeyboard {
[self.view endEditing:YES];
}
try this code may help you
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==/**urs testField on which you want to remove keyboard on click*/ ) {
[textField resignFirstResponder];
//*its depend your requirement
[here you can call yours custom view];
}
return YES;
}
adjust as per req..
Set the userInteractionEnabled property:
//UITextField *text;
text.userInteractionEnabled = NO;
Not sure, what exactly you are expecting. I hope that you need to handle the UITextField delegates but without UIKeyboard : If so, then implement delegate of
-(void)textFieldDidBeginEditing:(UITextField *)sender{
// resign the textfield and do your stuff with the data
}
Assign a IBOutlet property to that particular UITextField
and then in viewDidLoad add the following code [self.YourUITextField setUserInteractionEnabled:NO];
Disable the editing for the particular field
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if ([textField isEqual:myTextField]) {
return NO;
}
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view endEditing:YES]; // this statement will hide keyboard. Useful when we use many textfields based on tag value.
return YES;
}

iOS iPad app not hiding keyboard

In my app I have a view that is a form that has quite a few inputs.
When the UITextField calls textFieldDidBeginEditing, it checks the tag and will bring up a UIPopoverController or the keyboard depending on the what the input is meant to be.
If the keyboard is up, I need it disappear when the user presses a textfield that brings up the popover. However I cannot make it disappear, I have tried every way to get rid of the keyboard but it just stays there. I have tried:
calling resignFirstResponder in textFieldDidEndEditing
calling [self.view endEditing:YES] in textFieldDidEndEditing
calling resignFirstResponder AND [self.view endEditing:YES] in textFieldDidBeginEditing checking for the previous tag is equal to a keyboard input text field.
Any ideas would be great.
I have ripped it out and and put it in a example project if anyone wants to see the exact behaviour.
http://dl.dropbox.com/u/61692457/KB_Test.zip
Declare a Global UITextField in .h file
UITextField *txtfld;
Replace Your method textFieldDidBeginEditing with textFieldShouldBeginEditing and now write this code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 1 || textField.tag==3)
{
if(numPickerPopover == nil)
{
numPicker = [[[NumPicker alloc] initWithStyle:UITableViewStylePlain] autorelease];
numPicker.delegate = self;
numPickerPopover = [[UIPopoverController alloc] initWithContentViewController:numPicker];
[numPickerPopover setPopoverContentSize:CGSizeMake(60.0, 260.0f)];
}
[numPickerPopover presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[txtfld resignFirstResponder];
return NO;
}
if (textField.tag == 2)
{
txtfld = textField;
return YES;
}
return YES;
}
To dismiss the keyboard when the user touches the textField that brought it up, add this method:
- (IBAction)dismissKeyboard:(id)sender {
[textField resignFirstResponder];
}
In Interface Builder, connect this method to the textField event you want, like touch up inside (or whatever is more appropriate).

Resources