Lomohome.com :: 괴발자 모근원

지난 포스트를 보면 웹뷰에서 자바스크립트 alert 을 웹뷰에서 재정의 해서 썼는데
개발을 하다가 난관에 부딫혔다. 자바스크립트의 alert 말고, confirm 을 사용할때에도 재정의가 필요해졌다.

confirm 창은 특성상, '예','아니오' 버튼이 나오고 이 버튼이 눌리면 버튼에 따라 액션을 달리 취해주어야한다.

그런데 UIAlertView 를 show 하게 되면 예, 아니오 버튼과 상관없이 밑의 코드가 주~욱 실행되버려서 예,아니오의 리턴값을 받을수가 없다. (말로 표현하자니.. 이거 영..)


억지로 버튼을 누르기전까지 루프를 돌려서 지연을 시키고, UIAlertView 가 사라지는 시점에 버튼의 결과를 가지고와서 처리하는것으로 변경한 코드.
분명 더 좋은 방법이 있을것 같은데 ㅠ 못찾겠다.

*헤더부분은 생략.

//@Geunwon,Mo 2010.9.17 : UIWebView Javascript alert 위한 카테고리.

@implementation UIWebView (JavaScriptAlert)

.... 저번포스트 생략 ....

static BOOL diagStat = NO; //예,아니오 버튼의 상태를 저장할 임시 변수

- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame{

NSLog(@"javascript ConfirmPanel : %@",message);

UIAlertView *confirmDiag = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:NSLocalizedString(@"Yes", @"") otherButtonTitles:NSLocalizedString(@"No", @"아니오"), nil];

[confirmDiag show];

//버튼 누르기전까지 지연.

while (confirmDiag.hidden == NO && confirmDiag.superview != nil)

[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];

[confirmDiag release];

return diagStat;

}



//요놈은 UIAlertViewDelegate 를 구현하여 버튼이 눌렸을때 실행될 메소드

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

//index 0 : YES , 1 : NO

if (buttonIndex == 0){

//return YES;

diagStat = YES;

} else if (buttonIndex == 1) {

//return NO;

diagStat = NO;

}

}

@end


동작순서는 웹뷰에서 confirm 자바스크립트 함수가 호출이 되면 카테고리로 구현한 webView: runJavaScriptConfirmPanelWithMessage 메소드가 호출이되고,
UIAlertView 로 메세지창(+예,아니오)을 띄우고 while 문을 통하여 메세지 창이 없어질때까지 루프..;;
그리고 메세지창에서 버튼을 누르면 alertView: clickedButtonAtIndex 메소드가 호출이 되고 미리 준비해둔 전역변수에 버튼에 따라 상태값을 저장.
메세지창이 닫히면서 while 루프가 끝나게 되고 전역변수의 값을 자바스크립트로 리턴. 하게된다.


* 더 좋은 방법이 있으시면 공유 부탁드립니다. ㅠㅠ
Posted by 모근원
iPhone 어플에서 UIWebView 를 사용한 프로그램에서
웹뷰안에서 JavaScript 의 Alert 를 호출 하면  (ex -> javascript:alert("test some strings.");)
다음과 같이 호스트 이름이 얼럿창의 상단에 찍히게 된다.

보안때문에 상단의 ip 는 지웠지만 만약 웹뷰에서 lomohome.com  에서 얼럿창을 띄웠다면  lomohome.com 이라는 문구가 얼럿창의 타이틀에 뜨게 된다.

이 얼럿창은 UIWebView 에 카테고리를 이용하여 딜리게이트 메소드를 구현해주면 수정할 수 있다.
먼저 UIWebView 의 헤더파일에 다음과 같은 인터페이스를 추가해준다.

//@Geunwon,Mo 2010.9.17 : UIWebView Javascript alert 위한 카테고리.

@interface UIWebView (JavaScriptAlert) 

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;

@end


그리고 구현파일에 구현해준다.
 

//@Geunwon,Mo 2010.9.17 : UIWebView Javascript alert 위한 카테고리.

@implementation UIWebView (JavaScriptAlert)

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {

NSLog(@"javascript alert : %@",message);

    UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"Hana Bank" message:message delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];

    [customAlert show];

    [customAlert autorelease];

}

@end


 요렇게 구현하고 나면 다음과 같이 Title  에 원하는 문구로 자바스크립트 얼럿을 띄울 수 있다.



구현 메소드 중, 타이틀 인자를 nil 로 넣으면 아무것도 안찍힌다.

UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];




Posted by 모근원