본문 바로가기

Programming/iOS

WKWebView 설정하기

반응형

iOS8이상 버전부터 지원

기존의 스크립트 해석 속도 향상등 UIWebView보다 개선사항이 많아 짐


https://www.youtube.com/watch?v=ZC_rNie61IQ


아래와 같이 설정


- WebKit.framework 추가 후 Optional 설정

  (Deplyment Target이 8.0 일 경우, 그 이상 사용하는 경우는 Required로 해도 무방)




- 소스 적용

MainViewController.h

#import <WebKit/WebKit.h>

@interface MainViewController ()<WKUIDelegate, WKNavigationDelegate>


@end


MainViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];


    WKWebView  *mainWebView = [[WKWebView alloc] initWithFrame:self.view.frame];

    mainWebView.UIDelegate = self;

    mainWebView.navigationDelegate = self;

    mainWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;


    [self.view addSubview:mainWebView];



    NSURL *url = [NSURL URLWithString:@"http://www.naver.com"];

    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];

    

    [mainWebView loadRequest:mutableRequest];

}


WKUIDelegate

#pragma mark - WKWebView UIDelegate

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {


    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:message preferredStyle:UIAlertControllerStyleAlert];

    

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        completionHandler();

    }]];

    [self presentViewController:alertController animated:YES completion:nil];

}


- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {


    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:message preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        completionHandler(YES);

    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        completionHandler(NO);

    }]];

    

    [self presentViewController:alertController animated:YES completion:nil];

}


- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler {

    

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.text = defaultText;

    }];

    

    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSString *input = ((UITextField *)alertController.textFields.firstObject).text;

        completionHandler(input);

    }]];

    

    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        completionHandler(nil);

    }]];

    

    [self presentViewController:alertController animated:YES completion:nil];

}


WKNavigationDelegate 

#pragma mark - WKWebView WKNavigationDelegate

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation

{

    NSLog(@"start");

}


- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation

{

    NSLog(@"finish Navigation");

}


- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error

{

    NSLog(@"fail Navigation");

}


- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler

{

    NSString *requestString = navigationAction.request.URL.absoluteString;

    

     if ([requestString hasPrefix:@"http://www.facebook.com"]){

        NSURL *rurl = [NSURL URLWithString:requestString];

        [[UIApplication sharedApplicationopenURL:rurl];

        

        decisionHandler(WKNavigationActionPolicyCancel);    // cancel the navigation

        return;

     }

    

    decisionHandler(WKNavigationActionPolicyAllow);    // allow the navigation

}


반응형

'Programming > iOS' 카테고리의 다른 글

HTTPS Server Trust Evaluation  (0) 2016.01.05
프로그램 실행시 파라메터/환경변수 설정 방법  (0) 2015.12.04
Fuse(UX tool suite)  (0) 2015.12.01
UIWebView에 Cookie 실어 보내기  (0) 2015.12.01
맥북에서 Fiddler 사용하기  (0) 2015.11.30