doue9730 2011-05-07 01:03 采纳率: 100%
浏览 82
已采纳

iPhone - 异步调用php页面并确保它已加载

In one of my apps, I'd like to call asynchronously a php page I have written (http://serveradress/page.php?date=20111231), and I would be sure to know if the page could be called (no 404 error, php server down, customised 404 page, missing internet connection, or some things like that). I've planned for that to make the php page return a very simple HTML page with just "OK" in its body or title.

This would be a "phantom" call without use of any UIWebView, or if REALLY nedded, a hidden UIWebView but I'd prefer to avoid this.

Could you help me to write this call ?
And be sure to know if it has been loaded ?

I saw that I should probably use NSURLConnection, but I'm a little bit lost.

Could you help me ?

  • 写回答

1条回答 默认 最新

  • dongxili9934 2011-05-07 07:14
    关注

    NSURLConection is very good solution for asynchronous loading. There are few steps for creating one.

    1. Setup your NSURLConnection

    - (void)viewDidLoad {
        // your code...
        responseData = [[NSMutableData alloc] init];
        NSURL *url = [NSURL URLWithString:@"http://yourdomain.com/"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]];
    }
    

    responseData is your NSMutableData iVar.

    2. Implement delegate methods

    a) In this method we will check for HTTP status codes

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        if ([response respondsToSelector:@selector(statusCode)]) {
            int statusCode = [((NSHTTPURLResponse *)response) statusCode];
            if (statusCode >= 400) {
                [connection cancel]; 
                NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:NSLocalizedString(@"Server returned status code %d",@""),statusCode] forKey:NSLocalizedDescriptionKey];
                NSError *statusError = [NSError errorWithDomain:NSHTTPPropertyStatusCodeKey code:statusCode userInfo:errorInfo];
               [self connection:connection didFailWithError:statusError];
            }
        }
    }
    

    b) This creates your NSData component

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [responseData appendData:data];
    }
    

    c) Handles successful url connection

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // do whatever you want using responseData as your server output
    }
    

    d) Handles errors

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        // handle your error
    }
    

    You can get error info using [error userInfo] And display it in UIAlertView, for example.


    So as I said NSURLConnection is very good solution, but you should also look at ASIHTTPRequest library. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面