我正在iOS上创建一个登录方法,通过url向PHP页面发送GET请求,当我尝试从网站读取输出时,在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; }I am making a login method on iOS that sends a GET request to a PHP page via url, when I try to read the output from the website, the data is read before the PHP can complete the mysql query, I was wondering if there were any way to wait until the webpage is completely done loading to read the data from it code:
-(NSString *)getWebpageData:(NSString *)url { NSURL *URL = [NSURL URLWithString:url]; NSError *error = nil; NSString *content = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:&error]; return content; }最满意答案
我会尝试通过sendAsynchronousRequest使用NSURLConnection ,如此...
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以便将数据放在那里。 祝你好运。
I would try using NSURLConnection via sendAsynchronousRequest like so...
NSOperationQueue *myQueue = [[NSOperationQueue alloc]init]; [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { //do something }];It's mostly self-explanatory that when the handler block is fired you have your content.
Another option would be to invoke the NSURLConnectionDataDelegate. When you call your URL it will fire a few methods to let you know when things are done.
- (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 }Using the delegate methods you're likely going to want to leverage didReceiveData so that you have your data right there. Good luck.
更多推荐
发布评论