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

iPhone-异步调用php页面并确保其已加载,php,iphone,objective-c,asynchronous,nsurlconnection,Php,Iphone,Objective C,Asynchronous,Nsurlconnection,在我的一个应用程序中,我想异步调用我编写的php页面(http://serveradress/page.php?date=20111231),我肯定知道是否可以调用该页面(没有404错误、php服务器关闭、自定义404页面、缺少internet连接或类似的事情)。 我计划让php页面返回一个非常简单的HTML页面,其正文或标题中只有“OK” 这将是一个不使用任何UIWebView的“幻影”调用,或者如果真的需要,则是一个隐藏的UIWebView,但我更愿意避免这种情况 你能帮我写这个电话吗? 确

在我的一个应用程序中,我想异步调用我编写的php页面(http://serveradress/page.php?date=20111231),我肯定知道是否可以调用该页面(没有404错误、php服务器关闭、自定义404页面、缺少internet连接或类似的事情)。 我计划让php页面返回一个非常简单的HTML页面,其正文或标题中只有“OK”

这将是一个不使用任何UIWebView的“幻影”调用,或者如果真的需要,则是一个隐藏的UIWebView,但我更愿意避免这种情况

你能帮我写这个电话吗?
确保知道它是否已加载

我知道我应该使用NSURLConnection,但我有点迷路了


您能帮助我吗?

NSURLConnection是异步加载的非常好的解决方案。创建一个有几个步骤

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 iVar

2。实施委托方法

a) 在这个方法中,我们将检查HTTP状态代码

- (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组件

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}
c) 处理成功的url连接

- (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
}
例如,您可以使用
[error userInfo]
获取错误信息,并将其显示在UIAlertView中



正如我所说的,NSURLConnection是一个非常好的解决方案,但您也应该看看。:)

nsurlconnection是异步加载的非常好的解决方案。创建一个有几个步骤

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 iVar

2。实施委托方法

a) 在这个方法中,我们将检查HTTP状态代码

- (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组件

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}
c) 处理成功的url连接

- (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
}
例如,您可以使用
[error userInfo]
获取错误信息,并将其显示在UIAlertView中



正如我所说的,NSURLConnection是一个非常好的解决方案,但您也应该看看。:)

@Kashiv:谢谢你,我正在尝试。我想知道,我如何知道NSData中返回的值的类型?它可以是字符串以外的任何东西…@Kashiv:nshttpropertyStatusCodeKey si表示不赞成。我应该用什么来代替呢。我正在寻找替代者,但我没有找到合适的。奇怪的是,即使说不推荐,NSHTTPPropertyStatusCodeKey在编译时也无法识别。这是唯一的错误,很抱歉。作为errorDomain,您可以使用任何字符串,例如@“HTTPStatusCode”。请检查这是否有效;)@卡希夫:是的:-)这就是我所做的:-)但是对于NSData,我可以接收我自己发送的字符串,但是如果请求没有像我预期的那样成功,我也可以接收任何其他东西。我怎么知道我收到了什么样的数据?但是什么样的数据?我指的是图像、文本、html、错误页面等。?您可以使用id obj=[NSKeyedUnarchiver unarchiveObjectWithData:responseData];然后是if([obj是kindof类:[NSString类]])或[UIImage类]。然后,通过if语句,您可以用多种方式处理它。但如果您的服务器返回错误的状态码,则将调用connection:didFailWithError:。@Kashiv:谢谢,我正在尝试。我想知道,我如何知道NSData中返回的值的类型?它可以是字符串以外的任何东西…@Kashiv:nshttpropertyStatusCodeKey si表示不赞成。我应该用什么来代替呢。我正在寻找替代者,但我没有找到合适的。奇怪的是,即使说不推荐,NSHTTPPropertyStatusCodeKey在编译时也无法识别。这是唯一的错误,很抱歉。作为errorDomain,您可以使用任何字符串,例如@“HTTPStatusCode”。请检查这是否有效;)@卡希夫:是的:-)这就是我所做的:-)但是对于NSData,我可以接收我自己发送的字符串,但是如果请求没有像我预期的那样成功,我也可以接收任何其他东西。我怎么知道我收到了什么样的数据?但是什么样的数据?我指的是图像、文本、html、错误页面等。?您可以使用id obj=[NSKeyedUnarchiver unarchiveObjectWithData:responseData];然后是if([obj是kindof类:[NSString类]])或[UIImage类]。然后,通过if语句,您可以用多种方式处理它。但如果您的服务器将返回错误的状态代码,则将调用connection:didFailWithError:。