iPhone で URL からデータを受信しますか?
-
09-09-2019 - |
質問
Apple Document を使用してアプリを作成しています。サーバーへの接続には成功しましたが、サーバーから 0 バイトを受け取りました (応答データなし)。私は次の手順を実行します。
ビューベースのアプリを作成し、プロパティ「receivedData」を追加します。
ViewController.h 内:
@property (nonatomic, retain) NSMutableData *receivedData;
ViewController.m 内:
@synthesize receivedData;
ViewController.m のアクション「ViewDidLoad」に次を追加します。
receivedData = [NSMutableData alloc];
ビューにボタンを追加し、そのボタンに対するアクションを追加します。
// create the request NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://..."] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; // create the connection with the request // and start loading the data NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { // Create the NSMutableData that will hold // the received data // receivedData is declared as a method instance elsewhere receivedData=[[NSMutableData data] retain]; } else { // inform the user that the download could not be made }
これらのコードをデバッグすると、receivedData が 0 バイトを返すことがわかりました。何が問題なのか何か考えはありますか?私のコードを簡単に修正していただければ幸いです。
解決
(HTTP応答が受信された後)デリゲートのコールバックは、フレームワークによって呼び出された後のデータのみをreceivedData中に書かれており、利用できるようになります -
他のヒント
答えは、私が最後に答えたときと同じです。 iPhoneでURLからデータを受信するにはどうすればよいですか?. 。詳しく説明しましたが、読みましたか?
所属していません StackOverflow