iOS后台每隔X分钟运行API(APP关闭)的实现及合规性咨询(Swift3/4)
Hey there! Let's tackle your two questions head-on—since iOS has super strict background execution policies, this is a common pain point for developers. First, let's cover the App Store rules, then dive into what's possible with Swift 3/4.
先明确:你的需求是否符合审核规则?
Short answer: No, a non-navigation/non-music app can't legally run a service every 10 minutes in the background (even when closed) and still pass App Store review.
Apple's App Store Review Guidelines (specifically section 2.5.4) restrict background execution to apps that fit specific use cases: navigation, audio playback, VoIP, location tracking, Bluetooth peripheral interaction, or other approved background modes. Regular apps without these use cases aren't allowed to wake up on a strict timer in the background—Apple enforces this to preserve battery life and user privacy.
If you try to force this behavior via hacks or misusing background modes, your app will almost certainly get rejected, and in extreme cases, could risk your developer account.
合法的替代方案(符合规则,Swift 3/4实现)
While you can't get exact 10-minute intervals, here are the only Apple-approved ways to run API calls in the background:
1. Background App Refresh
This is Apple's official way for regular apps to update content in the background. The catch? The system decides when to trigger it (based on user usage patterns, network status, battery level)—you can't set a precise timer. But it's the closest you'll get for a non-specialized app.
Implementation Steps:
- First, enable Background App Refresh in your Xcode project's Capabilities > Background Modes.
- Add this code to request permission and set the minimum interval:
// In your AppDelegate's didFinishLaunchingWithOptions UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum) - Implement the background fetch delegate method to run your API call:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // Call your API here fetchDataFromAPI { result in switch result { case .success(let data): // Handle the retrieved data completionHandler(.newData) case .failure(let error): print("API fetch failed: \(error)") completionHandler(.failed) } } } - Note: If the user doesn't open your app often, the system will reduce or stop background refreshes to save battery.
2. Remote Push Notifications (APNs)
You can use Apple Push Notification Service to wake your app in the background and run an API call—this works when your server sends a push with the right payload.
Implementation Steps:
- Enable Push Notifications in Capabilities, and set up APNs on your server.
- Include
content-available: 1in your push payload (this tells iOS to wake the app in the background). - Handle the push in your AppDelegate:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // Check if the app is in the background if application.applicationState == .background { // Run your API call here fetchDataFromAPI { result in completionHandler(.newData) } } } - Caveat: You control the push frequency from your server, but don't spam users—if they disable push notifications, this method stops working. Also, Apple may limit push delivery if you send too many unnecessary notifications.
方案要避开(违反规则,可能被拒)
Don't waste time on these—they either don't work long-term or will get your app rejected:
- Extended Background Tasks: You can request extra background time when the app moves to the background, but it's limited to a few minutes (max ~10 minutes once), and you can't loop this to get repeated intervals. The system will block repeated requests.
- Private APIs/Hacks: Any third-party libraries or code that uses private APIs to force background wakeups will be detected during review. Apple takes this seriously—your app will be rejected, and you could lose your developer account.
Final Takeaway
For your non-specialized app, strict 10-minute background API calls aren't allowed under App Store rules. Stick to Background App Refresh or push notifications as your only legal options. If your business logic absolutely requires precise timing, you may need to rethink your approach—like prompting users to run tasks when they open the app, or adjusting your workflow to work within iOS's constraints.
内容的提问来源于stack exchange,提问作者Akif Demirezen

