Contents

조회 수 16431 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

보통 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];


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
363 Develop [c] KNN(K-nearest neighbor) 패턴인식 과제.. file hooni 2013.04.23 8557
362 Develop [c] 암호화 알고리즘 DES 구현 ㅋㅋ file hooni 2013.04.23 8556
361 Develop [c] 단기과정[01/07] 제어문, 피보나치수열 hooni 2003.04.23 8555
360 Develop [c] 도메인(호스트)으로 IP정보 알아오기.. (nslookup과 비슷) file hooni 2013.04.23 8553
359 Develop [chm] 윈도우즈에서 디버깅 기법(Debugging Applications) file hooni 2013.04.23 8549
358 Develop [jsp][php] LDAP 프로그래밍.. file hooni 2003.04.23 8536
357 Develop [c] 구조체 배열 예제 (학생 성적 계산) file hooni 2013.04.23 8534
356 Develop [php] 자바스크립트 개판 만들기.. file hooni 2013.04.23 8528
355 Develop [js] 네이버에서 그림 퍼올 때.. URL Encoding 관련ㅋㅋ hooni 2013.04.23 8522
354 Develop [c] 프로그래밍 과제..(colprint) file hooni 2003.04.23 8515
353 Develop [c] 간단한 순위 루틴.. (질문에 대한 답변) hooni 2003.04.23 8495
352 Develop [c] 스택/힙 오버플로우 테스트(overflow) file hooni 2003.04.23 8493
Board Pagination Prev 1 ... 36 37 38 39 40 41 42 43 44 45 ... 71 Next
/ 71