UIWebView

2014. 10. 28. 17:35개발자료/iOS


반응형

● 생성

UIWebView *webView = nil;

webView = [[UIWebView allocinitWithFrame:CGRectMake(00320480)];

webView.delegate = self;

[self.view addSubview:webView];

[webView release];

 

 

● 사이트 호출

NSURL *url = [NSURL URLWithString:@"http://wonyoung2.blog.me"];

[webView loadRequest:[NSURLRequest requestWithURL:url]];

 

 

●  Bounce 효과 제거

for (id subview in webView.subviews){

  if ([[subview classisSubclassOfClass: [UIScrollView class]]){

    ((UIScrollView *)subview).bounces = NO;

  }

}

 

●  POST 방식 호출

NSString *strSendData = [NSString stringWithFormat:@"DATA1=1&DATA2=2"];

NSMutableURLRequest *request = [[NSMutableURLRequest allocinitWithURL:url];

[request setHTTPMethod:@"POST"];

[request setHTTPBody: [strSendData dataUsingEncoding:NSUTF8StringEncoding]];

[webView loadRequest:request];

[request release];


● HTML 소스보기

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    NSString* strHtml = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];

    NSLog(@">>>>>> %s - html:%@", __func__, strHtml);

}


롱프레스 시 선택 방지

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];

}


● 롱프레스 시 옵션 풍선 표시 방지

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];

}


● 호출 URL 

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    NSURLRequest *request = m_webView.request;

    if(request){

        NSURL *url = [request URL];

        NSLog(@">>>>>> %s - url:%@", __func__, [url absoluteString]);

    }    

}

● Cookie 설정

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:

                        [NSDictionary dictionaryWithObjectsAndKeys:

                         @"domain", NSHTTPCookieDomain,

                         @"/", NSHTTPCookiePath,

                         @"CookieName", NSHTTPCookieName,

                         @"CookieValue", NSHTTPCookieValue,

                         [NSDate dateWithTimeIntervalSinceNow:60*60*168], NSHTTPCookieExpires,

                         nil]

                        ];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];


● Cookie 여러개 설정

NSString *urlString = @"";

NSDictionary *cookieProperties1 = [NSDictionary dictionaryWithObjectsAndKeys:

                                   @"domain", NSHTTPCookieDomain,

                                   @"/", NSHTTPCookiePath,

                                   @"CookieName1", NSHTTPCookieName,

                                   @"CookieValue1",NSHTTPCookieValue,nil];

NSDictionary *cookieProperties2 = [NSDictionary dictionaryWithObjectsAndKeys:

                                   @"domain", NSHTTPCookieDomain,

                                   @"/", NSHTTPCookiePath,

                                   @"CookieName2", NSHTTPCookieName,

                                   @"CookieValue2",NSHTTPCookieValue,nil];

NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:cookieProperties1];

NSHTTPCookie *cookie2 = [NSHTTPCookie cookieWithProperties:cookieProperties2];

NSArray* cookieArray = [NSArray arrayWithObjects: cookie1,cookie2, nil];

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookieArray forURL:[NSURL URLWithString:urlString] mainDocumentURL:nil];






반응형

'개발자료 > iOS' 카테고리의 다른 글

NSDate, NSDateFormat  (0) 2014.10.28
nan 값 체크하기  (0) 2014.10.28
UILabel  (0) 2014.10.28
[리젝사유] 2.5: Apps that use non-public APIs will be rejected  (0) 2014.10.28
UIApplicationDelegate Event Function  (0) 2014.10.28