__SOLVED__ iPhoneKeyboard.Open BREAKS with multiple entries

iPhoneKeyboard.Open seems to break when there are two entries. The second entry replaces the first. For example, if the first entry is a user NAME and the second one a PASSWORD, the name field is replaced with the password after the player enters the pass field. [See code below].

Is this a bug or my bad programming? The only fix I can think of would be to do these in two different scenes, entering the name in one, then loading another scene and entering the password, but that is super clunky. Any sensible fix would be really appreciated. thanks, xian

static var inputText : String = “text”;
static var inputText2 : String = “text2”;
private var keyboard : iPhoneKeyboard;
private var keyboard2 : iPhoneKeyboard;

function OnGUI() {
if (GUI.Button(Rect(10, 10, 200, 32), inputText))
keyboard = iPhoneKeyboard.Open(inputText);

if (keyboard)
inputText = keyboard.text;

if (GUI.Button(Rect(10, 50, 200, 32), inputText2))
keyboard2 = iPhoneKeyboard.Open(inputText2);

if (keyboard2)
inputText2 = keyboard2.text;
}

By the way, I tried separating the code out into two objects and it still doesn’t work.

272537--9802--$user_name_password_347625_884.jpg

hey xian.

here is some code that should point you in the right direction. Not tested it a lot but seems to work fine and you can expand upon it if you need to.

//buttons are displayed (username and password)
//click corresponding button to enter either and text is displayed
//buttons not visible when keyboard showing
//holdingText in place should you want to expand it
//to include a cancel text button when keyboard is showing/

using UnityEngine;
using System.Collections;

public class UsernameAndPassword : MonoBehaviour {

	//properties
	iPhoneKeyboard keyboard; // the reference to the on screen keyboard
	string userName = ""; // what will be displayed on the Label
	string password = ""; // what will be displayed on the Label
	string holdingText; // a intermediate text holding area
	bool setUsername = false;
	bool setPassword = false;

	//methods
	void Start()
	{
		iPhoneKeyboard.autorotateToPortraitUpsideDown = false;
		iPhoneKeyboard.autorotateToLandscapeLeft = false;
		iPhoneKeyboard.autorotateToLandscapeRight = false;
	}
	
	void OnGUI()
	{
			
		GUI.Label(new Rect(60,110,200,80),userName);
		GUI.Label(new Rect(60,150,200,80),password);
		
		if(!iPhoneKeyboard.visible  GUI.Button(new Rect(60,10,200,42),"Username"))
		{
			setUsername = true;
			setPassword = false;
			holdingText = userName; 
			keyboard = iPhoneKeyboard.Open(""); // open the Default keyboard
		}
		
		if(!iPhoneKeyboard.visible  GUI.Button(new Rect(60,55,200,42),"Password"))
		{
			setUsername = false;
			setPassword = true;
			holdingText = password; 
			keyboard = iPhoneKeyboard.Open(""); // open the Default keyboard
		}
		
		if(keyboard != null  keyboard.text != "")
		{
			if(setUsername)
				userName = keyboard.text;
			if(setPassword)
				password = keyboard.text;
		}
		
	}
	
	
		
}

Not the best way to manage this as you may be better opening the keyboard in secure mode but this follows your attempt more closely.

This method should also solve the issue you had with the rotating TextField.

Thanks Imparare!

Your solution is perfect! Thank you for sharing your expertise and other suggestion regarding the keyboard bug.

I had to get two keyboards working quickly several days ago so I used a basic toggle within OnGUI:

if (GUI.Button(Rect(310, 190, 160, 32), name)){
keyboardName = iPhoneKeyboard.Open(name);
inputSwitch=1;
}

then in Update I have listeners to the toggle:

if (inputSwitch==1){
if(keyboardName){
if (keyboardName.active){

etc.

thanks again!
xian

its not workingn for me xian,
my problem is unity GUI functions not working when iphonekeyboard in active state. How to overcome this problem . thanks in advance