网页完成加载后读取数据(Reading Data after Webpage is completely done loading)

编程入门 行业动态 更新时间:2024-10-27 14:29:35
网页完成加载后读取数据(Reading Data after Webpage is completely done loading)

我正在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.

更多推荐

本文发布于:2023-07-21 09:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1208962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   网页   数据   Reading   loading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!