The PubNub Swift/Objective-C SDK has built in features for successfully unsubscribing as an iOS app transitions into
the background. It's very easy to take advantage of by simply calling an unsubscribe from the App Delegate or in response
to an NSNotification
named UIApplicationWillResignActive
.
If you choose to wrap unsubscribes yourself in the UIBackgroundTaskIdentifier
API, we cannot ensure it works, nor can we test for such a situation. That is why we have our feature, so we can control the experience as best as possible.
# Initialize client
let config = PNConfiguration(publishKey:"demo", subscribeKey:"demo")
// Already YES by default, this wraps unsubscribes automatically for background execution
config.completeRequestsBeforeSuspension = YES;
let client = PubNub.clientWithConfiguration(config)
# In AppDelegate.swift
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
// As long as `completeRequestsBeforeSuspension` is YES, then
// this will be automatically wrapped in a `UIBackgroundTaskIdentifier` block
self.client.unsubscribeFromAll();
}
# Initialize client
PNConfiguration *config = [PNConfiguration configurationWithPublishKey:@"demo" andSubscribeKey:@"demo"];
// Already YES by default, this wraps unsubscribes automatically for background execution
config.completeRequestsBeforeSuspension = YES;
PubNub *client = [PubNub clientWithConfiguration:config];
# then in AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
// As long as `completeRequestsBeforeSuspension` is YES, then
// this will be automatically wrapped in a `UIBackgroundTaskIdentifier` block
[self.client unsubscribeFromAll];
}