菜鸟教程小白 发表于 2022-12-12 15:06:04

ios - Alamofire 错误 : Operation couldn't be completed. 软件导致连接中止


                                            <p><p>当我调用 API 并锁定手机屏幕或最小化应用程序时,Alamofire 给我一个错误。</p>

<blockquote>
<p><strong>&#34;The operation couldn&#39;t be completed. Software caused connection
abort&#34;</strong></p>
</blockquote>

<p> <a href="/image/CwUCL.png" rel="noreferrer noopener nofollow"><img src="/image/CwUCL.png" alt="enter image description here"/></a> </p>

<ul>
<li>我的 API 需要 60 秒才能完成一项操作。</li>
<li>这只发生在 iPhone 中,而不是模拟器中。</li>
</ul>

<p>请给我这个问题的解决方案。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为当应用程序进入后台时,iOS 12 会在最后一个请求返回结果之前关闭连接。您可以使用以下代码来解决您的问题:</p>

<pre><code>if (.applicationState == UIApplicationStateBackground) {
    ;

}

- (void) sendBackgroundDataToServer {
         UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
         bgTask = [ beginBackgroundTaskWithExpirationHandler:^{
             [ endBackgroundTask:bgTask];
         }];

         NSMutableDictionary *dictionary = ;
       forKey:@&#34;floLatitude&#34;];
       forKey:@&#34;floLongitude&#34;];
      // send to server with a synchronous request


       // AFTER ALL THE UPDATES, close the task
       if (bgTask != UIBackgroundTaskInvalid) {
         [ endBackgroundTask:bgTask];
       }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Alamofire 错误 : Operation couldn&#39;t be completed. 软件导致连接中止,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54902181/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54902181/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Alamofire 错误 : Operation couldn&#39;t be completed. 软件导致连接中止