Answer the question
In order to leave comments, you need to log in
How to properly store and retrieve a token during authorization in an ios application?
After authorization in the application, I receive a token in response, how to save it correctly and use it for further requests in the future? Here is the code:
- (IBAction)start:(id)sender{
NSString *loging = loginf.text;
NSString *passg = passwordf.text;
NSString *getreq = @"url/log_in.php?login=";
getreq = [getreq stringByAppendingString:loging];
getreq = [getreq stringByAppendingString:@"&password="];
getreq = [getreq stringByAppendingString:passg];
// создаем запрос
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:getreq]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
// создаём соединение и начинаем загрузку
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
// соединение началось
lableconnect.text = @"Connecting...";
// создаем NSMutableData, чтобы сохранить полученные данные
receivedData = [[NSMutableData data] retain];
} else {
// при попытке соединиться произошла ошибка
lableconnect.text = @"Connection error!";
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// получен ответ от сервера
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// добавляем новые данные к receivedData
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error {
// освобождаем соединение и полученные данные
[connection release];
[receivedData release];
// выводим сообщение об ошибке
NSString *errorString = [[NSString alloc] initWithFormat:@"Connection failed! Error - %@ %@ %@",
[error localizedDescription],
[error description],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];
lableconnect.text = errorString;
[errorString release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
lableconnect.text = dataString;
if ([dataString isEqual: @"{\"error\":[\"incorrect user\"]}"]) {
lableconnect.text = @"Wrong login/pass";
}
[connection release];
[receivedData release];
[dataString release];
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question