UIWebView 클래스를 이용하다보면,
가끔 로딩한 웹뷰 컨텐츠 내에서 어떤 이벤트가 일어나는지 가로채고 싶을 때가 있습니다.
예를 들면, Resource 폴더 내에 내가 원하는 .html 파일을 넣고, 그 폴더에서 Get 방식으로 파라미터를 특정 서버에 전달하는 경우에 정작 사용자가 만든 프로그램 내에서는 이 이벤트가 어떻게 서버로 흘러 들어가는지를 포착하기가 어려운 것이 현실입니다. UIWebView는 로딩만 할뿐, 안에 로딩된 컨텐츠가 어떻게 전달되는지는 미궁이겟죠..
그러나 역시나 이러한 이벤트에 대한 사용 가능한 함수가 있었네요.

이용법은 다음과 같습니다.

@interface WebBrowserTutorialAppDelegate : NSObject <UIWebViewDelegate> {

일단 델리게이트 부분에 웹뷰 델리게이트를 추가해주시고요...

webView.delegate = self;

웹뷰 변수 엔트리 부분에 델리게이트를 원하는 부분에 추가를 합니다.

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(urlString);
return YES;
}
그리고 위에서 처럼,
shouldStartLoadWithRequest 함수를 포함해서 이용하시면 되는데요.
먼저, request라는 변수는 웹뷰에 로드된 컨텐츠에서 어떤 리퀘스트가 있을때 들어오는 변수인데,
다음과 같은 것으로 구성이 되어 있습니다.

* absoluteString – An absolute string for the URL. Creating by resolving the receiver’s string against its base.
* absoluteURL – An absolute URL that refers to the same resource as the receiver.
If the receiver is already absolute, returns self.
* baseURL – The base URL of the receiver. If the receiver is an absolute URL, returns nil.
* host – The host of the URL.
* parameterString – The parameter string of the URL.
* password – The password of the URL (i.e. http://user:pass@www.test.com would return pass)
* path – Returns the path of a URL.
* port – The port number of the URL.
* query – The query string of the URL.
* relativePath – The relative path of the URL without resolving against the base URL.
 If the receiver is an absolute URL, this method returns the same value as path.
* relativeString – string representation of the relative portion of the URL.
 If the receiver is an absolute URL this method returns the same value as absoluteString.
* scheme – The resource specifier of the URL (i.e. http, https, file, ftp, etc).
* user – The user portion of the URL.

많은 것을 알수가 있죠?

금번에 새로 나오게 될 저의 5번째 어플에서는 이와 같이,
원래는 다른 사이트에 GET을 통해 특정 파라미터를 보내 서버의 특정 정해진 사이트로 이동하려는 것을 가로채어서,
 GET 방식의 파라미터만 추출해서 결과 사이트에서 저의 입맛에 맞게 다시 재조정하여 파싱한 사이트만을 보이기 위해 위의 방법을 이용하였습니다.
실재로 생각보다 더 많은 기능이 SDK에 포함이 되어 있습니다.
다소 횡설수설했네요;;;
2009/11/08 22:52 2009/11/08 22:52

Trackback Address >> http://gnamja.com/tc/trackback/173

댓글을 달아 주세요