31期_iOS_WKWebView的cookie处理
2023-09-02 本文已影响0人
萧修
WK的cookie的应用
NSDictionary *properties = @{
NSHTTPCookieName : @"name",
NSHTTPCookieValue : @"zhangsan",
NSHTTPCookieDomain : @"域名",
NSHTTPCookiePath : @"/",
NSHTTPCookieExpires:[NSDate dateWithTimeIntervalSinceNow:3600],
NSHTTPCookieSecure : @(YES)
};
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
[NSHTTPCookieStorage.sharedHTTPCookieStorage setCookie:cookie];
[NSHTTPCookieStorage.sharedHTTPCookieStorage deleteCookie:cookie];//删除okie
-
NSHTTPCookieName
:表示cookie的名字,通常标识客户端请求中的cookie,也用于后续客户端和服务器交互时访问该cookie -
NSHTTPCookieValue
:表示cookie的值,通常是服务器分配给客户端唯一标识符,用于表示客户端身份 -
NSHTTPCookieDomain
:表示cookie的域名,可以设置为服务器的主机名或域名,也可以为其子域名,当客户端请求该域名或者子域名的网页时,cookie会随之传给服务端 -
NSHTTPCookiePath
:表示cookie所属路径,通常设置服务器处理请求的路径,比如来自/login
的请求,可以设置路径为此 -
NSHTTPCookieExpires
:cookie过期时间 -
NSHTTPCookieSecure
:表示cookie是否采用加密,为yes为https,no为http
WK设置cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:@{};
WKWebsiteDataStore *websiteDataStore = config.websiteDataStore;
WKHTTPCookieStore *httpCookieStore = websiteDataStore.httpCookieStore;
[httpCookieStore setCookie:nil completionHandler:^{
}];
- WKHTTPCookieStore
仅支持iOS11之后
WK_EXTERN API_AVAILABLE(macos(10.13), ios(11.0))
@interface WKHTTPCookieStore : NSObject
- (instancetype)init NS_UNAVAILABLE;
/*! @abstract Fetches all stored cookies.
@param completionHandler A block to invoke with the fetched cookies.
*/
- (void)getAllCookies:(void (^)(NSArray<NSHTTPCookie *> *))completionHandler;
/*! @abstract Set a cookie.
@param cookie The cookie to set.
@param completionHandler A block to invoke once the cookie has been stored.
*/
- (void)setCookie:(NSHTTPCookie *)cookie completionHandler:(nullable void (^)(void))completionHandler;
/*! @abstract Delete the specified cookie.
@param completionHandler A block to invoke once the cookie has been deleted.
*/
- (void)deleteCookie:(NSHTTPCookie *)cookie completionHandler:(nullable void (^)(void))completionHandler WK_SWIFT_ASYNC_NAME(deleteCookie(_:));
/*! @abstract Adds a WKHTTPCookieStoreObserver object with the cookie store.
@param observer The observer object to add.
@discussion The observer is not retained by the receiver. It is your responsibility
to unregister the observer before it becomes invalid.
*/
- (void)addObserver:(id<WKHTTPCookieStoreObserver>)observer;
/*! @abstract Removes a WKHTTPCookieStoreObserver object from the cookie store.
@param observer The observer to remove.
*/
- (void)removeObserver:(id<WKHTTPCookieStoreObserver>)observer;
@end
案例
H5需要用到登录信息,原生需要将此传递给h5,我们通过js bridge方法传递。
从wk代理方法中decidePolicyForNavigationAction
请求开始之前处理cookie,没有则添加,添加通过usercontentviewcontroller或者wkusersript添加。
- cookie添加
- (void)setCookieWithUrl:(NSURL *)url {
NSString *host = [url host];
if ([self.cookieURLs containsObject:host]) {
return;
}
[self.cookieURLs addObject:host];
WKUserScript *wkcookieScript = [[WKUserScript alloc] initWithSource:self.webviewCookie
injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:NO];
[self.cookieWebview.configuration.userContentController addUserScript:wkcookieScript];
NSString *baseWebUrl = [NSString stringWithFormat:@"%@://%@", url.scheme, url.host];
[self.cookieWebview loadHTMLString:@"" baseURL:[NSURL URLWithString:baseWebUrl]];
}
- cookie删除
- (void)removeWKWebviewCookie {
self.webviewCookie = nil;
[self.cookieWebview.configuration.userContentController removeAllUserScripts];
NSMutableArray<NSHTTPCookie *> *cookies = [NSMutableArray array];
[[NSHTTPCookieStorage sharedHTTPCookieStorage].cookies enumerateObjectsUsingBlock:^(NSHTTPCookie * _Nonnull cookie, NSUInteger idx, BOOL * _Nonnull stop) {
if ([self.cookieData.allKeys containsObject:cookie.name]) {
[cookies addObjectOrNil:cookie];
}
}];
[cookies enumerateObjectsUsingBlock:^(NSHTTPCookie * _Nonnull cookie, NSUInteger idx, BOOL * _Nonnull stop) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}];
}