Tech

iOS7/8 Webview내 Javascript Alert 에 대한 타이틀을 바꾸기

가루군# 2014. 10. 20. 16:30

메모용도지만..


iOS 앱만들때 웹뷰안에 자바스크립으로 alert 이 있다면,

보통 상단은 해당 페이지 주소가 뜰것이다. http://blahbalh.com .... -_-

웹앱이거나 부분 웹경우 이런거 하나 보일때마다 신경쓰임으로 바꿔봅시다.

 

헤더에는 이렇게 추가해줍니다.


@interface UIWebView (Javascript)


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


@end



iOS6 이전까지는 굵게친부분 id가 WebFrame 인데 이게 뭐가 어쩌고 저쩌고해서 (찾아보세요 귀찮으니)
바뀐듯 id로 하면 잘굴러갑니다 ㅇㅇ

그럼 어디에 넣나요? .. 어디긴 h 파일에 넣으세요


요렇게.


그다음 m파일에도


@implementation UIWebView (Javascript)


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

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"타이틀" message:message delegate:nil cancelButtonTitle:@"확인" otherButtonTitles: nil];

    [alert show];

}


@end




이렇게.

이렇게 하면 alert이 웹뷰에서 냅다 뜨는게아니라 저기서 걸려서 작성한대로 뜹니다

저는 uialertview 로 넘겨서 타이틀을 가렸으니 (본 목적이 이거니까.) 작성했지만.

편한대로. 자기가 지지고 복고하면됩니다.