Skip to content

Configure AppDelegate Manually

StreetHawk configures AppDelegate function automatically. In case of conflicts with AppDelegate functions from your application, StreetHawk will call both application’s and StreetHawk’s functions. However if it is still required to manually setup the AppDelegate functions, then follow the below mentioned guidelines.

  • Set auto integration to false

Add the below mentioned code before calling registerInstallForApp()

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      StreetHawk.autoIntegrateAppDelegate = NO;
      [StreetHawk registerInstallForApp:@"<app_key>" withDebugMode:Yes/No];
}

Swift code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
      SHApp.sharedInstance().autoIntegrateAppDelegate = false
      SHApp.sharedInstance().registerInstall(forApp: "<app_key>", withDebugMode: true/false)
}

“StreetHawk.autoIntegrateAppDelegate” is YES by default, it means all below functions are called by StreetHawk itself automatically, and if your AppDelegate has same function, it will call too. Thus if “StreetHawk.autoIntegrateAppDelegate=YES”, you MUST NOT call [StreetHawk ...] in your own AppDelegate function, otherwise cause dead loop.

  • Handle user notification

With manual integration, the application developer is expected to handle user’s notification. Below is the sample code for the same:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings  //since iOS 8.0
{
    [StreetHawk handleUserNotificationSettings:notificationSettings];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [StreetHawk setApnsDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [StreetHawk handleRemoteNotification:userInfo treatAppAs:SHAppFGBG_Unknown needComplete:YES fetchCompletionHandler:nil];
}
//or
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
     [StreetHawk handleRemoteNotification:userInfo treatAppAs:SHAppFGBG_Unknown needComplete:YES fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
    [StreetHawk handleRemoteNotification:userInfo withActionId:identifier needComplete:YES completionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [StreetHawk handleLocalNotification:notification treatAppAs:SHAppFGBG_Unknown needComplete:YES fetchCompletionHandler:nil];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    [StreetHawk handleLocalNotification:notification withActionId:identifier needComplete:YES completionHandler:completionHandler];
}

Swift code:

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
        SHApp.sharedInstance().handle(notificationSettings)
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        SHApp.sharedInstance().setApnsDeviceToken(deviceToken)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        SHApp.sharedInstance().handleRemoteNotification(userInfo, treatAppAs: SHAppFGBG_Unknown, needComplete: true, fetchCompletionHandler: nil)
}
//or
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        SHApp.sharedInstance().handleRemoteNotification(userInfo, treatAppAs: SHAppFGBG_Unknown, needComplete: true, fetchCompletionHandler: completionHandler)
}

func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
        SHApp.sharedInstance().handleRemoteNotification(userInfo, withActionId: identifier, needComplete: true, completionHandler: completionHandler)
}

func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        SHApp.sharedInstance().handle(notification, treatAppAs: SHAppFGBG_Unknown, needComplete: true, fetchCompletionHandler: nil)
}

func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
        SHApp.sharedInstance().handle(notification, withActionId: identifier, needComplete: true, completionHandler: completionHandler)
}
  • Handle background fetch

Background fetch is used for notifying apps existence in the device. In absence of the below mentioned code, the application can be concluded as uninstalled from user’s device.

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [StreetHawk shRegularTask:completionHandler needComplete:YES];
}

Swift code:

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        SHApp.sharedInstance().shRegularTask(completionHandler, needComplete: true)
}
  • Handle open url

Handle registered open url.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [StreetHawk openURL:url];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
{
   return [StreetHawk continueUserActivity:userActivity];
}

Swift code:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return SHApp.sharedInstance().open(url)
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        return SHApp.sharedInstance().continue(userActivity)
}