![](/img/trans.png)
[英]is there any way to send different badge count to different users in a same topic?
[英]NSHTTPCookieStorage for same URL but different users
可能這是一個愚蠢的問題,但是我想為相同的URL存儲cookie,但為用戶名存儲不同的cookie。 如何使用NSHTTPCookieStorage來實現? 這就是我從響應中存儲Cookie的方式。
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *headerFields = [httpResponse allHeaderFields];
NSArray* cookies = [NSHTTPCookie
cookiesWithResponseHeaderFields:headerFields
forURL:[NSURL URLWithString:@""]];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
NSString *urlString = ...;
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:[NSURL URLWithString:urlString] mainDocumentURL:nil];
如果我對您的理解正確,那么您希望為不同的用戶提供不同的Cookie集。 如果是這樣,那么您應該:
創建您自己的自定義cookie存儲類(可能只是一個字典,以基本URL為鍵,並以cookie數組作為值),並在其中存儲cookie,除了使用sharedHTTPCookieStorage
對象。
更改用戶時,請擦除共享cookie的存儲並從新用戶的cookie jar中復制cookie(或有選擇地從共享jar中刪除該用戶創建的每個cookie)。
如果我誤解了您,並且出於某種原因您想在共享cookie存儲之外存儲一個特定的cookie:
基於一個配置為nil cookie jar的NSURLSession對象(禁用自動cookie存儲)。
像上面一樣將cookie存儲到cookie罐中(可能是共享cookie罐,也可能不是共享cookie罐),但是要避免存儲它們。
編寫一種方法,將Cookie從該Cookie罐添加到NSURLRequest對象。
在使用它們創建任務之前,請始終記住在URL請求對象上調用該方法。
當然,所有這些都假定為NSURLSession。 我認為沒有辦法用NSURLConnection控制Cookie的存儲。 所有cookie都存儲在cookie罐,句號,AFAIK中。
您可以使用sharedCookieStorage(forGroupContainerIdentifier:)
為同一個URL設置單獨的Cookie存儲(即多個用戶)。
您可以使用該方法獲得通用的cookie存儲,並在URLSession
使用它。
NSHTTPCookieStorage用於相同的URL但不同的用戶
let configuration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
configuration.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: UUID().uuidString)
configuration.httpCookieStorage?.cookieAcceptPolicy = .always
let sessionForCurrentUser = URLSession(configuration: configuration)
let task = sessionForCurrentUser.dataTask(with: URL(string: "https://foo.com/feature")!)
// ...
因此,如果要支持多個用戶,則為每個用戶創建一個公共組標識符。
如果應用程序以訪客用戶身份啟動,那么當用戶登錄時,您需要“升級” Cookie存儲。類似:
// Upgrade from Guest user to Paid user
let guestCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: guestId)
let userCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: user.uniqueIdentifier)
for cookie in guestCookieStorage.cookies ?? [] {
userCookieStorage.setCookie(cookie)
}
@interface NSHTTPCookieStorage (ApplePrivateHeader)
-(id)_initWithIdentifier:(id)arg1 private:(BOOL)arg2;
@end
@interface User()
@property (nonatomic, strong) NSHTTPCookieStorage *myHTTPCookieStorage;
@end
@implementation User
-(instancetype)init
{
self = [super init];
if (self) {
//the [NSHTTPCookieStorage init] will generate a instance with no inner `NSHTTPCookieStorageInternal`, so.
_myHTTPCookieStorage = [[NSHTTPCookieStorage alloc] _initWithIdentifier: @"user_id_xxx" private:0];
_myHTTPCookieStorage.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;
}
return self;
}
-(void)httpTask{
NSURLSessionConfiguration *c =[NSURLSessionConfiguration ephemeralSessionConfiguration];
c.HTTPCookieStorage = _myHTTPCookieStorage;
[[[NSURLSession sessionWithConfiguration: c] dataTaskWithRequest:request completionHandler:nil] resume];
}
// load from file
-(void)(id)initWithCoder:(NSCoder *)aDecoder{
NSArray<NSHTTPCookie*> *cookies = [aDecoder decodeObjectForKey:@"cookies"];
for (NSHTTPCookie *cookie in cookies) {
[_myHTTPCookieStorage setCookie: cookie];
}
}
//save to file
- (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:_myHTTPCookieStorage.cookies forKey: @"cookies"];
}
@end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.