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];
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
697 | Develop | [ios] 앱의 로컬 js 파일에서 해당 프로젝트의 이미지 불러오기 | hooni | 2015.02.10 | 631 |
696 | Develop | [ios] 문자열로 함수 실행하기 (eval 함수처럼) | hooni | 2015.02.10 | 860 |
695 | Develop | [ios] URL 랜딩 속도(OpenURL 10초 정지되는) 이슈 | hooni | 2015.02.09 | 855 |
694 | Develop | [js] 2048 예쁘게 만들고 있는거.. ㅋㄷ | hooni | 2015.01.30 | 0 |
693 | Develop | [php] XE 스킨에서 특정 도메인 리다이렉션 | hooni | 2015.01.28 | 583 |
692 | Develop | [php] XE에서 도메인 별로 광고 다르게 적용하기 | hooni | 2015.01.28 | 620 |
691 | Develop | [maven] Mac OS에 메이븐(maven) 설치하기 | hooni | 2015.01.21 | 1112 |
690 | Develop | [ios] iOS 앱 아이콘을 만드는 유틸 | hooni | 2015.01.03 | 875 |
689 | Develop | [ios] Xcode의 디버그 모드에서 콜스택 | hooni | 2015.01.03 | 1006 |
688 | Develop | [ios] binary를 C코드로 변환 | hooni | 2015.01.03 | 1533 |
687 | Develop | [ios] APNS에 사용할 인증서 만들기 (KeyChain에 있는 인증서 Export) | hooni | 2015.01.03 | 900 |
686 | Develop | [ios] iOS앱의 Xcode 빌드 과정 | hooni | 2015.01.03 | 2282 |