Swift, WKWebView에서 alert창 띄우기

2016. 7. 2. 18:21개발/iOS

코멘토에서

UIWebView로 되어 있는 부분을 WKWebView로 바꿔보는 중이다.

몇가지 막혔던 부분들을 하나씩 적어볼 예정이다.


Xcode 7.3, iOS 9




첫 난관은 아니었지만, 

(첫 난관은 아예 화면이 안뜨는 부분이었다. 이 부분은 viewDidLoad와 관련이 있었다.)

script로 되어있는 alert창이 안뜨는 상황이 발생하였다.


WKWebView에서는 WKUIDelegate에 이 부분을 처리하는 부분이 있다.

실제로 구현된 부분은 다음과 같다.


class ViewController: UIViewController, WKUIDelegate { ...


self.webView!.UIDelegate = self


func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String,

             initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {

    

    let alertController = UIAlertController(title: message, message: nil, preferredStyle: UIAlertControllerStyle.Alert);

    

    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel) {

        _ in completionHandler()}

    );

    

    self.presentViewController(alertController, animated: true, completion: nil);

}




이렇게 했더니 이번엔 다른 문제가 있었다.

실제 사이트에서는 alert창을 띄운 이후에 focus를 적용하고 있는데,

키보드가 움찔거리다 만다. (...)


그 부분은 마지막에 실제로 alert를 띄우는 부분을

self가 아닌 rootViewController를 이용했다.

다음과 같다.


//self.presentViewController(alertController, animated: true, completion: nil);


let rootViewController: UIViewController = (UIApplication.sharedApplication().windows.last?.rootViewController)!

rootViewController.presentViewController(alertController, animated: true, completion: nil);




여기에 한가지 더 문제가 있다면 

이제 해당 위치로 focus는 되는데 해당 위치로 스크롤링은 안된다. (...)

이 부분은 추후에 수정하기로 하고 잠시 멈췄다.