본문 바로가기

옛글/아이폰 프로그래밍

[iOS프로그래밍]Webview Javascript&URL Catch하기

반응형

최근 하이브리드 앱이 많아지면서, 웹앱을 앱으로 하이브리드하게 제어하면서 필요한 부분들이 JavaScript를 앱내에서 웹뷰로 제어하는 방법을 많이 공부해야 겠다는 생각이 듭니다. 


- (void)viewDidLoad

{

    UIwebView.delegate = self;

    NSString *fullURL = @"연결할 URL"

    NSURL *url = [NSURL URLWithString:fullURL]; 

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [UIwebView loadRequest:requestObj];

    [super viewDidLoad];

}


//특정 Request 캐치하기

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString *requestStr = [[request URL] absoluteString];  

    NSLog(@"request is %@", requestStr);

    

    //요청 URL 캐치

    if([requestStr isEqualToString:@"특정URL(요청될)"])

    {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"테스트" message:@"테스트 중입니다." delegate:self cancelButtonTitle:@"확인" otherButtonTitles:nil, nil];

        [alertView show];

        [alertView release];

        return NO; //NO = JavaScript 중단

    }

    //요청 JavaScript 캐치

    if ( [UIwebView stringByEvaluatingJavaScriptFromString:@"특정자바스크립트"] ) { //원하는 자바스크립트 입력

        //동작하고자 하는 명령

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"테스트" message:@"테스트 중입니다." delegate:self cancelButtonTitle:@"확인" otherButtonTitles:nil, nil];

        [alertView show];

        [alertView release];

        return YES;

        //NO 로 return 한 후 원하는 행동 구현        

    }


    return YES; //YES = 그대로 진행

}



좀 더 공부를 해서 웹뷰를 제어하는 API 를 만들어볼까도 고민중입니다. 


반응형