我想知道如果我的iOS应用程序正在终止,如果我发送HTTP请求通知服务器用户正在离线,服务器是否会收到此请求?
我的代码如下:
$array = file("countries.txt", FILE_IGNORE_NEW_LINES);
$str = "test";
foreach ($array as $key => $val) {
if ($val == $str) {
echo $val;
} else {
echo "not found";
}
}
答案 0 :(得分:1)
请参阅Executing Finite-Length Tasks,了解当应用进入后台模式时如何让应用请求多一点额外时间(最多3分钟,这应该足以满足您的目的)。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:@"GoingOffline" expirationHandler:^{
// Well, we ran out of time before network request finished ... handle that however you want
// but tell the OS that we understand, and that the background task is done
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
NSDictionary *para = @{@"unionID" : [[NSUserDefaults standardUserDefaults] valueForKey:@"UserBindId"],
@"OnlineState" : @0};
[Post updateOnlineWithParameters:para withBlock:^(NSDictionary *commitResult, NSError *error) {
// when done, tell the OS that the background task is done
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
我试图稍微简化你的参数(不需要可变字典......你可以使用字典文字),所以根据你的需要调整它。但希望你可以在这里遵循基本的想法,如果你想在应用程序进入后台时做某事,你必须要求获得许可。
答案 1 :(得分:0)
You can't provide a failsafe user online/offline status capability from the client side, using http requests. As Rob said, if the app is terminated (force quit or crashes e.t.c.), theres nothing you can do. The way to provide this kind of feature is to use a web socket, which is a "constant" two way communication between the phone and server. This way on the server side, you can check whether the connection was lost, which indicates that the client has gone offline. To create such a service yourself would take quite a long time. Luckily Firebase offer such software with a Backend as a Service. It's a godsend. This page describes how to manage offline and online status. It's as easy as pie.