iPhone UIWebview: How to force a numeric keyboard? Is it possible?

IphoneKeyboardUiwebviewCordova

Iphone Problem Overview


I'm experimenting with PhoneGap to develop some iPhone apps. PhoneGap basically wraps a UIWebView - it works well. The problem is the my app has several input fields that only take numeric input. I really need to force the numeric keypad instead of accepting the default standard keyboard, which then forces the user to switch to the numeric on every field. Does anyone know if this is possible? How?

Clarification: I know that Apple doesn't currently provide any API to allow this. I'm wondering if creating a custom class that inherited from UIWebView might help, or maybe creating a custom keyboard would open up some possibilities?

Update 6 Nov, 2009 - As noted below, Apple has recently updated safari functionality so that this is, once again, supported. So the accepted answer was changed to reflect that.

 <html>
<input type='tel'/>
<input type='number'/>
<input type='email'/>
<input />
</html>

Iphone Solutions


Solution 1 - Iphone

It looks like Mobile Safari supports the new HTML5 input type attributes of email, number, search, tel, and url. These will switch the keyboard that is displayed. See the type attribute.

So for example, you could do this:

<input type="number" />

And when the input box has focus, the number keyboard is shown (as if the user had the full keyboard and hit the "123" button.

If you really only want numbers, you could specify:

<input type="tel" />

And then the user would get the phone number dialing keypad.

I know this works with Mobile Safari -- I only assume it will work with UIWebView.

Solution 2 - Iphone

I found a better solution is to use <input type="text" pattern="[0-9]*"> when designing forms for both mobile and desktop browsers.

Solution 3 - Iphone

From Apple's documentation (the note about UIWebView):

> You cannot specify the keyboard type in input elements. The web view displays a custom keyboard that is based on the default keyboard but includes some additional controls for navigating between form elements.

Solution 4 - Iphone

Checked on IPhone OS 3.0, this functionality was still not there, not sure why Apple just removed this handy functionality, really upset to work on this right now :(

Solution 5 - Iphone

Here is a list of every type compatible with iOS (4.0 and greater) web views:

http://conecode.com/news/2011/12/mobile-safari-uiwebview-input-types/

Solution 6 - Iphone

This was possible in the first iteration of the iPhone OS - Safari would give numeric keyboards to form fields with 'zip' or 'phone' in their names - but it disappeared in iPhone OS 2.0.

PhoneGap doesn't have an API function to override this at the moment. It may be something they're working on, it'd definitely be a nifty feature.

Solution 7 - Iphone

You need to make the change in the HTML portion of your Dashcode project:

  1. In Dashcode open index.html in the code window.
  2. On the canvas of the view you are working on, select the input field that you want to set to use the optimized keyboard.
  3. Click on index.html to give it focus.
  4. Select edit>find from the Dashcode menu bar.
  5. Type into the find box the exact name you gave to the text field control. Find will locate the control in the index.html file.
  6. Change the type from type="text" to type="number".

Done.

Solution 8 - Iphone

iOS Mobile Safari also supports:

<input type="password" />

Solution 9 - Iphone

What you can also use for iOS phonegap based app is :

input type="number" pattern="[0-9]*"

It is shown in the image below. Works beautifully with iOS 8.2 and phonegap 3.5 and above.

Solution 10 - Iphone

iOs has a numeric keyboard UIKeyboardTypeNumbersAndPunctuation, but this can't be called from HTML (https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html)

since on the "tel" keyboard the punctuation is missing you have to create this on your own. Since ios8 custom keyboards are available. Or if you just need the punctuation you can add a button (https://stackoverflow.com/questions/20192303/how-to-add-a-done-button-to-numpad-keyboard-in-ios7) to the numeric keyboard which pops up when you specify your input field with type="number" pattern="[0-9]*" .

To do so: the objective-c code.

-(void)keyboardWillHide:(NSNotification *)note
{
    [self.donekeyBoardBtn removeFromSuperview];
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat: @"document.activeElement.blur()"]];
}

- (void)keyboardWillShow:(NSNotification *)note
{
    [UIView setAnimationsEnabled:NO];
    // create custom button
    //UIKeyboardTypeNumberPadin
    //type="number" pattern="[0-9]*"
    UIButton *extryB= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    extryB.frame = CGRectMake(0, self.view.frame.size.height+150, 100, 50);
    extryB.backgroundColor = [UIColor colorWithRed:204.0f/255.0f
                                             green:210.0f/255.0f
                                              blue:217.0f/255.0f
                                             alpha:1.0f];
    extryB.adjustsImageWhenHighlighted = NO;
    extryB.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [extryB setTitle:@"," forState:UIControlStateNormal];
    [extryB setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [extryB addTarget:self action:@selector(extryBpressed) forControlEvents:UIControlEventTouchUpInside];



self.donekeyBoardBtn = extryB;

// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
    keyboard = [tempWindow.subviews objectAtIndex:i];
    // keyboard view found; add the custom button to it
    
    if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES)
    {
        
        for(int i = 0 ; i < [keyboard.subviews count] ; i++)
        {
            UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
            
            if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES)
            {
                UIButton* donebtn = (UIButton*)[hostkeyboard viewWithTag:67123];
                if (donebtn == nil){
                    //to avoid adding again and again as per my requirement (previous and next button on keyboard)
                    
                    if([[self printKeyboardType] isEqualToString: @"UIKeyboardTypePhonePad"]){
                        [keyboard addSubview:extryB];
                    }
                    
                }
            }
        }
    }
    //[keyboard addSubview:extryB];
}
[UIView setAnimationsEnabled:YES];
// animate
[UIView animateWithDuration:0.5 animations:^{
    extryB.frame = CGRectMake(0, self.view.frame.size.height-50, 100, 50);
}];
}

