简体   繁体   English

即使应用程序终止,如何在 Swift 中每小时在后台获取数据?

[英]How to fetch data in background every hour in Swift even the app is terminated?

I am making an app that provides functionality to fetch data every hour.我正在制作一个提供每小时获取数据功能的应用程序。 It fetches data even when the app terminates.即使应用程序终止,它也会获取数据。 How can I implement this functionality?如何实现此功能?

After a lot searching I found background fetching using performFetchWithCompletionHandler.经过大量搜索,我发现使用 performFetchWithCompletionHandler 进行后台提取。 Will this function work even if my app terminates?即使我的应用程序终止,此功能也能工作吗? What I mean is if my app is in closed or terminated state, can I call this function automatically?我的意思是如果我的应用程序处于关闭或终止状态,我可以自动调用这个函数吗?

You can't do anything if your app is in terminated state.如果您的应用处于终止状态,您将无能为力。 Apple is not allowing anything (except receiving notification) in terminated or closed state. Apple 不允许处于终止或关闭状态的任何内容(接收通知除外)。

You are also restricted for background execution also.您也受到后台执行的限制。 You can perform specific kind of task in background mode like : Location update,Music playing, VOIP, finite length task, Newsstand downloads, etc .您可以在后台模式下执行特定类型的任务,例如: Location update,Music playing, VOIP, finite length task, Newsstand downloads, etc You can refer apple documentation for more details about background execution.你可以参考苹果文档了解更多关于后台执行的细节。

Hope this will help :)希望这会有所帮助:)

Once I needed this same when I was working on some project, unfortunately I didn't find any solution for it.有一次我在做某个项目时需要同样的东西,不幸的是我没有找到任何解决方案。

You can run a task when your app is in Background state(that too with limited services) but apple doesn't allow to run a task when app is Terminated当您的应用程序处于后台状态时,您可以运行任务(这也是有限的服务),但苹果不允许在应用程序终止时运行任务

Please go through the link.请通过链接。

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

and this is for background running.这是用于后台运行的。 Sending Latitude and Longitude to Server when app is in background 当应用程序在后台时将纬度和经度发送到服务器

Your app is terminated once it will not do any kind of task related to it.一旦您的应用程序不执行任何与其相关的任务,它就会终止。 Apple Doesn't support it.苹果不支持。 You can not call Functions when your app is not running.当您的应用未运行时,您无法调用 Functions。 So it's not possible Due to apple rules.所以这是不可能的,因为苹果规则。 performFetchWithCompletionHandler is only work when your app is running if your application is terminated it will not perform any kind of task. performFetchWithCompletionHandler仅在您的应用程序运行时有效,如果您的应用程序终止,它将不会执行任何类型的任务。

From what i have experienced:从我的经历来看:

First of all the timer will stop when the app goes to the background to stop fast battery drainage and expand the lifetime of the battery even to save network bundles quota consumption while you are not actively using the app.首先,当应用程序进入后台时,计时器将停止以停止快速电池消耗并延长电池寿命,甚至在您不主动使用应用程序时节省网络捆绑配额消耗。

You can do background fetch tasks by adding the following in the AppDelagate "as you mentioned" :您可以通过在AppDelagate "as you提到的"中添加以下内容来执行后台提取任务:

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    //Do whatever you need
    //This will keep running for max 3 mins
}

But the same is for background fetch tasks.但后台获取任务也是如此。 You have got maximum 3 min of background activity except for Locations services, Audio playing, VOIP and some other tasks.除了位置服务、音频播放、VOIP 和其他一些任务外,您有最多 3 分钟的后台活动。

I really don't know the use and the functionality of the app and why do you need to grab data from the server every 1hr if the app is already closed and the user is not concerned about seeing the updated data.我真的不知道应用程序的用途和功能,如果应用程序已经关闭并且用户不关心看到更新的数据,为什么你需要每 1 小时从服务器获取数据。

But if this data is important to be updated every hour and need to notify the user with the updates "for instance checking the status of a certain event on the server, like making a chat app and the user received a message" .但是,如果此数据很重要,则每小时更新一次并且需要通知用户更新“例如检查服务器上某个事件的状态,例如制作聊天应用程序并且用户收到一条消息” Then it is better to send a remote push notification to the user from the server upon a certain event taking place and this will make the user get the app again on the foreground and the app will start fetching the updated data "in my case here the message" .然后最好在发生某个事件时从服务器向用户发送远程推送通知,这将使用户再次在前台获取应用程序,并且应用程序将开始获取更新的数据“在我的情况下,这里是消息”

you can find location in app terminated state, after significantly change in location like about 500 meter.在位置发生显着变化(例如约 500 米)后,您可以在应用终止状态下找到位置。 here is working example这是工作示例

http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

but what I am stuck at is post location through webservice.但我坚持的是通过网络服务发布位置。

@Jack.Right you can call the method after every 1 hour so use NSTimeinterval it would be helpful!! @Jack.Right 您可以每 1 小时调用一次该方法,因此使用 NSTimeinterval 会有所帮助!!

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];

    //create new uiBackgroundTask
    __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    //and create new timer with async call: 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //run function methodRunAfterBackground
        NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(methodRunAfterBackground) userInfo:nil repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在应用程序活动,后台或终止时每小时触发一个方法 - How to trigger a method every hour irrespective to app active, background or terminated ios应用是否可以从服务器获取数据(即使终止) - Can an ios app fetch data from a server even if it is terminated Swift - 即使应用程序终止,如何使用 URLSession 发送位置 - Swift - How to send location with URLSession even if the App is terminated 快速显示本地通知,即使应用终止 - swift display local notification even the app terminated 即使应用终止,也要进行后台处理 - Doing background process even if app is terminated 在iOS中终止应用后,每小时进行一次api调用 - Make api call every hour after app is terminated in IOS 即使应用程序处于后台或终止状态,我该如何采取措施接收远程通知? - How can I do an action on receiving a remote notification even when the app is in background or terminated? 即使应用程序终止,iOS 9 如何获取位置 - iOS 9 how get locations even if app terminated 当应用程序终止时,Flutter 后台获取是否在 iOS 上工作? - Does Flutter Background Fetch work on iOS when the app is terminated? 即使应用程序在 iOS 中终止,我们还能运行后台任务吗 - Can we run background task even app is terminated in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM