用WKWebView向cookie或者localStroge传值的坑

一,首先介绍一下在mac上用safari调试ios手机的移动端页面

第一步:打开iphone找到以下页面,流程是:【设置】->【Safari】->【高级】->

开启【Web检查器】 ,如图1、图2



第二步:打开Mac上Safari,流程是【选中Safari】->【偏好设置】->【高级】->【在菜单栏中显示“开发”菜单】勾选,如图3,4


第三步:用数据线将iphone和mac连接起来,在手机app上打开一个web网页,在电脑的safari中按照流程执行:【开发】->【手机名称】->【正在调试的网站】如图5,6






第四步:点击上图的192.168.100.116-rechargelist web页面,出来当前页面的资源,时间线,存储空间等。如图5,6


二,正式说今天遇到的问题,方法一json存不进去,方法二可以存进去

方法一:

let data = try JSONSerialization.data(withJSONObject: ["id":GlobalData.shared.logInModel.userId, "phone":GlobalData.shared.logInModel.mobile], options: .prettyPrinted)

let userJson = String(data:data, encoding: .utf8) ?? "{}"

let script = WKUserScript(source:"localStorage.setItem('wrongUserJson','\(userJson)') ;", injectionTime: .atDocumentStart, forMainFrameOnly:false)

configuration.userContentController.addUserScript(script)

userJson = "{\n  \"id\" : \"376AA08A44F613A5960BBA9708DE6E057318626B4961F35A6632E2F9140A33E60031043B73978C1344010d16\",\n  \"phone\" : \"18616655261\"\n}"

之所以userJson串是这种带\n的格式,是因为JSONSerialization.data(withJSONObject: ["id":GlobalData.shared.logInModel.userId, "phone":GlobalData.shared.logInModel.mobile], options: .prettyPrinted)中的options是NSJSONWritingPrettyPrinted的原因,苹果官网资料是这样说的“为了使打印出来的json串格式标准一些,添加了一些空格和\n符号”,这样的json字符串存不进去cookie和localStroge。

方法二:

let data = try JSONSerialization.data(withJSONObject:["id":GlobalData.shared.logInModel.userId ,"phone":GlobalData.shared.logInModel.mobile], options:JSONSerialization.WritingOptions(rawValue:0))

let userJson = String(data:data, encoding: .utf8) ?? "{}"

let script = WKUserScript(source:"localStorage.setItem('rightUserJson','\(userJson)') ;", injectionTime: .atDocumentStart, forMainFrameOnly:false)

configuration.userContentController.addUserScript(script)

userJson =  "{\"id\":\"376AA08A44F613A5960BBA9708DE6E057318626B4961F35A6632E2F9140A33E60031043B73978C1344010d16\",\"phone\":\"18616655261\"}"

这种格式的json字符串能存进去,如下图

fileprivate func creatUserCookie() -> HTTPCookie? {

let data = try JSONSerialization.data(withJSONObject: ["id":GlobalData.shared.logInModel.userId, "phone":GlobalData.shared.logInModel.mobile], options:JSONSerialization.WritingOptions(rawValue:0))

let userJson = String(data: data, encoding: .utf8) ??  "{}"

let properties: [HTTPCookiePropertyKey:Any] = [

            HTTPCookiePropertyKey.name: "rightUserJson",

            HTTPCookiePropertyKey.value: userJson,

            HTTPCookiePropertyKey.domain: "https://www.baidu.com",

            HTTPCookiePropertyKey.path: "/"

        ]

        returnHTTPCookie(properties: properties)

    }

if let userCookie = creatUserCookie(), let cookieStr = HTTPCookie.requestHeaderFields(with: [userCookie])["Cookie"] {

            // 设置页面请求的cookie

            request.addValue(cookieStr, forHTTPHeaderField:"Cookie")

            let cookieScript = WKUserScript(source:"document.cookie = '\(cookieStr)' ;", injectionTime: .atDocumentStart, forMainFrameOnly:false)

            configuration.userContentController.addUserScript(cookieScript)

        }

json字符串存cookie,也成功存进去,如下图


写得比较匆忙,如有错误请指正,谢谢。

你可能感兴趣的:(用WKWebView向cookie或者localStroge传值的坑)