IOS 有没有办法发送网页请求并在 iphone ios 上忘记它?

发布于 2024-10-08 19:43:59 字数 233 浏览 6 评论 0原文

我想从我的应用程序向 www.test.com/updateMySqlTables.php 发送一个电话,但我不想等待该过程完成。

像这样的电话

NSString *checkReturn = [NSString stringWithContentsOfURL:checkURL]

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

I want to send a call to lets say www.test.com/updateMySqlTables.php from my app but i dont want to wait around for the proccess to complete.

something like this call

NSString *checkReturn = [NSString stringWithContentsOfURL:checkURL]

But don't want to wait for the return. I want my server to cleanup any old dates in table in the background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帅的被狗咬 2024-10-15 19:43:59

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

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

I think all you need is start an asynchronous request and implement its corresponding requestDidDeceiveData: method and requestDidFinish: method, and leave these two methods blank, since you don't care about any response the request might generate. Since it's an asynchronous one, it'll automatically run in the background. Your app's main thread won't be affected.

iOS SDK has its own way to post an asynchronous request. you can also consider to use a more famous ASIHTTPRequest package to do your job, which is easier to setup and monitor.

陌生 2024-10-15 19:43:59

是的。加载请求:

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
}

Yes. Load the request:

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

And be sure to add the appropriate delegate method(s) that will be called on success/error:

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
  // Error handling here
}
墨小沫ゞ 2024-10-15 19:43:59

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

One way would be to load your information in a background thread, using performSelectorInBackground:withObject: or [NSThread detachNewThreadSelector:toTarget:withObject:]. Both would require you to create a new method to load the data.

这个俗人 2024-10-15 19:43:59

好的,现在 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/

Ok, now that php tag was removed, my answer is not relative anymore.

//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

stolen from: http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/

暖树树初阳… 2024-10-15 19:43:59

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

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

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

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

I know that in Linux it is possible to run a php script in the background.

I guess there may be a restriction on using exec in some configurations.

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

Which runs the php script in the background (&) and sneds output to nowhere (/dev/null)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文