조회 수 16431 추천 수 0 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
569 Develop [c] 프로그래밍 ppt, 스킬업 (비트 수업자료) file hooni 2003.04.23 9442
568 Develop [jsp] HelloServlet 출력문 file hooni 2003.04.23 9439
567 Develop [c] 마우스 따라다니는 고양이 - 네코95 (WinAPI) file hooni 2013.04.23 9432
566 Develop [c] 베지어 곡선(Bézier curve) 알고리즘 file hooni 2013.04.23 9415
565 Develop [c++] WinSock2.0 채팅 프로그램 ㅋㅋ file hooni 2013.04.23 9406
564 Develop [mysql] 양력, 음력 DB데이터 file hooni 2013.04.23 9399
563 Develop [c] OpenGL 관측점 이동 hooni 2003.04.23 9385
562 Develop [c] 프로그래밍의 전반적인 설명 ppt file hooni 2003.04.23 9371
561 Develop [java] 메모패드.. 스윙(swing)으로.. file hooni 2013.04.23 9369
560 Develop [c++] 압축프로그램(Lite Zip) 샘플 file hooni 2013.04.23 9357
559 Develop [c] 콘솔에서 패스워드 입력시 문자 보이지 않게 하는 코드 file hooni 2013.04.23 9352
558 Develop [js] 자바스크립트로 만든 게임 file hooni 2013.04.23 9342
557 Develop [java] 채팅 프로그램.. swing 사용.. file hooni 2013.04.23 9338
556 Develop [c] 가위 바위 보 서버, 클라이언트 소스코드 file hooni 2003.04.23 9328
555 Develop [c] 유닉스 프로그램에서 인수처리 해주는 getopt() 함수 file hooni 2013.04.23 9313
554 Develop [c] 구조체의 설명과 예제.. hooni 2003.04.23 9308
Board Pagination Prev 1 ... 37 38 39 40 41 ... 74 Next
/ 74