Skip to content

Instantly share code, notes, and snippets.

@jzucker2
Last active August 16, 2017 18:37
Show Gist options
  • Save jzucker2/2622d1c5edc3d2b85bc7a571f0bd1ae3 to your computer and use it in GitHub Desktop.
Save jzucker2/2622d1c5edc3d2b85bc7a571f0bd1ae3 to your computer and use it in GitHub Desktop.

Background Unsubscribe on iOS

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.

Swift

# 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();
}

Objective-C

# 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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment