Develop
2014.01.16 14:39
[ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기
조회 수 14861 댓글 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];
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
897 | Develop | [ios] 아이폰에서 진동(Vibrate) 기능 추가하기 | hooni | 2014.04.18 | 3470 |
896 | System/OS | [android] 초간단 HTTP, POST 전송 샘플 1 | hooni | 2017.02.16 | 3490 |
895 | Develop | [js] jQuery 셀 병합 1 | hooni | 2014.09.23 | 3501 |
894 | Etc | 영어. 불규칙 동사 정리 | hooni | 2017.10.04 | 3524 |
893 | Develop | [ios] iOS에서 디바이스 종류 알아오기 | hooni | 2014.05.24 | 3657 |
892 | Develop | [ios] 유용한 매크로 | hooni | 2014.03.26 | 3658 |
891 | Develop | [ios] UIWebView 캐쉬 삭제 | hooni | 2014.04.08 | 3670 |
890 | Develop | [ios] URL 파라미터 파싱~ | hooni | 2014.05.12 | 3682 |
889 | Develop | [ios] SQLite 사용하기(튜토리얼) + 샘플코드 | hooni | 2014.03.28 | 3705 |
888 | Develop | [php] 3 Ways to Detect Mobile or Desktop in PHP | hooni | 2020.01.28 | 3730 |
887 | Develop | [ios] iOS 의 인앱구매 소개 | hooni | 2014.04.29 | 3834 |
886 | Develop | [android] 코드에서 문자열로 Resource 가져오기 | hooni | 2015.07.09 | 3948 |