2011-03-05 4 views
2

내 앱에서 사용자가 사용자 정의보기에 로그인 정보를 입력 한 다음 UIWebView를 표시하게 할 수 있습니다. UIWebView가 프로그래밍 방식으로 사용자의 사용자 이름과 암호로 채워지 길 원합니다. 그런 다음 UIWebView는 프로그래밍 방식으로 로그인 단추를 클릭해야합니다. 나는 노력해왔다 :프로그래밍 방식으로 데이터를 입력하십시오. UIWebview

[webView stringByEvaluatingJavaScriptFromString:@"document.L_lbsc_txtUserName.text.value='ANdrew';"]; 

그러나 작동하지 않는다. stringByEvaluatingJavaScriptFromString의 작동 방식이나 수행 방법을 실제로 이해하지 못합니다.

위의 문자열 필드에 입력 한 것과 관련이있을 수 있습니다. 나는 사용자 이름과 암호 필드를 대체하기 위해 노력하고있어

<div class="lgntxt"> 
    <div style="padding-left:3px;"> 
    <input name="L$lbsc$txtUserName" type="text" value=" -username-" size="10" id="L_lbsc_txtUserName" tabindex="1" class="textbox" onfocus="if(this.value == ' -username-') this.value = ''; Hide('L_lbsc_txtPasswordHelp'); Show('L_lbsc_txtPassword');" onblur="if(this.value == '') this.value = ' -username-';" style="width:80px;"><br> 
    <img src="/podium/images/spacer.gif" border="0" width="3" height="1"><br> 
    <input name="L$lbsc$txtPasswordHelp" type="text" value=" -password-" size="10" id="L_lbsc_txtPasswordHelp" tabindex="1" class="textbox" onfocus="Hide('L_lbsc_txtPasswordHelp'); Show('L_lbsc_txtPassword'); document.getElementById('L_lbsc_txtPassword').focus();" style="width:80px;display:;"> 
    <input name="L$lbsc$txtPassword" type="password" size="10" id="L_lbsc_txtPassword" tabindex="2" class="textbox" onkeydown="if ((event.which ? event.which : event.keyCode) == 13) {return false;};" onkeyup="if ((event.which ? event.which : event.keyCode) == 13) {__doPostBack('L$lbsc$btnLogin','');return false;};" onblur="if(this.value == '') {Show('L_lbsc_txtPasswordHelp'); Hide('L_lbsc_txtPassword');}" style="width:80px;display:none;"> 
    </div> 
</div> 

: 는 여기에 액세스하려고하고있어 웹 사이트의 소스 코드입니다.

도움을 주시면 감사하겠습니다. 미리 감사드립니다.

편집 그래서 다음을 시도하고 그들 중 누구도 작동하지 : 방법은 문서의 맥락에서 그것을 실행, 자바 스크립트를 필요

[webView stringByEvaluatingJavaScriptFromString:@"document.myForm.myText.value='text from Obj-C';"]; 

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('L$lbsc$txtUserName').value='ANdrew';"]; 

답변

4

그건, 다음에 결과를 반환합니다. 귀하의 JavaScript가 잘못되었습니다. 시도 :

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('L$lbsc$txtUserName').value='ANdrew';"]; 

당신이 방법은, the iOS reference library로 이동 the class reference for the class you are using을 찾아 the description of the method you are using 아래로 스크롤 무엇을 모르는 경우.

+0

감사합니다. 어떤 방법을 사용해야합니까? viewDidLoad, webViewDidFinishLoad? – Andrew

+0

글쓰기가 끝나기 전에 페이지의 내용에 접근 할 수 있어야합니다. 그래서'webViewDidFinishLoad :'가 아마도 적절한 장소 일 것입니다. – Jim

+0

나는 그것을 시도했으나 효과가없는 것으로 보인다. 그것이 작동하는 경우 UIWebView에 나타나지 않습니다. 보기 등을 다시로드해야합니까? – Andrew

3

빌, 나는 아래에서 일하는 것을 얻을 수있었습니다. 기본적으로 ID가 'logon-field' 인 요소 만 있습니다. 자바 스크립트를 여러 줄로 나누어 내 변수 (사용자 정의 화면의 UITextField's 사용자 입력 값, 내 UIWebView 맨 위에 있음)를 삽입하고 연결할 수 있습니다. 나는 사용자가 텍스트 필드에 입력을 완료 할 때까지 실행되지 않도록보기 컨트롤러의 textFieldShouldReturn : 메서드에 아래 코드를 입력합니다.

NSString *logonJSStart = @"document.getElementById('logon-field').value='"; 

NSString *logonWithValue = [logonJSStart stringByAppendingString:self.logon.text]; 

NSString *logonJSFinal = [logonWithValue stringByAppendingString:@"';"]; 

[webView stringByEvaluatingJavaScriptFromString:logonJSFinal]; 
관련 문제