조회 수 16430 추천 수 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
번호 분류 제목 글쓴이 날짜 조회 수
1177 Develop '2014 모바일 개발 트렌드' 발표자료입니다. file hooni 2014.10.02 2533
1176 System/OS Apache CORS 설정 1 hooni 2020.09.04 6129
1175 Develop Aspect Oriented Programming in Objective-C hooni 2015.05.18 3251
1174 System/OS asx미디어 정보 기록.. hooni 2003.04.23 22154
1173 System/OS Brave Browser for Debian, Ubuntu, Mint file hooni 2024.10.23 6939
1172 System/OS CentOS 6.5 USB 설치 6 file hooni 2013.12.18 39980
1171 System/OS CentOS 에서 Cacti 설치하기 hooni 2015.01.02 3180
1170 System/OS Configure Postfix to Use Gmail SMTP on Ubuntu 18.04 file hooni 2020.02.07 12161
1169 Develop DDay Memo 1.9.4 소스코드 secret hooni 2015.10.03 0
1168 Etc EBS [수학영역] 미적분과 통계 기본 - 정규분포의 의미와 특징은? hooni 2015.04.20 2664
1167 System/OS Enable Safari Hidden Debug Menu in Mac OS X file hooni 2017.02.07 7558
1166 System/OS Enable the Develop Menu in Safari file hooni 2017.02.07 4041
1165 Develop GCM 사용하기 2 (단말에 GCM 구현하기) file hooni 2013.07.06 24652
1164 Develop GCM 사용하기 3 (JSP로 GCM 푸시 서버 만들기) 4 file hooni 2013.07.06 27129
1163 Develop git 브런치 배우기 (링크) hooni 2013.07.09 22078
1162 Develop GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스 file hooni 2014.10.14 2060
Board Pagination Prev 1 2 3 4 5 ... 74 Next
/ 74