0
My Problem is solved
我在做一個從服務器端獲取cookie的應用程序。我想存儲cookie並在註銷功能中刪除cookie,但我不知道如何執行此操作。 基於這個cookie,我的應用必須向用戶顯示一些額外的好處,所以我想保存這個cookie。 這是登錄請求:有解決方案如何在登錄中存儲cookie並在註銷操作中刪除cookie
- (IBAction)enterButtonAction:(id)sender {
NSString *post =[NSString stringWithFormat:@"user[email]=%@&user[password]=%@",userNameTF.text,passWordTF.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)postData.length];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection != nil) {
NSLog(@"Connection Successfull");
self.responseData = [NSMutableData alloc];
[connection start];
}else {
NSLog(@"Failed");
}
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.responseData setLength:0];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError * parseError = nil;
NSMutableDictionary * _jsonDictionary = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:&parseError];
NSLog(@"%@",_jsonDictionary);
NSString * roleString = [_jsonDictionary objectForKey:@"role"];
if ([roleString isEqualToString:@"user"]) {
UIStoryboard * storyBD = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController * obj = [storyBD instantiateViewControllerWithIdentifier:@"tab"];
[self.navigationController pushViewController:obj animated:YES];
}
else {
[self alertStatus:@"Please Enter 'Correct Email & Password'" :nil];
}
}
////解決方案是這樣的一個,現在是工作非常精細,沒有必要存儲或刪除cookies每一件事情都會處理這個代碼
NSString *post =[NSString stringWithFormat:@"user[email]=%@&user[password]=%@",userNameTF.text,passWordTF.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)postData.length];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:@"myCookie" forHTTPHeaderField:@"Set-cookie"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
@"http://yourwebsite.com", NSHTTPCookieDomain,
@"\\", NSHTTPCookiePath,
@"myCookie", NSHTTPCookieName,
@"1234", NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
NSArray* cookieArray = [NSArray arrayWithObject:cookie];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
[request setAllHTTPHeaderFields:headers];
NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection != nil) {
NSLog(@"Connection Successfull");
self.responseData = [NSMutableData alloc];
[connection start];
}else {
[sharedAppDelegate dismissGlobalHUD];
NSLog(@"Failed");
}