Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
陈高翔
/
DreamSleep-iOS
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 967ff9ba
由
cgx
编写于
2022-06-24 09:37:08 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
优化cookie同步
1 个父辈
e007f266
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
95 行增加
和
27 行删除
DreamSleep/DreamSleep/Basement/Utils/DSConstUtil.h
DreamSleep/DreamSleep/Basement/Utils/DSConstUtil.m
DreamSleep/DreamSleep/Class/AISleepModule/Controller/AISleepCoachController.m
DreamSleep/DreamSleep/Class/ProfileModule/UserLogin/LoginUtils.m
DreamSleep/DreamSleep/Basement/Utils/DSConstUtil.h
查看文件 @
967ff9b
...
...
@@ -74,6 +74,9 @@ FOUNDATION_EXTERN NSString * const SelectUpdateTime;
/// 远程服务器主机地址
FOUNDATION_EXTERN
NSString
*
const
R_HOST_URL
;
/// cookie
FOUNDATION_EXTERN
NSString
*
const
DsAppCookie
;
/// 常用常量值
@interface
DSConstUtil
:
NSObject
@end
...
...
DreamSleep/DreamSleep/Basement/Utils/DSConstUtil.m
查看文件 @
967ff9b
...
...
@@ -50,5 +50,7 @@ NSString * const SelectUpdateTime = @"SelectUpdateTime";
NSString
*
const
R_HOST_URL
=
@"R_HOST_URL"
;
NSString
*
const
DsAppCookie
=
@"DsAppCookie"
;
@implementation
DSConstUtil
@end
DreamSleep/DreamSleep/Class/AISleepModule/Controller/AISleepCoachController.m
查看文件 @
967ff9b
...
...
@@ -7,12 +7,11 @@
#import "AISleepCoachController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import "RelaxTrainController.h"
#import "SomeProxy.h"
#import "UnityGameController.h"
#import "UserRequestModel.h"
#import "DsMaskView.h"
#import "UserRequestModel.h"
#import "RelaxTrainController.h"
#import "UnityGameController.h"
@interface
AISleepCoachController
()
<
WKNavigationDelegate
,
WKScriptMessageHandler
,
DsWebControllerDelegate
,
RelaxTrainControllerDelegate
>
@property
(
strong
,
nonatomic
)
WKWebView
*
aiWebView
;
...
...
@@ -49,28 +48,6 @@
[
self
.
dsMaskView
display
];
}
#pragma mark - 登录成功后刷新
-
(
void
)
needUpdateAiCoach
{
NSHTTPCookie
*
sidCookie
=
nil
;
for
(
NSHTTPCookie
*
cookie
in
[
NSHTTPCookieStorage
sharedHTTPCookieStorage
].
cookies
)
{
// connect.sid是后台下发的cookie字段
if
([
cookie
.
name
isEqualToString
:
@"connect.sid"
])
{
sidCookie
=
cookie
;
break
;;
}
}
// 将APP获取的cookie同步给wkwebview容器里面的cookie(wkwebview存在cookie不同步的巨坑,该同步API在iOS 11及以后支持)
[
self
.
aiWebView
.
configuration
.
websiteDataStore
.
httpCookieStore
setCookie
:
sidCookie
completionHandler
:
^
{
[
self
.
aiWebView
loadRequest
:
self
.
request
];
}];
}
-
(
void
)
startOpen
{
if
(
!
[
LoginUtils
getUserLoginData
])
{
[
LoginUtils
jumpToLoginControllerWithTarget
:
self
];
}
}
#pragma mark - DsWebControllerDelegate && RelaxTrainControllerDelegate
-
(
void
)
reloadAIPage
{
[
self
.
aiWebView
reload
];
...
...
@@ -131,7 +108,9 @@
switch
(
type
)
{
case
1
:
// 未登录点击开启
{
[
self
startOpen
];
if
(
!
[
LoginUtils
getUserLoginData
])
{
[
LoginUtils
jumpToLoginControllerWithTarget
:
self
];
}
}
break
;
case
2
:
// 登录点击开启-开启我的页面里面的AI睡眠教练
...
...
@@ -183,6 +162,74 @@
}
}
#pragma mark - 方法1:同步cookie(登录成功后设置,iOS 11及以上)
-
(
void
)
needUpdateAiCoach
{
// 设置cookie
[[
NSHTTPCookieStorage
sharedHTTPCookieStorage
]
setCookieAcceptPolicy
:
NSHTTPCookieAcceptPolicyAlways
];
// 获取cookie信息
NSDictionary
*
properties
=
kGetUserDefaultsObj
(
DsAppCookie
);
NSMutableDictionary
*
muProperties
=
[
NSMutableDictionary
dictionaryWithDictionary
:
properties
];
NSURL
*
url
=
[
NSURL
URLWithString
:
ServerURL
];
NSString
*
IP
=
url
.
host
;
[
muProperties
setValue
:
IP
forKey
:
@"Domain"
];
NSHTTPCookie
*
cookie
=
[
NSHTTPCookie
cookieWithProperties
:
muProperties
];
[[
NSHTTPCookieStorage
sharedHTTPCookieStorage
]
setCookie
:
cookie
];
// 同步cookie
WKHTTPCookieStore
*
cookieStrore
=
self
.
aiWebView
.
configuration
.
websiteDataStore
.
httpCookieStore
;
NSArray
*
cookies
=
[[
NSHTTPCookieStorage
sharedHTTPCookieStorage
]
cookies
];
for
(
NSHTTPCookie
*
cookie
in
cookies
)
{
if
([
cookie
.
domain
isEqualToString
:
IP
])
{
[
cookieStrore
setCookie
:
cookie
completionHandler
:
^
{
[
self
.
aiWebView
loadRequest
:
self
.
request
];
}];
}
}
// 该方法在部分设备失效
// NSHTTPCookie *sidCookie = nil;
// for (NSHTTPCookie *cookie in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) {
// // connect.sid是后台下发的cookie字段
// if ([cookie.name isEqualToString:@"connect.sid"]) {
// sidCookie = cookie;
// break;;
// }
// }
// // 将APP获取的cookie同步给wkwebview容器里面的cookie(wkwebview存在cookie不同步的巨坑,该同步API在iOS 11及以后支持)
// [self.aiWebView.configuration.websiteDataStore.httpCookieStore setCookie:sidCookie completionHandler:^{
// [self.aiWebView loadRequest:self.request];
// }];
}
#pragma mark - 方法2:通过js注入cookie(推荐)
-
(
WKUserScript
*
)
injectCookieScript
{
WKUserScript
*
userScript
=
[[
WKUserScript
alloc
]
initWithSource
:[
self
cookieString
]
injectionTime
:
WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly
:
NO
];
return
userScript
;
}
-
(
NSString
*
)
cookieString
{
[[
NSHTTPCookieStorage
sharedHTTPCookieStorage
]
setCookieAcceptPolicy
:
NSHTTPCookieAcceptPolicyAlways
];
// 获取cookie信息
NSDictionary
*
properties
=
kGetUserDefaultsObj
(
DsAppCookie
);
NSMutableDictionary
*
muProperties
=
[
NSMutableDictionary
dictionaryWithDictionary
:
properties
];
NSURL
*
url
=
[
NSURL
URLWithString
:
ServerURL
];
NSString
*
IP
=
url
.
host
;
[
muProperties
setValue
:
IP
forKey
:
@"Domain"
];
NSHTTPCookie
*
cookie
=
[
NSHTTPCookie
cookieWithProperties
:
muProperties
];
[[
NSHTTPCookieStorage
sharedHTTPCookieStorage
]
setCookie
:
cookie
];
NSMutableString
*
script
=
[
NSMutableString
string
];
[
script
appendString
:
@"var cookieNames = document.cookie.split('; ').map(function(cookie) { return cookie.split('=')[0] } );
\n
"
];
for
(
NSHTTPCookie
*
cookie
in
[[
NSHTTPCookieStorage
sharedHTTPCookieStorage
]
cookies
])
{
if
([
cookie
.
value
rangeOfString
:
@"'"
].
location
!=
NSNotFound
)
{
continue
;
}
if
([
cookie
.
domain
isEqualToString
:
IP
])
{
NSString
*
string
=
[
NSString
stringWithFormat
:
@"%@=%@;domain=%@;path=%@"
,
cookie
.
name
,
cookie
.
value
,
cookie
.
domain
,
cookie
.
path
?:
@"/"
];
[
script
appendFormat
:
@"if (cookieNames.indexOf('%@') == -1) {document.cookie='%@'; };
\n
"
,
cookie
.
name
,
string
];
}
}
return
script
;
}
#pragma mark - lazy
-
(
WKWebView
*
)
aiWebView
{
if
(
!
_aiWebView
)
{
...
...
DreamSleep/DreamSleep/Class/ProfileModule/UserLogin/LoginUtils.m
查看文件 @
967ff9b
...
...
@@ -116,10 +116,26 @@
}
+
(
void
)
updateDataNotiAfterLoginSuccess
{
[
self
saveCookie
];
// 刷新主页数据
[[
NSNotificationCenter
defaultCenter
]
postNotificationName
:
NeedUpdateHomePage
object
:
nil
];
// 刷新AI睡眠教练
[[
NSNotificationCenter
defaultCenter
]
postNotificationName
:
NeedUpdateAICoach
object
:
nil
];
}
+
(
void
)
saveCookie
{
// 存储Cookie
NSArray
*
cookies
=
[[
NSHTTPCookieStorage
sharedHTTPCookieStorage
]
cookies
];
NSURL
*
url
=
[
NSURL
URLWithString
:
ServerURL
];
NSString
*
IP
=
url
.
host
;
for
(
NSHTTPCookie
*
tempCookie
in
cookies
)
{
if
([
tempCookie
.
domain
isEqualToString
:
IP
])
{
DSLog
(
@"tempCookie:%@"
,
tempCookie
);
kSetUserDefaultsObj
(
tempCookie
.
properties
,
DsAppCookie
);
kUserDefaultsSynchronize
;
}
}
}
@end
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论