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;
}
Related
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
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
I'm using the AWS iOS SDK in my app, and I recently tested a no-network situation. This is my code:
dispatch_queue_t Q = dispatch_queue_create("ec2_instance_fetch", NULL);
dispatch_async(Q, ^{
AmazonEC2Client *client = [[AmazonEC2Client alloc] initWithAccessKey:[ESCredentialsManager accessKey] withSecretKey:[ESCredentialsManager secretKey]];
client.endpoint = [NSString stringWithFormat:#"https://%#", [ESRegionManager endpointForRegion:[ESRegionManager activeRegionObject]]];
EC2DescribeInstancesResponse *response = [client describeInstances:[[EC2DescribeInstancesRequest alloc] init]];
NSMutableArray *temp = #[].mutableCopy;
for (EC2Reservation *reserv in response.reservations) {
for (EC2Instance *instance in reserv.instances) {
[temp addObject:instance];
}
}
self.instances = temp;
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
In a no connection situation, it throws this error:
*** Terminating app due to uncaught exception 'AmazonClientException', reason: 'Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x16e55460 {NSErrorFailingURLStringKey=https://ec2.us-east-1.amazonaws.com/, NSErrorFailingURLKey=https://ec2.us-east-1.amazonaws.com/, NSLocalizedDescription=The Internet connection appears to be offline., NSUnderlyingError=0x16dea2c0 "The Internet connection appears to be offline."}'
*** First throw call stack:
(0x301adf4b 0x3a91c6af 0xbbae1 0xbb31d 0xba6c7 0xba177 0xbd3ab 0xa2bab 0x3adffd1b 0x3ae06273 0x3ae0606b 0x3ae06ce1 0x3ae06f59 0x3af41dbf 0x3af41c84)
libc++abi.dylib: terminating with uncaught exception of type AmazonServiceException
(lldb)
So I tried to simply catch the error like this:
dispatch_queue_t Q = dispatch_queue_create("ec2_instance_fetch", NULL);
dispatch_async(Q, ^{
#try {
AmazonEC2Client *client = [[AmazonEC2Client alloc] initWithAccessKey:[ESCredentialsManager accessKey] withSecretKey:[ESCredentialsManager secretKey]];
client.endpoint = [NSString stringWithFormat:#"https://%#", [ESRegionManager endpointForRegion:[ESRegionManager activeRegionObject]]];
EC2DescribeInstancesResponse *response = [client describeInstances:[[EC2DescribeInstancesRequest alloc] init]];
NSMutableArray *temp = #[].mutableCopy;
for (EC2Reservation *reserv in response.reservations) {
for (EC2Instance *instance in reserv.instances) {
[temp addObject:instance];
}
}
self.instances = temp;
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
#catch (NSError *error) {
NSLog(#"Error: %#", error);
}
});
But it still fails with the same error, and the app crashes. Why can't I catch this error this way? How can I?
Don't catch NSError. Catch NSException.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url=[NSURL URLWithString:share.profilePicUrl];
[_imageview setImageWithURLRequest:[NSURLRequest requestWithURL:url] placeholderImage:
[UIImage imageNamed:#""] success:^(NSURLRequest *request,NSHTTPURLResponse *response,UIImage *image)
{
dispatch_async(dispatch_get_main_queue(), ^{
_imageview.image = image;
});
} failure:^(NSURLRequest *request,NSHTTPURLResponse *response,NSError *error){
}];
});
UIImageView setImageWithURLRequest:placeholderImage:success:failure:]:
unrecognized selector sent to instance 0xb1b71f0
2013-12-12 23:41:00.826 Application[6749:1403] * Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[UIImageView
setImageWithURLRequest:placeholderImage:success:failure:]:
unrecognized selector sent to instance 0xb1b71f0'
Ensure that AFNetworking+UIImageView.m (or more likely, all AFNetworking implementation files) has been added to your target.
Click on AFNetworking+UIImageView.m, and select your target under "Target Membership" in the right window.
See Adding files to separate targets in Xcode 4
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.