Help us understand the problem. What is going on with this article?

Objective-Cで非同期通信

More than 5 years have passed since last update.

.hに

NSURLConnection *connection;
NSMutableData *listData;

viewDidLoadとかに

// パラメータを作成
NSString *data = [[NSString alloc] init];
data = [NSString stringWithFormat:@"user_id=%@", uuid_];

NSURL *url = [NSURL URLWithString:[NSString stringWithCString:URL encoding:NSUTF8StringEncoding]];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];

// 取得データを格納
listData = [[NSMutableData alloc] initWithCapacity:0];

if (connection) [connection cancel];
connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];

別にメソッドを作成

// レスポンスを受け取った時点で呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [listData setLength:0];
}

// データを受け取る度に呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data2 {
   [listData appendData:data2];
}

// 非同期通信ダウンロード完了
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"%@", listData);
}

解析するために、NSDataをNSStringに変換

NSString *json_string = [[NSString alloc] initWithData:listData encoding:NSUTF8StringEncoding];
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away