I define my own protocol:
@protocol NGSAuthProvider <NSObject>
- (BOOL)isReady;
- (BOOL)isSessionValid;
- (void)login;
- (void)logout;
- (NSString *)accessToken;
- (BOOL)handleOpenURL:(NSURL *)url;
@end
I want to have different suppliers. So, one of them is a Facebook provider:
@interface NGSFacebookAuthProvider : NSObject <NGSAuthProvider>
@end
@interface NGSFacebookAuthProvider () <FBSessionDelegate>
@property BOOL ready;
@property(nonatomic, retain) Facebook *facebook;
@property(nonatomic, retain) NSArray *permissions;
@end
@implementation NGSFacebookAuthProvider
- (NSString *)accessToken
{
return [self.facebook accessToken];
}
@end
I am setting up Objection to bind my class to the protocol.
@interface NGSObjectionModule : ObjectionModule
@end
@implementation NGSObjectionModule
- (void)configure
{
self bind:[NGSFacebookAuthProvider class] toProtocol:@protocol(NGSAuthProvider)];
}
@end
I install a global injector:
@implementation NGSAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ObjectionModule *module = [[NGSObjectionModule alloc] init];
ObjectionInjector *injector = [Objection createInjector:module];
[module release];
[Objection setGlobalInjector:injector];
}
I use this in my RootViewController as follows:
@interface RootViewController : UITableViewController
@end
@interface RootViewController ()
@property(nonatomic, retain) id<NGSAuthProvider> authProvider;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.authProvider = [[Objection globalInjector] getObject:@protocol(NGSAuthProvider)];
}
- (void)processConfig {
NSString *token = [self.authProvider accessToken];
}
@end
When I run this, I get the following error:
2011-07-26 21:46:10.544 ngs[6133:b603] +[NGSFacebookAuthProvider accessToken]: unrecognized selector sent to class 0x30c7c
2011-07-26 21:46:10.546 ngs[6133:b603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NGSFacebookAuthProvider accessToken]: unrecognized selector sent to class 0x30c7c'
*** Call stack at first throw:
(
0 CoreFoundation 0x00e825a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00fd6313 objc_exception_throw + 44
2 CoreFoundation 0x00e8417b +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00df3966 ___forwarding___ + 966
4 CoreFoundation 0x00df3522 _CF_forwarding_prep_0 + 50
5 ngs 0x0000324b -[RootViewController processConfig] + 731
6 ngs 0x000041a2 __33-[RootViewController viewDidLoad]_block_invoke_0 + 50
So my class implements the protocol. It is successfully assigned id<NGSAuthProvider>. I tried using [[NGSFacebookAuthProvider alloc] init]explicitly instead of using Objection , and it still crashed.
I tried scrolling the selectors using methods objc/runtime.hto see which selectors are there, but the only thing it finds is initialize:
- (void)logSelectors:(id)obj
{
int i=0;
unsigned int mc = 0;
Method * mlist = class_copyMethodList(object_getClass([obj class]), &mc);
NSLog(@"%d methods", mc);
for(i=0;i<mc;i++)
NSLog(@"Method no #%d: %s", i, sel_getName(method_getName(mlist[i])));
free(mlist);
}
- , . , Cocoa . UIViewController .
, Obj-C runtime ! id<NGSAuthProvider> NGSFacebookAuthProvider , .
:
, , . :
@implementation NGSObjectionModule
- (void)configure
{
[self bind:[[[NGSFacebookAuthProvider alloc] init] autorelease] toProtocol:@protocol(NGSAuthProvider)];
}
@end
, , Objection, , ?