Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
陈高翔
/
DreamSleep-iOS
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit 6167454c
由
cgx
编写于
2022-05-29 12:58:40 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
解决wkwebview cookie不同步巨坑
1 个父辈
f44682f1
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
48 行增加
和
39 行删除
DreamSleep/DreamSleep/Class/AISleepModule/AISleepCoachController.m
DreamSleep/DreamSleep/Class/HomeModule/Home/Controller/HomeViewController.m
DreamSleep/DreamSleep/Class/AISleepModule/AISleepCoachController.m
查看文件 @
6167454
...
...
@@ -8,11 +8,10 @@
#import "AISleepCoachController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import "WKWebView+Extras.h"
@interface
AISleepCoachController
()
<
WKNavigationDelegate
,
WKScriptMessageHandler
,
WKUIDelegate
>
@property
(
strong
,
nonatomic
)
WKWebView
*
aiWebView
;
@property
(
strong
,
nonatomic
)
NSURLRequest
*
request
;
@property
(
strong
,
nonatomic
)
NS
Mutable
URLRequest
*
request
;
@property
(
strong
,
nonatomic
)
UIProgressView
*
progressView
;
@property
(
nonatomic
,
strong
)
ExceptionDefaultView
*
exceptionView
;
@end
...
...
@@ -63,14 +62,18 @@
#pragma mark - 登录成功后刷新
-
(
void
)
needUpdateAiCoach
{
[
WKWebView
deleteWebCache
];
// 延迟的时间
int64_t
delayInSeconds
=
3
.
0
;
[[
UIApplication
sharedApplication
]
setNetworkActivityIndicatorVisible
:
YES
];
dispatch_after
(
dispatch_time
(
DISPATCH_TIME_NOW
,
(
int64_t
)(
delayInSeconds
*
NSEC_PER_SEC
)),
dispatch_get_main_queue
(),
^
{
[
self
.
aiWebView
reload
];
});
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
{
...
...
@@ -109,6 +112,7 @@
DSLog
(
@"加载失败:%@"
,
error
);
}
#pragma mark - WKScriptMessageHandler
-
(
void
)
userContentController
:
(
WKUserContentController
*
)
userContentController
didReceiveScriptMessage
:
(
WKScriptMessage
*
)
message
{
if
([
message
.
name
isEqualToString
:
@"AppModel"
])
{
NSDictionary
*
bodyDic
=
message
.
body
;
...
...
@@ -117,34 +121,18 @@
int
type
=
[
bodyDic
[
@"type"
]
intValue
];
if
(
type
==
1
)
{
// 未登录点击开启
[
self
startOpen
];
// 注入js代码用于强制刷新小梦睡眠
NSString
*
jsCallBack
=
@"window.parent.location.reload()"
;
[
self
.
aiWebView
evaluateJavaScript
:
jsCallBack
completionHandler
:
^
(
id
_Nullable
result
,
NSError
*
_Nullable
error
)
{
[
self
.
aiWebView
loadRequest
:
self
.
request
];
DSLog
(
@"注入成功..."
);
if
(
error
)
{
DSLog
(
@"err is %@"
,
error
.
domain
);
}
}];
}
}
// if ([message.name isEqualToString:@"xxx"]) {
// NSDictionary *jsData = message.body;
// NSLog(@"%@", message.name);
//读取js function的字符串
// NSString *jsFunctionString = jsData[@"result"];
// //拼接调用该方法的js字符串(convertDictionaryToJson:方法将NSDictionary转成JSON格式的字符串)
// NSString *jsonString = [NSDictionary convertDictionaryToJson:@{@"test":@"123", @"data":@"666"}];
// NSString *jsCallBack = [NSString stringWithFormat:@"(%@)(%@);", jsFunctionString, jsonString];
// //执行回调
// [self.weWebView evaluateJavaScript:jsCallBack completionHandler:^(id _Nullable result, NSError * _Nullable error) {
// if (error) {
// NSLog(@"err is %@", error.domain);
// }
// }];
// }
}
// js调用alert
-
(
void
)
webView
:
(
WKWebView
*
)
webView
runJavaScriptAlertPanelWithMessage
:
(
NSString
*
)
message
initiatedByFrame
:
(
WKFrameInfo
*
)
frame
completionHandler
:
(
void
(
^
)(
void
))
completionHandler
{
UIAlertController
*
alert
=
[
UIAlertController
alertControllerWithTitle
:
@"alert"
message
:
@"JS调用alert"
preferredStyle
:
UIAlertControllerStyleAlert
];
[
alert
addAction
:[
UIAlertAction
actionWithTitle
:
@"确定"
style
:
UIAlertActionStyleDefault
handler
:
^
(
UIAlertAction
*
_Nonnull
action
)
{
completionHandler
();
}]];
[
self
presentViewController
:
alert
animated
:
YES
completion
:
NULL
];
DSLog
(
@"message:%@"
,
message
);
}
#pragma mark - lazy
...
...
@@ -168,7 +156,6 @@
_aiWebView
.
backgroundColor
=
DSWhite
;
_aiWebView
.
UIDelegate
=
self
;
_aiWebView
.
hidden
=
YES
;
[
_aiWebView
debugViewShowBorder
];
_aiWebView
.
scrollView
.
bounces
=
NO
;
[
_aiWebView
addObserver
:
self
forKeyPath
:
@"estimatedProgress"
options
:
NSKeyValueObservingOptionNew
context
:
nil
];
_aiWebView
.
navigationDelegate
=
self
;
...
...
@@ -176,7 +163,7 @@
_aiWebView
.
scrollView
.
showsHorizontalScrollIndicator
=
NO
;
// 解决页面顶部出现白色问题
_aiWebView
.
scrollView
.
contentInsetAdjustmentBehavior
=
UIScrollViewContentInsetAdjustmentNever
;
self
.
request
=
[
NS
URLRequest
requestWithURL
:[
NSURL
URLWithString
:
AICoachURL
]
];
self
.
request
=
[
NS
MutableURLRequest
requestWithURL
:[
NSURL
URLWithString
:
AICoachURL
]
cachePolicy
:
NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval
:
10
.
0
];
[
_aiWebView
loadRequest
:
self
.
request
];
}
return
_aiWebView
;
...
...
DreamSleep/DreamSleep/Class/HomeModule/Home/Controller/HomeViewController.m
查看文件 @
6167454
...
...
@@ -35,7 +35,10 @@
UIButton
*
dailyTaskBtn
=
[
UIButton
new
];
[
dailyTaskBtn
addTarget
:
self
action
:
@selector
(
jumpToDailyTaskPage
)
forControlEvents
:
UIControlEventTouchUpInside
];
[
dailyTaskBtn
dk_setImage
:
DKImagePickerWithNames
(
@"ic_home_meirirw"
,
@"dk_ic_home_meirirw"
,
@"ic_home_meirirw"
)
forState
:
UIControlStateNormal
];
self
.
navigationItem
.
rightBarButtonItem
=
[[
UIBarButtonItem
alloc
]
initWithCustomView
:
dailyTaskBtn
];;
self
.
navigationItem
.
rightBarButtonItem
=
[[
UIBarButtonItem
alloc
]
initWithCustomView
:
dailyTaskBtn
];
dispatch_after
(
dispatch_time
(
DISPATCH_TIME_NOW
,
(
int64_t
)(
3
.
0
*
NSEC_PER_SEC
)),
dispatch_get_main_queue
(),
^
{
[
self
addScaleAnimtaionWithView
:
dailyTaskBtn
];
});
[
self
.
view
addSubview
:
self
.
homeTV
];
[
self
.
view
addSubview
:
self
.
noisePlayBar
];
...
...
@@ -63,6 +66,25 @@
[[
NSNotificationCenter
defaultCenter
]
addObserver
:
self
selector
:
@selector
(
needUpdate
)
name
:
NeedUpdateHomePage
object
:
nil
];
}
#pragma mark - 先放大,再缩小
-
(
void
)
addScaleAnimtaionWithView
:
(
UIView
*
)
view
{
// 放大效果,并回到原位
CABasicAnimation
*
animation
=
[
CABasicAnimation
animationWithKeyPath
:
@"transform.scale"
];
// 速度控制函数,控制动画运行的节奏
animation
.
timingFunction
=
[
CAMediaTimingFunction
functionWithName
:
kCAMediaTimingFunctionEaseInEaseOut
];
// 执行时间
animation
.
duration
=
0
.
2
;
// 执行次数
animation
.
repeatCount
=
1
;
// 完成动画后会回到执行动画之前的状态
animation
.
autoreverses
=
YES
;
// 初始伸缩倍数
animation
.
fromValue
=
[
NSNumber
numberWithFloat
:
0
.
9
];
// 结束伸缩倍数
animation
.
toValue
=
[
NSNumber
numberWithFloat
:
1
.
1
];
[
view
.
layer
addAnimation
:
animation
forKey
:
nil
];
}
-
(
void
)
viewDidDisappear
:
(
BOOL
)
animated
{
[
super
viewDidDisappear
:
animated
];
...
...
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论