溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS Cookie相關(guān)操作

發(fā)布時(shí)間:2020-07-12 00:37:05 來源:網(wǎng)絡(luò) 閱讀:389 作者:xx541100047 欄目:移動(dòng)開發(fā)
注:CookieData是我的自定義對象,繼承SQLitePersistentObject
@interface CookieData : SQLitePersistentObject

@property(nonatomic,retain)NSString *mname;
@property(nonatomic,retain)NSString *mvalue;
@property(nonatomic,retain)NSString *mdomain;
@property(nonatomic,retain)NSString *mpath;

@end

#pragma -#pragma mark cookie相關(guān)
/**
 *     @brief    保存cookie信息
 *    
 *    登陸成功后保存
 */
+(void)saveCookie{
   for(CookieData *_d in [CookieData allObjects]){
        [_d deleteObject];
    }
   NSHTTPCookieStorage *sharedHTTPCookie=[NSHTTPCookieStorage sharedHTTPCookieStorage];
   NSArray *_cookies=[sharedHTTPCookie cookies];
   for(int i=0; i<[_cookies count]; i++) {
       NSHTTPCookie *_cookie=[_cookies objectAtIndex:i];
       CookieData *_cookieData=[[CookieData alloc]init];
        _cookieData.mname=[_cookie name];
        _cookieData.mvalue=[_cookie value];
        _cookieData.mpath=[_cookie path];
        _cookieData.mdomain=[_cookie domain];
        [_cookieData save];
        [_cookieData release];
    }
}
/**
 *     @brief    恢復(fù)cookie信息
 *    
 *    請求weblogid時(shí)恢復(fù)
 */
+(void)recoverCookie{
     NSHTTPCookieStorage *sharedHTTPCookie=[NSHTTPCookieStorage sharedHTTPCookieStorage];
     NSArray *_cookiesDB=[CookieData allObjects];
     for(int i=0; i<[_cookiesDB count]; i++) {
          CookieData* _cookieData=[_cookiesDB objectAtIndex:i];
          //Create a cookie
          NSDictionary *properties = [[[NSMutableDictionary alloc] init] autorelease];
          [properties setValue:_cookieData.mvalue forKey:NSHTTPCookieValue];
          [properties setValue:_cookieData.mname forKey:NSHTTPCookieName];
          [properties setValue:_cookieData.mdomain forKey:NSHTTPCookieDomain];
          [properties setValue:[NSDatedateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];
          [properties setValue:_cookieData.mpath forKey:NSHTTPCookiePath];
          NSHTTPCookie*cookie = [[[NSHTTPCookie alloc] initWithProperties:properties] autorelease];
          [sharedHTTPCookie setCookie:cookie];
     }
}

iOS Cookie相關(guān)操作

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI