无论如何,IOS在那里发送网页请求并在iphone ios上忘了它?

时间:2010-12-18 00:41:31

标签: iphone ios background nsurl

我想从我的应用程序发出一个叫www.test.com/updateMySqlTables.php的电话,但我不想等待处理完成。

类似这样的电话

NSString *checkReturn = [NSString stringWithContentsOfURL:checkURL]

但不想等待回归。我希望我的服务器在后台清理表中的任何旧日期。

5 个答案:

答案 0 :(得分:1)

是。加载请求:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"www.test.com/updateMySqlTables.php"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

并确保添加将在成功/错误时调用的适当委托方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
  // Success handling here
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
  // Error handling here
}

答案 1 :(得分:1)

我认为您需要的只是启动一个异步请求并实现其相应的requestDidDeceiveData:方法和requestDidFinish:方法,并将这两个方法留空,因为您不关心请求可能生成的任何响应。由于它是异步的,它将自动在后台运行。您应用的主要线程不会受到影响。

iOS SDK有自己发布异步请求的方式。您还可以考虑使用更着名的ASIHTTPRequest包来完成您的工作,这样更容易设置和监控。

答案 2 :(得分:0)

一种方法是使用performSelectorInBackground:withObject:或[NSThread detachNewThreadSelector:toTarget:withObject:]在后台线程中加载信息。两者都需要您创建一个新方法来加载数据。

答案 3 :(得分:0)

好的,现在删除了php标签,我的答案已不再相关了。

//Erase the output buffer
ob_end_clean();
//Tell the browser that the connection's closed
header("Connection: close");

//Ignore the user's abort.
ignore_user_abort(true);

//Extend time limit to 30 minutes
set_time_limit(1800);
//Extend memory limit to 10MB
ini_set("memory_limit","10M");
//Start output buffering again
ob_start();

//Tell the browser we're serious... there's really
//nothing else to receive from this page.
header("Content-Length: 0");

//Send the output buffer and turn output buffering off.
ob_end_flush();
//Yes... flush again.
flush();

//Close the session.
session_write_close();

//Do some work

被盗:http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/

答案 4 :(得分:0)

我知道在Linux中可以在后台运行php脚本。

我想在某些配置中使用exec可能会有限制。

exec ("/usr/bin/php yourscript.php >/dev/null &");

哪个在后台运行php脚本(&)并且sneds输出到无处(/ dev / null)