• Stars
    star
    353
  • Rank 120,322 (Top 3 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 11 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

UIApplicationDelegate class that separates the different responsibilities into more more reusable classes.

JSDecoupledAppDelegate

UIApplicationDelegate class that separates the different responsibilities into more more reusable classes.

Motivation

UIApplicationDelegate is perhaps the most convoluted protocol in UIKit. A class that conforms to it won't have one single responsibility, hence it violates one of the most important OOP principles: the Single Responsibility Principle (SRP)

The consequence of this is that all the code that we throw in it becomes essentially non-reusable. Our App Delegate becomes often times the most coupled class to your application, and none of the code we write there is easily reusable in another application. However, a lot of the code that we write there should be reusable: handling notifications, state restoration, opening URLs... Ideally we should extract each of these responsibilities into their own class. This is exactly what JSDecoupledAppDelegate allows you to do.

Protocols

JSDecoupledAppDelegate exposes a series of different protocols that each of the classes that will implement one of the responsibilities will conform to:

@protocol JSApplicationStateDelegate;
@protocol JSApplicationDefaultOrientationDelegate;
@protocol JSApplicationBackgroundFetchDelegate;
@protocol JSApplicationRemoteNotificationsDelegate;
@protocol JSApplicationLocalNotificationsDelegate;
@protocol JSApplicationStateRestorationDelegate;
@protocol JSApplicationURLResourceOpeningDelegate;
@protocol JSApplicationShortcutItemDelegate;
@protocol JSApplicationHealthDelegate;
@protocol JSApplicationProtectedDataDelegate;
@protocol JSApplicationWatchInteractionDelegate;
@protocol JSApplicationExtensionDelegate;
@protocol JSApplicationActivityContinuationDelegate;

JSDecoupledAppDelegate has one property for each of these protocols that you have to set with the object that conforms to it. All of the protocols are optional, meaning that you don't have to provide an object unless you want to implement one of the methods.

Check JSDecoupledAppDelegate.h for more details on what methods each of the protocols has.

NOTE: Some of the protocol methods are missing because they're not necessary: whenever there's a corresponding NSNotification that gets sent (specially if the method doesn't have to return anything), it's a better practice to listen for that instead of implementing the method. This allows you to listen to the notification in multiple places vs. implementing all of the logic related to that event in the same method, hence helping you keep responsibilities separate.

Usage

First you must tell UIApplication that it should instantiate an instance of the JSDecoupledAppDelegate class. This happens on the int main() method normally on the main.m file:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([JSDecoupledAppDelegate class]));

After this, all you have to do is to get the instance of the App Delegate and start setting some of its properties.

The only caveat when using JSDecoupledAppDelegate is making sure that you set some of the delegate properties before one of the methods is going to be invoked (e.g. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;)

One way of doing this is to implement +load on one of the classes that implements a JSDecoupledAppDelegate delegate protocols and doing something similar to this:

+ (void)load
{
    [JSDecoupledAppDelegate sharedAppDelegate].appStateDelegate = [[self alloc] init];
}

This is why JSDecoupledAppDelegate provides a -sharedAppDelegate method which returns the singleton instance of it. This is to make sure that if this gets called before UIApplication creates the App Delegate, not two instances are created. Also because it doesn't make sense for an application to have more than one App Delegate object.

For more details on this, check the Sample Application in this repo.

Installation

Just add this line to your Podfile:

pod 'JSDecoupledAppDelegate', '~> 1.1.0'
  • Manually:

Simply add the files JSDecoupledAppDelegate.h and JSDecoupledAppDelegate.m to your project.

Compatibility

JSDecoupledAppDelegate is compatible with iOS 5, iOS 6, iOS 7 and iOS 8.

License

JSDecoupledAppDelegate is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

JSBadgeView

Customizable UIKit badge view like the one on applications in the iOS springboard.
Objective-C
1,296
star
2

iOS10-Runtime-Headers

iOS 10 Runtime Headers
Objective-C
975
star
3

JSAnimatedImagesView

UIView subclass to easily add a cool animated photo carrusel to your iOS app
Objective-C
408
star
4

JSErrorStackTrace

Category on NSError that stores the stack trace of the creation of the NSError object for later retrieval
Objective-C
122
star
5

Talks

A repo with the talks I've given at conferences, etc
Swift
85
star
6

RubikSwift

Rubik's cube API in Swift + a genetic solver algorithm
Swift
79
star
7

SwiftNeuralNetwork

Swift
76
star
8

Base-iOS-View-Controllers

Convenient base VCs for all your iPhone projects if you, like me, like writing less code. Allows to very easily create controllers with table views, controllers to show data from core data, adjusting the view with the keyboard easily and automatically, and more.
Objective-C
44
star
9

SwiftPoker

Experimental implementation of Poker (Texas Hold'em) rules in Swift Lang
Swift
39
star
10

NotificationCenterSpy

Class to enable NSLogging all NSNotifications being posted in real time
Objective-C
33
star
11

JSSetBreakpointOnMethodForObject

C function that makes the debugger stop whenever an Objective-C method with the specified selector is called on the specified object.
Objective-C
28
star
12

JSReachability

Easy to use iOS class to asynchronously monitor the internet connectivity and reachability of a host.
Objective-C
27
star
13

JSRestNetworkKit

Lightweight library to manage the backend of your iOS / Mac OSX applications perfect to work against a model-based REST backend
Objective-C
27
star
14

JSPebbleReminders

Pebble watchapp and iOS app to retrieve reminders and display them on the watch.
Objective-C
27
star
15

Blog-Posts

My blog posts published in www.javiersoto.me
Swift
26
star
16

JSBarrierOperationQueue

NSOperationQueue subclass that allows you to add operations that behave as a barrier. Basically providing the same functionality dispatch_barrier_async provides in the GCD world.
Objective-C
24
star
17

SwiftChessEngine

Prototype chess engine written in Swift with https://github.com/nvzqz/Sage
Swift
22
star
18

Github-JumpToObjCCounterPart-ChromeExt

Control+Command+Up shortcut to navigate between header and implementation in Objective-C files in Github
JavaScript
16
star
19

JSCache

Simple and fast thread-safe key-value cache class to easily archive objects and cache images to disk (built on top of EGOCache)
Objective-C
15
star
20

UIImageView-AFNetworkingJSAdditions

Improvements over AFNetworking image download handling, meant for perfect perfomance on table views and the ability to fade images in when they finish loading. Requires my fork on AFNetworking
Objective-C
14
star
21

JSLiveCameraFilters

Easy to use UIView subclass to implement a camera feed with real time filters using Core Image (iOS5+)
Objective-C
11
star
22

bash-alfred-workflow

Alfred Workflow to run a bash command and see the output on the Alfred window
PHP
11
star
23

JSTAPIToolsURLProtocol

Easily make your application redirects some of the HTTP(s) requests it makes to your https://www.apitools.com/ account.
Objective-C
9
star
24

Pusher

Mac OSX app to send push notifications to your phone using Pushover
Objective-C
3
star
25

Block-based-UIKit-classes

Subclasses of frequently used UIKit classes to avoid having to implement the delegate method for more readable code
Objective-C
3
star
26

Taller-iOS-Betabeers-Demo-2

Twitter Client
Objective-C
1
star
27

ActiveConfig-SampleNodeBackend

Sample ActiveConfig backend that runs on Heroku.
JavaScript
1
star
28

Taller-iOS-Betabeers-Demo-1

Web Browser
Objective-C
1
star
29

SwiftRayTracer

Swift
1
star
30

javisoto.github.com

Placeholder project to redirect javisoto.github.com to my personal website
1
star
31

watchOSNotificationActionHandlingRadar

A watchOS application to demonstrate a bug when trying to use SwiftUI's NavigationStack to navigate when handling a notification
Swift
1
star