Skip to main content

App Delegate Task Configuration (app_delegate)

Modify AppDelegate file

The app_delegate task is used to modify the AppDelegate.mm or AppDelegate.swift file in an iOS project. This task allows you to insert code, import statements, or comments into specific methods within the AppDelegate file. The modifications can be made before or after a specified point in the method. This task is particularly useful for integrating third-party libraries or SDKs that require changes to the AppDelegate file.

Task Properties

PropertyTypeDescription
task"app_delegate", requiredSpecifies the task type, which should be set to "app_delegate" for this task.
lang"objc" or "swift", requiredSpecifies the file language.
namestringAn optional name for the task. If provided, the task state will be saved as a variable. Visit Task and Action States page to learn more.
labelstringAn optional label or description for the task.
whenobjectVisit Conditional Tasks and Actions page to learn how to execute task conditionally.
actionsArray<Action>, requiredAn array of action items that define the modifications to be made in the file.

Action Properties

Common properties

PropertyTypeDescription
namestringAn optional name for the task. If provided, the task state will be saved as a variable. Visit Task and Action States page to learn more.
whenobjectVisit Conditional Tasks and Actions page to learn how to execute action conditionally.

Context reduction properties

PropertyTypeDescription
blockone of Allowed Method NamesSpecifies the name of the method within AppDelegate where the modification should be applied. It must match one of the allowed method names. See Allowed Method Names section for details. Omitting this field instructs the action item to modify whole file.
beforestring or {regex: string, flags: string}Text or code that is used to specify a point within the context where text should be inserted before. It can be a string or an object with a regex and flags field to perform a regex-based search.
afterstring or {regex: string, flags: string}Text or code that is used to specify a point within the context where text should be inserted after. It can be a string or an object with a regex and flags field to perform a regex-based search.
searchstring or {regex: string, flags: string}A string or object (with regex and flags) that narrows the context to a specific text within the method or file.

Context modification properties

PropertyTypeDescription
prependstring or {file: string}Text or code to prepend at the beginning of the specified context. It can be a string or an object with a file field that points to a file containing the code to prepend.
appendstring or {file: string}Text or code to append at the end of the specified context. It can be a string or an object with a file field that points to a file containing the code to append.
replacestring or {file: string}Text or code to replace the entire specified context. It can be a string or an object with a file field that points to a file containing the code to replace.
scriptstringJS code script to evaluate. In script these functions are available to be called: `await prepend(content), await append(content), await replace(content)

Other properties

PropertyTypeDescription
exactbooleanA boolean flag that modifies the whitespace and new line management.
strictbooleanSpecifies the behavior of the before and after fields. If set to true, the task will throw an error if the text in the before or after field is not found in the context, otherwise, it will ignore the field.
ifNotPresentstringIndicates that the task should only be executed if the specified text or code is not present within the specified context.
commentstringAn optional comment to add before the inserted code or text. The comment is purely informational and does not affect the code's functionality.

Allowed Method Names

The block field within the action items must match one of the allowed method names within the AppDelegate file. The method is created if it does not exist. The following method names are allowed:

  • didFinishLaunchingWithOptions
  • applicationDidBecomeActive
  • applicationWillResignActive
  • applicationDidEnterBackground
  • applicationWillEnterForeground
  • applicationWillTerminate
  • openURL
  • restorationHandler
  • didRegisterForRemoteNotificationsWithDeviceToken
  • didFailToRegisterForRemoteNotificationsWithError
  • didReceiveRemoteNotification
  • fetchCompletionHandler

Example

Here's an example of how to use the app_delegate task:

task: app_delegate
label: "Integrate Firebase"
actions:
- prepend: "#import <Firebase.h>"
- block: "didFinishLaunchingWithOptions"
prepend: "[FIRApp configure];"
- block: "openURL"
before: "return YES;"
append: "// Handle custom URL schemes here."

In this example, the task is labeled "Integrate Firebase." It defines three action items:

  1. It prepends the header import to the file #import <Firebase.h>.
  2. In the didFinishLaunchingWithOptions method, it prepends the code [FIRApp configure];.
  3. In the openURL method, it adds a comment before the return YES; statement, followed by code to handle custom URL schemes.