Develop
2014.01.16 14:39
[ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기
조회 수 14899 댓글 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];
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
717 | Develop | [c] 거리와 각도를 입력받아서 좌표로 변환 | hooni | 2013.04.23 | 10830 |
716 | System/OS | [linux] 프로세스의 stat 상태에 대한 설명 | hooni | 2013.04.23 | 10820 |
715 | Develop | [php] XE 관리자 IP대역 설정 오류 해결법 | hooni | 2014.02.10 | 10815 |
714 | Develop | [c++] 가짜 인증서(하나은행) 프로그램 | hooni | 2013.04.23 | 10769 |
713 | Develop | [c++] p.118 확인학습 5번 | hooni | 2003.04.23 | 10762 |
712 | Develop | [js] jQuery plugin 요약 | hooni | 2013.12.20 | 10744 |
711 | Develop | [c++]현승이가 보내준 암호화 모듈 AES라인델.. | hooni | 2003.04.23 | 10737 |
710 | System/OS | [perl] 영규가 만든 스크립트.. 하하.. | hooni | 2013.04.23 | 10668 |
709 | System/OS | [linux] SSH에 대한 기본 설명과 설치/설정 | hooni | 2013.04.23 | 10640 |
708 | Develop | [php] GregorianToJD(), JDToGregorian() 함수 내용 | hooni | 2013.12.25 | 10629 |
707 | Develop | [php] 날짜 정보 출력 (년,월,일,시,분,초) | hooni | 2013.04.23 | 10562 |
706 | System/OS | [doc] 레드햇 리눅스 메뉴얼 (html버전) | hooni | 2013.04.23 | 10508 |