투케이2K

162. (Objective-C/objc) [간단 소스] WKWebview 웹뷰 runJavaScriptTextInputPanelWithPrompt 입력 팝업창 이벤트 감지 본문

Objective-C

162. (Objective-C/objc) [간단 소스] WKWebview 웹뷰 runJavaScriptTextInputPanelWithPrompt 입력 팝업창 이벤트 감지

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

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

 

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





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

        - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
            
            
            UIAlertController * alert = [UIAlertController
                                          alertControllerWithTitle:@"알림"
                                          message:prompt
                                          preferredStyle:UIAlertControllerStyleAlert];
             
            
            // [텍스트 필드 넣어주기]
            [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                textField.text = defaultText;
                
            }];
            
            UIAlertAction* ok = [UIAlertAction actionWithTitle:@"확인" style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * action) {
                                                        
                completionHandler(alert.textFields.firstObject.text);
            }];
            
            
            UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"취소" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction * action) {
                
                completionHandler(nil);
            }];
             
            // [버튼 넣어주기]
            [alert addAction:ok];
            [alert addAction:cancel];
             
            [self presentViewController:alert animated:YES completion:nil];
            
        }

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

 

반응형
Comments