ios - 在后台模式下运行网络连接

标签 ios objective-c uiapplication

即使应用程序处于后台模式(最小化应用程序),我也有发送和接收数据的代码:

MyViewController.m

    -(void)viewDidAppear:(BOOL)animated{
        [self doUpdateEvenAppMinimized];
    }

    - (void) doUpdate{

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            [self beginBackgroundUpdateTask];

            [self sendFilesToServer];//Inside this method have a sleep that call every 5 minutes

//The code used in sendFilesToServer is the same in this website https://dcraziee.wordpress.com/2013/05/29/how-to-upload-file-on-server-in-objective-c/

            //[self endBackgroundUpdateTask];//This method is forever...so I not need to call this line
        });

    }
    - (void)beginBackgroundUpdateTask{ 
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{   
    [self endBackgroundUpdateTask];
    }];
    }

    - (void) endBackgroundUpdateTask{

    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundUpdateTask = UIBackgroundTaskInvalid;

    }

文档说最长时间是 10 分钟,要删除它,我使用实现长时间运行的任务 的概念,为此我选择我的项目 > 功能 > 后台模式(打开) > 外部附件通信(选中)。

通过这些步骤,我的申请将免除 10 分钟?

最佳答案

试图规避规则在后台运行听起来是错误的做法。考虑使用 NSURLSession用于与应用的生命周期无关的长时间运行的网络操作。

关于ios - 在后台模式下运行网络连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28541224/

相关文章:

ios - 如何在 iOS 中创建 gmail 收件人格式?

ios - Swift AppDelegate 应用程序 :didFinishLaunchingWithOptions error

ios - 字符串过滤器的核心数据数组

ios - 无尽的 UICollectionView 幻灯片 + scrollToItemAtIndexPath

ios - iOS:将UIImage中的黑色更改为另一种颜色

ios - 如何从 iOS 应用程序在 viber、Facebook Messenger 和 Instagram 上分享文本?

objective-c - iOS 从非运行状态启动应用程序后处理通知

ios - 这个地理定位计算有多准确?

ios - 从 JSON 调用获取日期时遇到问题

ios - UINavigationItem 标题截断