Develop
2014.01.16 14:39
[ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기
조회 수 14908 댓글 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];
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
297 | System/OS | [mac] 맥OSX에서 NTFS 쓰기 기능 활성화 | hooni | 2014.03.12 | 4376 |
296 | System/OS |
Mac에서 Node.js 설치하기
![]() |
hooni | 2018.09.28 | 4343 |
295 | Develop |
[c#]뉴 툴바 개인적으로 만든거.. (old)
![]() |
hooni | 2013.04.23 | 4272 |
294 | Develop |
[ios] 애플 앱스토어 IDFA 리뷰 정책 변경 안내
![]() |
hooni | 2014.05.07 | 4251 |
293 | Develop |
[ios] AES256 알고리즘을 이용해 데이터 암호화/복호화 방법
![]() |
hooni | 2015.07.21 | 4196 |
292 | Develop |
[ios] DatePicker iOS 6.x 이하 디자인.
![]() |
hooni | 2014.04.10 | 4127 |
291 | Develop |
[ios] iOS 7 이상 UIBarButtonItem 여백
![]() |
hooni | 2014.03.27 | 4103 |
290 | Develop | [android] 코드에서 문자열로 Resource 가져오기 | hooni | 2015.07.09 | 4076 |
289 | Develop | [ios] 아이폰 GPS 사용하기 | hooni | 2014.05.24 | 4054 |
288 | Develop | [ios] UDID 사용 제한에 따른 대안들 | hooni | 2014.03.13 | 4023 |
287 | Develop |
[php] 3 Ways to Detect Mobile or Desktop in PHP
![]() |
hooni | 2020.01.28 | 3997 |
286 | Develop |
[ios] iOS 의 인앱구매 소개
![]() |
hooni | 2014.04.29 | 3902 |