-(NSString*)printKeyboardType
{

NSString* strKeyboardType = @"";
switch (self.textDocumentProxy.keyboardType) {
    case UIKeyboardTypeAlphabet:
        strKeyboardType = @"UIKeyboardTypeAlphabet";
        break;
    case UIKeyboardTypeDecimalPad:
        strKeyboardType = @"UIKeyboardTypeAlphabet";
        break;
    case UIKeyboardTypeDefault:
        strKeyboardType = @"UIKeyboardTypeDefault";
        break;
    case UIKeyboardTypeEmailAddress:
        strKeyboardType = @"UIKeyboardTypeEmailAddress";
        break;
    case UIKeyboardTypeTwitter:
        strKeyboardType = @"UIKeyboardTypeTwitter";
        break;
    case UIKeyboardTypeNamePhonePad:
        strKeyboardType = @"UIKeyboardTypeNamePhonePad";
        break;
    case UIKeyboardTypeNumberPad:
        strKeyboardType = @"UIKeyboardTypeNumberPad";
        break;
    case UIKeyboardTypeNumbersAndPunctuation:
        strKeyboardType = @"UIKeyboardTypeNumbersAndPunctuation";
        break;
    case UIKeyboardTypePhonePad:
        strKeyboardType = @"UIKeyboardTypePhonePad";
        break;
    case UIKeyboardTypeURL:
        strKeyboardType = @"UIKeyboardTypeURL";
        break;
    case UIKeyboardTypeWebSearch:
        strKeyboardType = @"UIKeyboardTypeWebSearch";
        break;
    default:
        strKeyboardType = @"UNKNOWN";
        break;
}
NSLog(@"self.textDocumentProxy.keyboardType=%@", strKeyboardType);
return strKeyboardType;
}

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

- (void)extryBpressed{
    //simulates a "." press
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat: @"simulateKeyPress('.');"]];
}

Then you should add the a method which can be accessed application wide in your JS code.

simulateKeyPress: function(k) {
        var press = jQuery.Event("keypress");

        press.which = k;
        // place the id of your most outer html tag here
        $("#body").trigger(press);
            // just needed because my active element has a child element where the value should be placed in
                var id = document.activeElement.id.indexOf("-");
                if(id != -1){
                    var currentTf = sap.ui.getCore().byId(document.activeElement.id.split("-")[0]);
                    //append the value and set it (my textfield has a method setValue())
                    var currentTfValue = currentTf.getValue();
                    currentTf.setValue(currentTfValue + k);
                }
            },

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJJ RohrerView Question on Stackoverflow
Solution 1 - IphoneChris HusemanView Answer on Stackoverflow
Solution 2 - Iphonejon__oView Answer on Stackoverflow
Solution 3 - IphoneashcatchView Answer on Stackoverflow
Solution 4 - IphoneLeon GuanView Answer on Stackoverflow
Solution 5 - IphoneChrisView Answer on Stackoverflow
Solution 6 - IphoneceejayozView Answer on Stackoverflow
Solution 7 - IphoneJAntonView Answer on Stackoverflow
Solution 8 - IphonekaleazyView Answer on Stackoverflow
Solution 9 - IphoneSomView Answer on Stackoverflow
Solution 10 - IphoneDominik FeiningerView Answer on Stackoverflow