Develop
2014.01.16 14:39
[ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기
조회 수 14855 댓글 0
보통 UIWebView 에서 특정 URL로 이동할 때 아래와 같은 코드를 사용한다.
NSURLRequest를 이용한 URL 이동
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame]; NSURL *url = [NSURL URLWithString:@"https://www.hooni.net"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request]; [webView setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:webView];
쿠키(Cookie)와 같은 헤더에 값을 실어서 보낼 때는 Request에 값을 세팅해야 하므로,
아래 코드와 같이 NSMutableURLRequest를 사용한다.
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame]; NSURL *url = [NSURL URLWithString:@"https://www.hooni.net"]; NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url]; NSLog(@"%@", @"PersisteWebCookie"); NSMutableString *cookieStringToSet = [[NSMutableString alloc] init]; [cookieStringToSet appendFormat:@"ENC=%@;", @"6EA4E3F7E03A9..."]; if (cookieStringToSet.length) { [mutableRequest setValue:cookieStringToSet forHTTPHeaderField:@"Cookie"]; NSLog(@"Cookie : %@", cookieStringToSet); } NSLog(@"%@", @"PersisteWebCookie Restored"); [webView loadRequest:mutableRequest]; [webView setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:webView];
기존에 저장된 쿠키를 읽어오는 코드와 섞으면 아래처럼 활용할 수 있다.
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame]; NSURL *url = [NSURL URLWithString:@"https://www.hooni.net"]; NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url]; NSLog(@"%@", @"PersisteWebCookie"); NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@"MySavedCookies"]; if([cookiesdata length]) { NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata]; for (cookie in cookies) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; NSLog(@"cookiesdata ; %@", cookie); } NSMutableString *cookieStringToSet = [[NSMutableString alloc] init]; for (NSHTTPCookie *cookie in cookies) { [cookieStringToSet appendFormat:@"%@=%@;", cookie.name, cookie.value]; } if (cookieStringToSet.length) { [mutableRequest setValue:cookieStringToSet forHTTPHeaderField:@"Cookie"]; NSLog(@"Cookie : %@", cookieStringToSet); } } NSLog(@"%@", @"PersisteWebCookie Restored"); [webView loadRequest:mutableRequest]; [webView setBackgroundColor:[UIColor clearColor]]; [self.view addSubview:webView];
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
685 | Develop | [c++] 헤더, 소스 파일 분리해서 작성해본 테스트 소스 | hooni | 2013.04.23 | 6688 |
684 | Develop | [c++] 현승이가 준 P2P 프로그램 소스 ㅋㅋ | hooni | 2013.04.23 | 12061 |
683 | Develop | [c++] 현승이가 준 메신저 소스.. ㅋㅋ | hooni | 2013.04.23 | 8264 |
682 | Develop | [c++][mfc] 파일 입출력 샘플 (한줄씩 읽어서 다른 파일에 쓰기) | hooni | 2013.04.23 | 14979 |
681 | Develop | [c++]현승이가 보내준 동영상 암호화 자료.. 볼것.. | hooni | 2003.04.23 | 9728 |
680 | Develop | [c++]현승이가 보내준 암호화 모듈 AES라인델.. | hooni | 2003.04.23 | 10714 |
679 | Develop | [c/c++] Makefile 사용하기.. ㅋㅋ | hooni | 2003.04.23 | 7321 |
678 | Develop | [chm] C++ 문법 가이드 | hooni | 2013.04.23 | 7942 |
677 | Develop | [chm] Programming Applications for Microsoft Windows | hooni | 2013.04.23 | 7265 |
676 | Develop | [chm] 비주얼 C++ 팁 모음 문서 | hooni | 2013.04.23 | 7500 |
675 | Develop | [chm] 윈도우즈에서 디버깅 기법(Debugging Applications) | hooni | 2013.04.23 | 7527 |
674 | Develop | [coding] Find all anagrams in a string | hooni | 2017.06.27 | 1134 |