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];
}

 

人已赞赏
iOS文章

iOS-NSString汉子转拼音

2019-11-1 14:19:18

iOS文章

iOS APP 架构图

2019-11-2 7:03:46

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索