Skip to content

Instantly share code, notes, and snippets.

@hite
Created June 12, 2018 09:15
Show Gist options
  • Save hite/97e9e3d60c39ce4aac647a77534d4ebf to your computer and use it in GitHub Desktop.
Save hite/97e9e3d60c39ce4aac647a77534d4ebf to your computer and use it in GitHub Desktop.
AppHostCookie 源码
@implementation AppHostCookie
+ (NSMutableArray<NSString *> *)cookieJavaScriptArray
{
NSMutableArray<NSString *> *cookieStrings = [[NSMutableArray alloc] init];
//取出cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookieStorage.cookies) {
//
NSString *excuteJSString = [NSString stringWithFormat:@"%@=%@;", cookie.name, cookie.value];
if (cookie.domain.length > 0) {
excuteJSString = [excuteJSString stringByAppendingFormat:@"domain=%@;", cookie.domain];
}
if (cookie.path.length > 0) {
excuteJSString = [excuteJSString stringByAppendingFormat:@"path=%@;", cookie.path];
}
if (cookie.expiresDate != nil) {
excuteJSString = [excuteJSString stringByAppendingFormat:@"expires=%@;", cookie.expiresDate];
}
if (cookie.secure) {
excuteJSString = [excuteJSString stringByAppendingString:@"secure=true"];
}
[cookieStrings addObject:excuteJSString];
DDLogDebug(@"[JSBridge] cookie to be set = %@", excuteJSString);
}
// 检查cookie的数量,正常的应该是 7 个
DDLogInfo(@"[JSBridge] cookieJavaScriptArray. cookie.length = %ld", cookieStrings.count);
//执行js
return cookieStrings;
}
static WKProcessPool *_sharedManager = nil;
// 当从未登录到登录时,这个状态需要置为 No
static BOOL kLoginCookieHasBeenSynced = NO;
+ (WKProcessPool *)sharedPoolManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedManager = [[WKProcessPool alloc] init];
// 监听 urs 登出的事件。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLogout:) name:kLogoutNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUserLoginSucss:) name:kLoginSuccessNotification object:nil];
});
return _sharedManager;
}
+ (void)didLogout:(id)notif
{
// 清空旧的 progress
DDLogInfo(@"[JSBridge] didLogout, old pool is destoryed");
_sharedManager = [[WKProcessPool alloc] init];
}
#pragma mark - cookie sync part
+ (void)didUserLoginSucss:(id)notif
{
kLoginCookieHasBeenSynced = NO;
}
+ (void)setLoginCookieHasBeenSynced:(BOOL)synced
{
kLoginCookieHasBeenSynced = synced;
}
+ (BOOL)loginCookieHasBeenSynced
{
return kLoginCookieHasBeenSynced;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment