Git Product home page Git Product logo

ggwkcookie's Introduction

GGWkCookie

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

WKWebView管理cookie是很麻烦事,经常出现 App自定义cookie的值丢失 或 更新不及时 的情况。 通过长期踩坑,总结出WKWebview管理cookie的一种方案。

原理: WKWebview支持的插入脚本的方式,在每次页面渲染前,通过插入的Js脚本检测Cookie是否存在,如不存在,将cookie重新种入的思路。

注意:因为考虑避免子域名和根域名cookie重复出现,该方法所有的Cookie值将种在 根域名上。

Installation

GGWkCookie is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'GGWkCookie'

Use

1.导入 引用头文件:

#import "GGWkCookie.h"

#import <GGWkCookie/GGWkCookie.h>

2.实现 <GGWkWebViewDelegate> 协议 ,开启cookie开关 startCustomCookie():

 // 设置cookie代理
 webView.cookieDelegate = self;
 
 // 开启自定义cookie(在loadRequest前开启)
 [webView startCustomCookie];

/// 代理方法中设置 app自定义的cookie
- (NSDictionary *)webviewSetAppCookieKeyAndValue {

    return @{
             @"cookieName":@"value",
             };
}

3.如果代理中的cookie发生改变,调用 reloadCookie() 更新cookie, - (NSDictionary *)webviewSetAppCookieKeyAndValue代理方法将获取最新的cookie值。

总结: 所以,我们只需要维护好代理方法- (NSDictionary *)webviewSetAppCookieKeyAndValue的返回值, 在需要更新的时调用 reloadCookie() 方法即可。

博客地址:https://www.jianshu.com/p/43a1e87a91d5

Author

gaoguohao, [email protected]

License

GGWkCookie is available under the MIT license. See the LICENSE file for more info.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.