iPhone - 异步调用PHP页面,并确保它已经加载 [英] iPhone - Call a php page asynchronously and be sure it has been loaded

查看:156
本文介绍了iPhone - 异步调用PHP页面,并确保它已经加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序之一,我想异步调用我写一个PHP页面(HTTP://serveradress/page.php日期= 20111231),我就一定要知道,如果该页面可能是所谓的(没有404错误,PHP服务器关闭,自定义404错误页面,缺少互联网连接,或类似的一些事情)。
我计划,为了使PHP页面返回一个非常简单的HTML页面,在其身体或标题就是OK。

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.

这是不使用任何的UIWebView的幻影的呼叫,或者如果真的nedded,一个隐藏的UIWebView但我preFER避免这种情况。

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 ?

我看到了,我应该使用NSURLConnection的,但我是一个有点失去了。

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

你能帮助我吗?

推荐答案

NSURLConection是异步加载很好的解决方案。有用于制作一个仅有几步之遥。

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

1。设置您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 是NSMutableData伊娃。

responseData is your NSMutableData iVar.

2。实施委托方法

一)在这个方法中,我们将检查HTTP状态codeS

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)这将创建您的NSData组件

b) This creates your NSData component

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

C)把手成功URL连接

c) Handles successful url connection

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

D)处理错误

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

您可以使用 [错误USERINFO] 得到错误信息,并在UIAlertView中显示它,例如

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

所以,正如我说的是NSURLConnection的非常好的解决方案,你也应该看的 ASIHTT prequest库。 :)

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

这篇关于iPhone - 异步调用PHP页面,并确保它已经加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