我已經嘗試過這種方法,我想了解其餘API
在iOS的整合。 我想了解有關JSON
在Web服務中的解析。我知道這些方法,但如何使用它?但每種方法的功能是什麼?如何在iOS中使用NSURLConnection集成rest API?
此IBAction方法向服務器發送註冊請求字符串。
-(void)genSignup
{
responseData = [NSMutableData data];
urlLoc= [urlLoc stringByAppendingString:service];
NSLog(@"%@",requestString);
NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlLoc]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//NSLog(@"%@",request);
PostConnectionRegisterGenUser = [[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)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[MyClass removePregressIndicator:self.view];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[MyClass removePregressIndicator:self.view];
if (connection == PostConnectionRegisterGenUser)
{
NSError *error;
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"%@",results);
}
}
如何使用它?
你在尋找一個解決問題/崩潰/錯誤或一些文檔的鏈接/引導? – ddb
檢查此http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/ – 7vikram7
NSURLConnection已棄用有效的Mac OS 10.11和iOS 9.因此,此時應使用NSURLSession代替NSURLConnection的。正如NSURLConnection.h頭文件所示: 不推薦使用:不應再使用NSURLConnection類。 NSURLSession是NSURLConnection的替代品。 –