iOS Target을 11.0으로 올린 뒤 UIWebView에서 WKWebView 변환 작업을 했다.
자동로그인일 경우, setCookie를 해주어야하는데... 생각했던 방법으로 정상적으로 작동을 안했다.
일단, WKWebView를 여러개를 사용하기때문에 WKWebViewConfiguration는 AppDelegate에서 초기화를 해주고 동일한 configuration을 사용하였다.
AppDelegate.m (didFinishLaunchingWithOptions 메소드)
self.wkWebViewConfiguration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *wkUserContentController = [[WKUserContentController alloc] init];
WKProcessPool *wkProcessPool = [[WKProcessPool alloc] init];
WKPreferences *wkPreferences = [[WKPreferences alloc] init];
wkPreferences.javaScriptEnabled = YES;
wkPreferences.javaScriptCanOpenWindowsAutomatically = YES;
self.wkWebViewConfiguration.userContentController = wkUserContentController;
self.wkWebViewConfiguration.processPool = wkProcessPool;
self.wkWebViewConfiguration.preferences = wkPreferences;
초기화한 configuration은 self.appDelegate.wkWebViewConfiguration 꺼내 사용했다.
(참고로, 여러개의 WKWebView에서 쿠키값을 공유하려면 같은 WKProcessPool을 사용해야한다.)
1. setCookie
[self.webView.configuration.websiteDataStore.httpCookieStore setCookie:cookie];
2. evaluateJavaScript
[self.webView evaluateJavaScript:[NSString stringWithFormat:@"document.cookie='%@=%@';domain='%@';path='%@';document.cookie='%@=%@';domain='%@';path='%@';", cookie.name, cookie.value, cookie.domain, cookie.path, cookie2.name, cookie2.value, cookie2.domain, cookie2.path] completionHandler:nil];
3. WKUserScript : A WKUserScript object represents a script that can be injected into a webpage.
(https://developer.apple.com/documentation/webkit/wkuserscript)
WKUserScript * cookieScript = [[WKUserScript alloc]
initWithSource: [NSString stringWithFormat:@"document.cookie='%@=%@';domain='%@';path='%@';document.cookie='%@=%@';domain='%@';path='%@';", cookie.name, cookie.value, cookie.domain, cookie.path, cookie2.name, cookie2.value, cookie2.domain, cookie2.path]
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[self.appDelegate.wkWebViewConfiguration.userContentController addUserScript:cookieScript];
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) configuration:self.appDelegate.wkWebViewConfiguration];
[self.webView evaluateJavaScript:[NSString stringWithFormat:@"document.cookie='%@=%@';domain='%@';path='%@';document.cookie='%@=%@';domain='%@';path='%@';", cookie.name, cookie.value, cookie.domain, cookie.path, cookie2.name, cookie2.value, cookie2.domain, cookie2.path] completionHandler:nil];
3가지 모두 정상적으로 작동을 안하였다.
(3번 WKUserScript은 쿠키값은 적용되나 로그아웃했을 경우 계속 해당 쿠키를 가지고 있어 로그인이 작동하였다.)
4. WKWebsiteDataStore.nonPersistentDataStore (작동 O)
: Returns a new nonpersistent data store.
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
@"domain", NSHTTPCookieDomain,
@"/", NSHTTPCookiePath,
@"name", NSHTTPCookieName,
@"value", NSHTTPCookieValue, nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
NSDictionary *properties2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"domain", NSHTTPCookieDomain,
@"/", NSHTTPCookiePath,
@"name2", NSHTTPCookieName,
@"value2", NSHTTPCookieValue, nil];
NSHTTPCookie *cookie2 = [NSHTTPCookie cookieWithProperties:properties2];
WKWebsiteDataStore *wkWebsiteDataStore = WKWebsiteDataStore.nonPersistentDataStore;
dispatch_async(dispatch_get_main_queue(), ^{
[wkWebsiteDataStore.httpCookieStore setCookie:cookie completionHandler:^{
[wkWebsiteDataStore.httpCookieStore setCookie:cookie2 completionHandler:^{
self.appDelegate.wkWebViewConfiguration.websiteDataStore = wkWebsiteDataStore;
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) configuration:self.appDelegate.wkWebViewConfiguration];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.webView setBackgroundColor:UIColor.whiteColor];
[self.webView setOpaque:NO];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
[self.view addSubview:self.webView];
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL: [NSURL URLWithString:@"url"]]];
}];
}];
});
참고 url :
https://developer.apple.com/documentation/webkit/wkwebview
https://developer.apple.com/documentation/webkit?language=objc
https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie
https://stackoverflow.com/questions/50974353/wkwebview-setting-cookie-not-possible-ios-11
'iOS' 카테고리의 다른 글
[iOS] AVCaptureSession를 이용한 Barcode Scanner (0) | 2020.01.06 |
---|---|
[iOS] iOS 13.0 DarkMode Check (Widget 포함) (0) | 2019.08.30 |
[iOS] 푸시 인증서_서버 전달 (0) | 2019.07.31 |
[Xcode] iOS 13 Simulator dark mode 변경 (0) | 2019.07.18 |
[iOS] Git에 ssh key 적용 (0) | 2019.07.04 |