What is Method Swizzling?

Method swizzling is a technique used in Objective-C and Swift that allows developers to interchange the implementations of two methods at runtime. This is particularly useful in modifying the behavior of existing system methods without subclassing or altering the original source code.

In the context of iOS development, swizzling can intercept system calls to the AppDelegate’s push notification delegate methods, providing an opportunity to insert additional logic such as analytics, third-party SDK functions, or custom handling.

Advantages of Using Swizzling for Push Notifications

Swizzling streamlines the process of integrating push notifications with various services and frameworks. When using swizzling:

  • Developers do not need to write boilerplate code to handle common notification tasks.

  • ensures that all the necessary methods are called without explicitly invoking them in the app’s code.

-SDKs can provide out-of-the-box functionality, enhancing or augmenting notification handling with minimal setup required.

Default Behavior in Castled SDK

Castled SDK is designed to simplify push notification handling. By default, swizzling is enabled, which means the SDK automatically handles the registration of device tokens, the presentation of notifications, and the response to user interactions with those notifications.

This default setup allows for a seamless integration process, requiring minimal code changes on the developer’s part.

Disabling Swizzling in Castled SDK

Although swizzling is convenient, some developers may prefer to have more granular control over notification handling or may need to comply with specific architectural requirements. In such cases, Castled SDK allows swizzling to be disabled.

To disable method swizzling with Castled SDK, you must update the Info.plist file of your application. Add a new key-value pair where the key is specific to the Castled SDK’s configuration and the value explicitly indicates that swizzling should be turned off.

Info.plist
<key>CastledSwizzlingDisabled</key>
<true/>

Manual Handling of Push Notifications

Once swizzling is disabled, the responsibility of invoking the necessary methods falls on the developer. Developer needs to implement various UNUserNotificationCenterDelegate and UIApplicationDelegate methods to handle different notification scenarios.

Castled SDK exposes corresponding APIs to be called within these delegate methods to maintain the functionality that swizzling would have provided:

1. Handling Device Token Registration

When the device registers for push notifications, you should capture the device token and pass it to the Castled SDK using the setPushToken method.

2. Handling Registration Failure

If the device fails to register, log the error. There is no Castled method for this scenario as it is purely for logging purposes.

3. Handling Notification Responses

When the user interacts with the notification (e.g., tap on the notification), invoke the userNotificationCenter(didReceive:response) method from the Castled SDK.

4. Handling Foreground Notification Presentation

Determine how to handle notifications when the app is in the foreground with the userNotificationCenter(willPresent:notification) Castled method.

5. Handling Background Notifications

For handling background notification fetch calls, use the didReceiveRemoteNotification Castled method.