Effortless dependency injection framework for Objective-C

Who is using it?

Flashcards+ FlashCards+ CheggApp Chegg – Textbooks, eTextbooks & Study Tools

Are you using AppleGuice and your app is not on the list? Drop me a line.

What AppleGuice does for you?

AppleGuice helps you write clean, reuseable and testable code by allowing you to easily inject your services to any class. Other dependency injection frameworks require binding, xml editing or initializing your classes with a special method. With AppleGuice all you have to do is declare the injected type and thats it. As a bonus, you will still be able to initialize classes with [[MyClass alloc] init] so it is even easier to integrate it with your existing code base.

Show Me

Inject your first injectable instance with 3 simple steps:

Start AppleGuice


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Some code

[AppleGuice startService];

//More Code

Create your injectable service

Mark your injectable service with the protocol AppleGuiceInjectable so AppleGuice will find it.

@protocol MyServiceProtocol <AppleGuiceInjectable>

-(void) doStuff;


@interface MyService : NSObject<MyServiceProtocol>

@implementation MyService

Enjoy automatic injection while coding

Create an ivar prefixed with the ioc prefix (the default is _ioc_). AppleGuice will automatically inject the proper implementation when calling the init method.

@interface MyClass : NSObject

@property (nonatomic, strong) id<MyServiceProtocol> ioc_myService;


@implementation MyClass

//Now, you can use _ioc_myService anywhere. Even in the init function!

-(id) init {
  self = [super init];
  [self.ioc_myService doStuff];
  return self;

AppleGuice initialized _ioc_myService without any manual binding!

Stub with ease while testing

#import <AppleGuice/AppleGuice.h>

@implementation MyClassTests {
    MyClass* classUnderTest;

    [super setUp];
    [AppleGuice startService];
    [AppleGuice setInstanceCreationPolicy:AppleGuiceInstanceCreationPolicyCreateMocks];
    classUnderTest = [[MyClass alloc] init];

-(void) test_foo_returnsValue {
  //the injectable ivar is initialized with a mock. You can stub methods on it as you normally do with OCMock.
  [[[classUnderTest.ioc_myService expect] andReturn:@"someString"] doStuff:OCMOCK_ANY];

  [classUnderTest foo];

  [classUnderTest.ioc_myService verify];

*When testing, AppleGuice works best with OCMock.

Inject In every flavour

Injecting a service is done by declering an ivar in a class. You can add it in the interface, implementation, as a property or even inside a private category. AppleGuice will find it. Injection comes in three flavours:

@interface MyClass () {
    MyService* _ioc_MyService; //will create an instance of MyService.
    id<MyServiceProtocol> _ioc_MyService //will create an instance of the first class conforming to MyServiceProtocol.
    NSArray* _ioc_MyProtocol //will return an array containing instances of all classes conforming to MyProtocol

More features

Singletons in a snap

Instead of messing your code with shared instance declerations or macros, you can just add AppleGuiceSingleton to the implemented protocols list and AppleGuice will always return the same instance.

@protocol MyServiceProtocol <AppleGuiceInjectable, AppleGuiceSingleton>

Circular dependency support

AppleGuice can handle circular depenency between injected classes as long as the dependent classes conforms to AppleGuiceSingleton.

LazyLoad objects

You can configure AppleGuice to inject a proxy object instead of the real service. Once the service is needed (A method in the service is called) the proxy will be replaced with the real object.

//add in your AppDelegate
[AppleGuice setInstanceCreationPolicy:AppleGuiceInstanceCreationPolicyLazyLoad];

Ready to start?

Check out the quick installation guide.

Documentation can be found here. githalytics.com alpha