조회 수 14859 추천 수 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
번호 분류 제목 글쓴이 날짜 조회 수
25 Develop [ios] Swift 4 Dictionary 사용하기 file hooni 2018.11.29 2027
24 Develop [ios] Locale Identifiers file hooni 2018.11.29 1625
23 Develop [Javascript][Ajax] 자바스크립트 강의 산출물 file hooni 2019.10.05 702
22 Develop 링크들 보고 지울 내용 secret hooni 2019.11.21 0
21 Develop [python] 파라미터 앞에 *, ** 의 의미? (*args, **kwargs) hooni 2019.11.22 1553
20 Develop [python][django] request.cookie 읽어오기 ㅋㅋㅋ (쓰기) hooni 2019.12.06 1688
19 Develop 자주 쓰는 Docker 명령어 alias hooni 2020.01.10 269442
18 Develop [php] 3 Ways to Detect Mobile or Desktop in PHP file hooni 2020.01.28 3722
17 Develop [ios] Start developing your navigation app for CarPlay without enrollment file hooni 2020.02.22 124756
16 Develop [sh] html 안에 있는 img 다운 받는 쉘 스크립트 file hooni 2020.05.26 638
15 Develop [sh] 쉘스크립트 if 비교 연산 hooni 2020.05.26 59295
14 Develop [js] Text 값을 클립보드에 복사하기 hooni 2020.10.10 677
13 Develop [ubuntu] 우분투 18.04에 PHP5 설치하기 hooni 2020.11.14 941
12 Develop [kotlin] 코틀린 안드로이드 앱 버전/빌드 정보 hooni 2020.12.15 819
11 Develop [swift] NotificationCenter 간단 예제 file hooni 2021.01.27 8112
10 Develop [swift] popToRoot 모달뷰, 네비게이션컨트롤러 한꺼번에 닫기 file hooni 2021.01.29 1383
Board Pagination Prev 1 ... 49 50 51 52 53 Next
/ 53