Unrecognized selector calling factory method in static iOS library - ios

currently I'm implementing a static library and everything works fine if I use the code "as is" in a test-app, but if I compile my code to a static library, I get an unrecognized selector, here is my code:
+ (id)sharedInstance {
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
static dispatch_once_t pred = 0; \
__strong static id _sharedObject = nil; \
dispatch_once(&pred, ^{ \
_sharedObject = block(); \
}); \
return _sharedObject; \
Calling
[ZanoxTrackingEvent sharedInstance]
results in
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[ZanoxTrackingEvent sharedInstance]: unrecognized selector sent to class 0x27ee8'
I'm pretty desperate right now, I tried several Singleton-implementation.

This error can happen when you have the wrong path in your filesystem. This is what happened in my case.

Related

Can not find selector when exchange method

I come across a problem when trying to exchange method:
#implementation LoginViewModel
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method fromMethod = class_getClassMethod([NSURL class], #selector(URLWithString:));
Method toMethod = class_getClassMethod([self class], #selector(TempURLWithString:));
method_exchangeImplementations(fromMethod, toMethod);
}
});
}
+ (NSURL *)TempURLWithString:(NSString *)URLString {
NSLog(#"url: %#", URLString);
return [LoginViewModel TempURLWithString:URLString];
}
When calling [NSURL URLWithString:], I successfully get the parameter in the exchanged method TempURLWithString:. But it crashed when returning the result from the original implementation:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[LoginViewModel
URLWithString:relativeToURL:]: unrecognized selector sent to class
0x10625ff80'
What I want to do is modifying the url String when init NSURL, any one can give me some help, thanks!
The implementation of +[NSURL URLWithString:] is basically the following:
+ (NSURL *)URLWithString:(NSString *)string {
return [self URLWithString:string relativeToURL:nil];
}
The important thing to note here is that the self refers to the NSURL class.
However, when you call [LoginViewModel TempURLWithString:URLString], the self in the original URLWithString: method is now a reference to the LoginViewModel class, meaning that when the original implementation calls [self URLWithString:string relativeToURL:nil], that call gets dispatched to +[LoginViewModel URLWithString:relativeToURL:], which doesn't exist (hence the exception).
You can fix this by also adding a stub for URLWithString:relativeToURL to your class which just forwards the call to +[NSURL URLWithString:relativeToURL:]:
#implementation LoginViewModel
+ (NSURL *)URLWithString:(NSString *)string relativeToURL:(nullable NSURL *)relativeURL {
return [NSURL URLWithString:string relativeToURL:relativeURL];
}
#end

session invalidated with iOS 8 share extension

I have two targets in my workspace an iOS App (Bundle id: com.gotit.iphone.gotit, App Group: group.com.gotit.iphone.gotit) and an share extension (Bundle id: group.com.gotit.iphone.gotit.Got-it, App Group: group.com.gotit.iphone.gotit).
I can use my share extension without problem but sometimes (not all the time) when I directly come back to my iOS app I have this error and my app crashes :
Attempted to create a task in a session that has been invalidated
2015-07-22 16:31:21.999 iphone[6095:717111] *** Assertion failure in -[BDBOAuth1SessionManager setDelegate:forTask:], /Users/gautier/Documents/Dev/gotit/iOS/iPhone/trunk/iphone/Pods/AFNetworking/AFNetworking/AFURLSessionManager.m:449
2015-07-22 16:31:22.001 iphone[6095:717111] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: task'
*** First throw call stack:
(0x1865a02d8 0x1982140e4 0x1865a0198 0x187454ed4 0x100547340 0x1005476d8 0x100549190 0x100522084 0x100520a38 0x1000c94a8 0x1000d9f9c 0x1000d9d8c 0x18b02ff08 0x18b1bb2bc 0x18b0cde84 0x18b0cdc50 0x18b0cdbd0 0x18b0156f4 0x18a951db8 0x18a94c820 0x18a94c6c4 0x18a94be58 0x18a94bbd8 0x18a945300 0x1865582a4 0x186555230 0x186555610 0x1864812d4 0x18fedf6fc 0x18b07ef40 0x1000d6fac 0x1988bea08)
libc++abi.dylib: terminating with uncaught exception of type NSException
I use AFNetworking (with BDBOAuth1SessionManager) and this is how I create my networkManager :
+ (instancetype)create {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[[self class] alloc] init];
});
return _sharedClient;
}
+ (instancetype)sharedClient {
if (!_sharedClient) {
[GotItClient create];
}
return _sharedClient;
}
- (id)init {
self = [super init];
if (self) {
NSURL *baseURL = [NSURL URLWithString:kSCGotItClientAPIURL];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:#"group.com.gotit.iphone.gotit"];
sessionConfiguration.sharedContainerIdentifier = #"group.com.gotit.iphone.gotit";
_networkManager = [[BDBOAuth1SessionManager alloc]
initWithBaseURL:baseURL
sessionConfiguration:sessionConfiguration
consumerKey:kSCGotItClientKey
consumerSecret:kSCGotItClientSecret
shareDefaultGroupId:#"group.com.gotit.iphone.gotit"];
_networkManager.responseSerializer = [[AFJSONResponseSerializer alloc] init];
//4 operations at same time
[[_networkManager operationQueue] setMaxConcurrentOperationCount:20];
}
return self;
}
Tell me if you need more code

iOS - AFNetworking 2 Dispatch_once

I've just updated Afnetworking 1 to 2.1 and app started to crash. there was no problem at AF 1.0. How can i solve this problem?
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[API initWithBaseURL:]: unrecognized selector sent to instance 0xa3e6810'
API.m
+ (API*)sharedInstance {
static API *sharedInstace = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstace = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
sharedInstace.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
});
return sharedInstace;
}

