투케이2K

160. (Objective-C/objc) [간단 소스] WKWebview 웹뷰 runJavaScriptAlertPanelWithMessage 사용해 Alert 팝업창 이벤트 감지 본문

Objective-C

160. (Objective-C/objc) [간단 소스] WKWebview 웹뷰 runJavaScriptAlertPanelWithMessage 사용해 Alert 팝업창 이벤트 감지

투케이2K 2024. 9. 22. 19:32

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

 

    // ----------------------------------------------------------------------
    // [사전 설정 사항] 
    // ----------------------------------------------------------------------
    // 1. WKUIDelegate 정의 필요 : 
    // 
    // @interface ViewController : UIViewController <WKUIDelegate , WKNavigationDelegate , WKScriptMessageHandler>
    // ----------------------------------------------------------------------
    // 2. 딜리게이트 정의 필요 : 
    //
    // [webView setUIDelegate:self];
    // ----------------------------------------------------------------------





    // ----------------------------------------------------------------------
    // [소스 코드] 
    // ----------------------------------------------------------------------

        - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
           
            
            UIAlertController * alert = [UIAlertController
                                          alertControllerWithTitle:@"알림"
                                          message:message
                                          preferredStyle:UIAlertControllerStyleAlert];
             
            UIAlertAction* ok = [UIAlertAction
                                 actionWithTitle:@"확인"
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                    completionHandler();

                                 }];
            
            UIAlertAction* cancel = [UIAlertAction
                                     actionWithTitle:@"취소"
                                     style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction * action)
                                     {
                                          completionHandler();
                                          
                                     }];

            [alert addAction:ok];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:nil];


        }

    // ----------------------------------------------------------------------

 

반응형
Comments