开发者

IOS is there anyway to send a webpage request and forget about it on iphone ios?

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.


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.


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
}


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.


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/


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