3

出于某种原因,我为 facebook 连接开发了静态库。

对于正确的过程授权 URL appDelegate 应该有 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

方法。因此,当我在 appDelegate.mm 文件中对其进行编码时,一切正常,方法调用和 facebook 会话被授权。但我必须将此方法添加到运行时委托,所以我使用以下代码:

{
NSString* methodDescription;

methodDescription = @"B@:@@@@";
UIApplication* app = [UIApplication sharedApplication];

bool res = class_addMethod([app.delegate class],     @selector(application:openURL:sourceApplication:annotation:), (IMP)applicationIMP,     [methodDescription UTF8String]);
NSLog(@"Result of add method is %d", res);
}

//这里是新方法的实现:

 bool applicationIMP(id self, SEL _cmd, UIApplication *application, NSURL *url, NSString     *sourceApplication, id annotation)
 {
NSLog(@"Log from applicationIMP injected method");
return [[LibSocial sharedInstance] FacebookHandleOpenURL:url];
 } 

this code successfully adds method (I see this method when calls class_copyMethodList): 2013-02-04 23:02:00.704 LibSocialTest[38167:19a03] Mathod[0] is application:openURL:sourceApplication:annotation:

But Facebook SDK doesn't fire this method after authentication, and I got FBSessionState FBSessionStateClosedLoginFailed.

Why this method didn't fired?

Update: Even if I replace normally implemented method with custom implementation at runtime, all works well and Facebook SDK fires new method implementation. But if I didn't code normally this method, but add it at runtime, it isn't fires.

Update2 If I add method to appDelegate class before instantiating UIApplication (in main.m file), then injected method works (even there are no default implementation of method in appDelegate.mm file), but if I inject method after UIApplication was created (so instance of appDelegate class was created too), then injection of method doesn't affect on already instantiated instances of class.

4

0 回答 0