Dependecy injection in iOS apps post

09/01/2014 with tags : objc, code

Silicon is a simple dependency injection / service location for Objective-C iOS applications. It was inspired by phalconphp library.

Silicon implements the Inversion of Control pattern and is used as a service dependecy injector. It increases testability of the code and makes it easier to change component implementations. Great if you're tired of over-used singleton pattern.


  • all application services / models / what-ever-app-needs are defined in a service locator

  • all services are lazy initializated when requested and retained

  • access any service by name

  • use build-in dependency injection to inject services


Define service called apiService

Silicon *si = [Silicon si];
[si service:@"apiService" withBlock:^NSObject*(Silicon *si){
           NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
           NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
           // custom config
           return session;

Session object will be created when requested for the first time. After creating service instance Silicon uses it in any subsequent requests.

Using defined service

Silicon *si = [Silicon si];
NSURLSession *api = [si get:@"apiService"]


Grab a code on Github