Contents

조회 수 14861 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
717 Develop [js] 사진첩에 쓸 내용 - 마우스 오버로 바꾸기 hooni 2013.04.23 6341
716 Develop [js] 사라지는 브라우저 hooni 2003.04.23 6857
715 Develop [js] 빈도우즈(bindows96) file hooni 2013.04.23 7371
714 Develop [js] 비만 지수 측정(BMI) ㅎㅎ file hooni 2013.04.23 7616
713 Develop [js] 부모창에서 자식창으로 문자열 전달.. file hooni 2013.04.23 7460
712 Develop [js] 밀리터리 프로그램(전역일 계산) 7 file hooni 2013.04.23 8913
711 Develop [js] 문자열에서 숫자만 걸러내기 (jQuery 안쓰고 정규표현식) hooni 2017.12.14 1111
710 Develop [js] 모바일웹에서 이미지 저장하는거 (context menu) 막기 hooni 2013.04.23 26372
709 Develop [js] 모바일 스크롤 방지(해제) hooni 2015.04.14 1594
708 Develop [js] 동적(innerHTML)으로 자바스크립트 실행하기.. 2 file hooni 2013.04.23 18205
707 Develop [js] 네이버에서 그림 퍼올 때.. URL Encoding 관련ㅋㅋ hooni 2013.04.23 7225
706 Develop [js] 네이버, 다음의 플레이어 스킨 file hooni 2013.04.23 6942
Board Pagination Prev 1 ... 34 35 36 37 38 39 40 41 42 43 ... 98 Next
/ 98