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

 

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!
技术狗 » UIWebView实现自动登陆(存储、读取Cookie)