ios - 如果app在终止期间发送http请求,服务器会收到吗?
<p><p>我想知道,如果我的 iOS 应用程序正在终止,如果我发送 HTTP 请求通知服务器用户下线,服务器会收到此请求吗?</p>
<p>我的代码如下:</p>
<pre><code>- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSMutableDictionary *para = [initWithCapacity:0];
valueForKey:@"UserBindId"] forKey:@"unionId"];
;
[Post updateOnlineWithParameters:para withBlock:^(NSDictionary *commitResult, NSError *error) {
NSLog(@"applicationWillTerminate = %@", commitResult);
}];
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>见 <a href="https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW3" rel="noreferrer noopener nofollow">Executing Finite-Length Tasks</a>例如,当应用进入后台模式时,如何让应用请求一点额外时间(最多 3 分钟,这对于您的目的应该绰绰有余)。</p>
<pre><code>- (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
;
bgTask = UIBackgroundTaskInvalid;
}];
NSDictionary *para = @{@"unionID" : [ valueForKey:@"UserBindId"],
@"OnlineState" : @0};
[Post updateOnlineWithParameters:para withBlock:^(NSDictionary *commitResult, NSError *error) {
// when done, tell the OS that the background task is done
;
bgTask = UIBackgroundTaskInvalid;
}];
}
</code></pre>
<p>我尝试稍微简化您的参数(不需要可变字典......您可以使用字典文字),因此请根据您的需要进行调整。但希望你能按照这里的基本思路,如果你想在应用进入后台时做某事,你必须请求权限。</p></p>
<p style="font-size: 20px;">关于ios - 如果app在终止期间发送http请求,服务器会收到吗?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31420556/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31420556/
</a>
</p>
页:
[1]