Return string from NSURLRequest

Can someone help me get the string returned from NSURLRequest in the iPhone app? I need to send some user credentials to the url and it will return the client identification number. This is what I need to pull into a string, I'm not used to working with servers or HTTP requests, so any help would be great. I already read Apple Docs about it and I lost this part a bit.

+6
source share
1 answer

This is the standard asynchronous template loader:

In the .h file:

NSMutableData *responseData; 

And the .m file:

 -(void) execute { NSString *urlString = @"http://www.google.com"; responseData = [[NSMutableData data] retain]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ [responseData setLength:0]; } -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [responseData appendData:data]; } -(void) connectionDidFinishLoading:(NSURLConnection *)connection{ [connection release]; NSString *data = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease]; NSLog(@"%@", data); [responseData release]; } 

This will download Google and print the content.

+7
source

Source: https://habr.com/ru/post/914163/


All Articles