Я проверяю, существует ли пользователь в моей базе данных, поэтому я отправляю fbid и имя пользователя на сервер PHP и проверяю свою базу данных, чтобы узнать, существует ли этот пользователь. Однако я продолжаю получать NULL, когда получаю возвращенный массив, который должен содержать пользователя.
в моей консоли это говорит:
2014-10-18 13:53:17.385 KandiTAG[1206:60b] checkUser has been called.
2014-10-18 13:53:17.453 KandiTAG[1206:60b] mutableData: <>
Код:
#pragma mark - NSURLConnection Delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[mutableData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[mutableData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
//if we get any connection error manage it here
//for example use alert view to say no internet connection
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableArray *data = [NSJSONSerialization JSONObjectWithData:mutableData options:kNilOptions error:nil];
NSLog(@"mutableData: %@", mutableData);
NSLog(@"response JSON: %@", data);
}
-(void)checkUser {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
FBid = [defaults stringForKey:@"FBID"];
userName = [defaults stringForKey:@"NAME"];
usr = [NSString stringWithFormat:@"user"];
pw = [NSString stringWithFormat:@"password"];
NSLog(@"checkUser has been called");
NSString *requestString = [NSString stringWithFormat:@"user=%@&pw=%@&fbid=%@&username=%@", usr, pw, FBid, userName];
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"example.com/login.php?"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/..." forHTTPHeaderField:@"content-type"];
[request setHTTPBody:requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
mutableData = [[NSMutableData alloc] init];
}
}
@interface yourVC : UIViewController<NSURLConnectionDelegate>
{
NSMutableData *_responseData;
}NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
FBid = [defaults stringForKey:@"FBID"];
userName = [defaults stringForKey:@"NAME"];
usr = [NSString stringWithFormat:@"user"];
pw = [NSString stringWithFormat:@"password"];NSString *requestString = [NSString stringWithFormat:@"user=%@&pw=%@&fbid=%@&username=%@", usr, pw, FBid, userName];
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"example.com/login.php?"]];// check this directly in a browser to see if it even returns data
// are you missing the http:// in your actual example?
// make sure the server responds to a GET for this
NSLog(@"%@%@",@"http://example.com/login.php?",requestString);[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"%@", _responseData);
}
Других решений пока нет …