cookie的失效问题,由服务端控制,而服务端同事告知,目前cookie的过期时间尚不确定。
测试登录时,发现iOS 系统,在杀掉APP进程后,网页的cookie就失效了,致使每次重启APP后,在需要登录的页面,都需要重新登录方可使用。
而Android不存在该问题。
那么,如何解决这个问题呢?
基于APP开发的方案中,我们一般都会在登录成功的时候,存储cookie,其他网络请求时,携带该cookie。那么,我们是不是同样可以在登录成功的回调中,获取cookie,并存储呢?
尝试了百度到的方法,使用document.cookie,打印信息为空,咨询前端同事,告知除了这个办法,他也无法在请求成功的回调中,获取到响应头中的cookie。
那么,借助插件的形式呢?测试,OK!问题解决。
插件的原生代码如下:
// HHCookieHandle.h
// TCM
//
// Created by 晗 on 2018/9/10.
// Copyright © 2018年 晗. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HHCookieHandle : NSObject
+ (NSArray *)saveCookieWithUrlStr:(NSString *)urlStr;
+ (NSArray *)setCookie;
+ (void)removeCookie;
@end
//
// HHCookieHandle.m
// TCM
//
// Created by 晗 on 2018/9/10.
// Copyright © 2018年 晗. All rights reserved.
//
#import "HHCookieHandle.h"
#define HHUserDefaultCookie @"userDefaultCookie"
@implementation HHCookieHandle
+ (NSArray *)saveCookieWithUrlStr:(NSString *)urlStr{
//保存cookie
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:urlStr]];
for (NSHTTPCookie *tempCookie in cookies) {
NSLog(@"手机登录时获取的cookie: %@",tempCookie);
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cookies];
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:data forKey:HHUserDefaultCookie];
[userDefault synchronize];
return cookies;
}
+ (NSArray *)setCookie{
NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:HHUserDefaultCookie];
NSArray *cookies;
if(cookiesdata && cookiesdata.length > 0) {
NSLog(@"手机登录获取的cookie存在");
cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
NSHTTPCookie *cookie;
for (cookie in cookies) {
NSLog(@"%@",cookie);
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}
return cookies;
}
+ (void)removeCookie{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:HHUserDefaultCookie];
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in storage.cookies) {
[storage deleteCookie:cookie];
}
}
@end
// HHCookiePlugin.h
// TCM
//
// Created by 晗 on 2018/9/10.
// Copyright © 2018年 晗. All rights reserved.
//
#import "HHBasePlugin.h"
#import "PGMethod.h"
@interface HHCookiePlugin : HHBasePlugin
- (void)saveCookie:(PGMethod *)command;
- (void)setCookie:(PGMethod *)command;
- (void)removeCookie:(PGMethod *)command;
@end
#import "HHCookiePlugin.h"
#import "HHCookieHandle.h"
@implementation HHCookiePlugin
- (void)saveCookie:(PGMethod *)command{
if(command){
NSString *urlStr = command.arguments[1];
NSLog(@"手机登录 ==== >%@",urlStr);
NSArray *cookies = [HHCookieHandle saveCookieWithUrlStr:urlStr];
[self handleResultWithValue:cookies command:command];
}
}
- (void)setCookie:(PGMethod *)command{
NSArray *cookies = [HHCookieHandle setCookie];
[self handleResultWithValue:cookies command:command];
}
- (void)removeCookie:(PGMethod *)command{
[HHCookieHandle removeCookie];
[self handleResultWithValue:nil command:command];
}
@end
在程序入口的方法中,调用
[HHCookieHandle setCookie];
在登录页面的js中调用plus.cookiePlugin.saveCookie方法