UIWebView实现自动登陆(存储、读取Cookie)

热门标签

,

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

1.在AppDelegate中键入这几句代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSURL *cookieHost = [NSURLURLWithString:@"http://appweb.cosfund.com"];
    NSDictionary *propertiesDict = [NSDictionarydictionaryWithObjectsAndKeys:[cookieHosthost],NSHTTPCookieDomain,[cookieHostpath],NSHTTPCookiePath,@"COOKIE_NAME",NSHTTPCookieName,@"COOKIE_VALUE",NSHTTPCookieValue,nil];
    NSHTTPCookie *cookie = [NSHTTPCookiecookieWithProperties:propertiesDict];
    [[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookie:cookie];
 
    return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
    [[NSHTTPCookieStoragesharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
}

3.退出登陆时删除Cookie

- (void)LoggedOutBtnClick:(UIButton *)button
{
    //清空Cookie
    NSHTTPCookieStorage *myCookie = [NSHTTPCookieStoragesharedHTTPCookieStorage];
    for (NSHTTPCookie *cookiein [myCookie cookies])
    {
        [[NSHTTPCookieStoragesharedHTTPCookieStorage] deleteCookie:cookie];
    }
    //删除沙盒自动生成的Cookies.binarycookies文件
    NSString *path = NSHomeDirectory();
    NSString *filePath = [pathstringByAppendingPathComponent:@"/Library/Cookies/Cookies.binarycookies"];
    NSFileManager *manager = [NSFileManagerdefaultManager];
    [manager removeItemAtPath:filePath error:nil];
}

 

标签:

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《UIWebView实现自动登陆(存储、读取Cookie)》 发布于2019-11-01

分享到:
赞(0) 打赏

评论 抢沙发

3 + 8 =


UIWebView实现自动登陆(存储、读取Cookie)

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?