0

URL経由でPHPページにGETリクエストを送信するiOSでログインメソッドを作成しています.Webサイトから出力を読み取ろうとすると、PHPがmysqlクエリを完了する前にデータが読み取られます.ウェブページの読み込みが完全に完了するまで待機して、コードからデータを読み取る方法:

-(NSString *)getWebpageData:(NSString *)url {
    NSURL *URL = [NSURL URLWithString:url];
    NSError *error = nil;
    NSString *content = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error];
    return content;
}
4

1 に答える 1

0

私はそのようにNSURLConnection経由して使用しようとします...sendAsynchronousRequest

NSOperationQueue *myQueue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        //do something
    }];

ハンドラー ブロックが起動されたときにコンテンツがあることは、ほとんど自明です。

別のオプションは、NSURLConnectionDataDelegate. URL を呼び出すと、処理が完了したことを知らせるいくつかのメソッドが起動されます。

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //Fired on error
}

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
    //Fired First
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //Fired Second
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //Fired Third
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Fired Fourth
}

デリゲート メソッドを使用すると、didReceiveDataすぐにデータを取得できるようになります。幸運を。

于 2013-06-20T03:52:56.467 に答える