BlackRaccoon while downloading a file giving error: unrecognized selector sent to instance 0xcd609f0

I am using BlackRaccoon to download a file from FTP server. Following is the code:
- (IBAction)download:(id)sender {
downloadData = [NSMutableData dataWithCapacity: 1];
downloadFile = [[BRRequestDownload alloc] initWithDelegate: self];
downloadFile.path = #"psnewsletter.pdf";
downloadFile.hostname = #"server";
downloadFile.username = #"user";
downloadFile.password = #"pswd";
[downloadFile start];
}
Its gives following error
2014-02-20 11:48:43.526 BlackHillPrimarySchool[2036:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController requestFailed:]: unrecognized selector sent to instance 0xcd609f0'
Any help will be appreciated.
requestFailed: is one of the BRRequest delegate method that you must implement in your view controller.
- (void)requestFailed:(BRRequest *)request {
BRRequestError *reqError = request.error;
// check the error, handle as needed.
}
I'm not sure how your code even compiled. This is a required method of the protocol.

Strange exception Facebook iOS sdk

Request:
- (void) getMyImageWithContestOfFace{
currentApiCall = getMyImageFb;
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"SELECT pic FROM user WHERE uid=me()",#"query",
nil];
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[[delegate facebook] requestWithMethodName:#"fql.query"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
Parsing:
...
case getMyImageFb:{
flag = TRUE;
imageWithFace = [[UIImage alloc] init];
imageWithFace = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:#"pic"]]]];
}
break;
...
When i call it i catch very strange exeption:
*2011-11-10 00:30:30.661 FaceGraph[1699:f803] I have some information
2011-11-10 00:30:30.664 FaceGraph[1699:f803] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6d3d470
2011-11-10 00:30:30.666 FaceGraph[1699:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6d3d470'
First throw call stack:
(0x15d9052 0x176ad0a 0x15daced 0x153ff00 0x153fce2 0x254db 0xb6d6 0xbcf7 0xc9ba59 0xc99e94 0xc9aeb7 0xc99e4f 0xc99fd5 0xbdef6a 0x3b8bbbd 0x3c585ea 0x3b82298 0x3c5816b 0x3b82137 0x15ad97f 0x1510b73 0x1510454 0x150fdb4 0x150fccb 0x14c2879 0x14c293e 0x2a6a9b 0x2332 0x22a5)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language: auto; currently objective-c
No idea why. The most strange that i did it before ABSOLUTELY the same, and it worked fine.
I can't find the difference between my old projects and current. Maybe somebody have the same problem?? If you need more code - ask for it.
thanks
Well, the exception seems to be quite clear: result is probably an array and not a dictionary.
PS: The line imageWithFace = [[UIImage alloc] init]; is totally superfluous given the line that follows it. You should remove it.
I forgot to add:
if ([result isKindOfClass:[NSArray class]]){
result = [result objectAtIndex:0];
}
in result-parsing function.

Resources