Il mio previous question riguardava il problema che dovevo accedere ogni volta per fare servizi web come pubblicare un link o caricare un'immagine. Philipe ha risposto che devo utilizzare i cookie al posto della procedura di accesso per ogni richiesta. Ho trovato questo metodo per ottenere i cookie:Come ottenere i cookie e usarli per altre richieste come POST (iOS)?
- (void)getCookies {
NSHTTPURLResponse * response;
NSError * error;
NSMutableURLRequest *request;
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://MyWebsite.com/login.php"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:120];
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://MyWebsite.com/login.php"]];
NSLog(@"%d", all.count);
for (NSHTTPCookie *cookie in all) {
NSLog(@"Name: %@ : Value: %@", cookie.name, cookie.value);
NSLog(@"Comment: %@ : CommentURL: %@", cookie.comment, cookie.commentURL);
NSLog(@"Domain: %@ : ExpiresDate: %@", cookie.domain, cookie.expiresDate);
NSLog(@"isHTTPOnly: %c : isSecure: %c", cookie.isHTTPOnly, cookie.isSecure);
NSLog(@"isSessionOnly: %c : path: %@", cookie.isSessionOnly, cookie.path);
NSLog(@"portList: %@ : properties: %@", cookie.portList, cookie.properties);
NSLog(@"version: %u", cookie.version);
}
}
ho trovato anche questo codice per utilizzare questi biscotti, ma non sono sicuro di come usarlo:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookies];
Ecco il mio metodo per la pubblicazione, sto usando RestKit API:
- (IBAction)addLinkPressed:(UIButton *)sender {
[RKClient clientWithBaseURLString:@"http://MyWebsite.com"];
NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
self.linkField.text, @"url",
self.linkTitleField.text, @"title",
self.linkSummaryField.text, @"summary",
nil];
RKRequest *request = [[RKClient sharedClient] post:@"/send_link.php" params:params delegate:self];
[request setUserData:@"sendLink"];
}
domanda: quale proprietà del cookie devo conservare di usarlo per le informazioni di login e dove dovrei mettere nel mio codice?
Quindi nessuna soluzione ancora? –
@FadyKamal Inserisco la mia metodologia, spero che sia utile per voi. – Hamid