Created
July 11, 2016 15:00
-
-
Save adi-li/7a7f82c98199ce303805dd82d59fcd49 to your computer and use it in GitHub Desktop.
Parse allow invalid certificate in iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ParseClientConfiguration.h | |
@protocol ParseMutableClientConfiguration <NSObject> | |
@property (nonatomic, assign) BOOL allowInvalidCertificate; | |
@end | |
@interface ParseClientConfiguration : NSObject <NSCopying> | |
@property (nonatomic, assign, readonly) BOOL allowInvalidCertificate; | |
@end | |
// ParseClientConfiguration.m | |
@implementation ParseClientConfiguration | |
- (void)setAllowInvalidCertificate:(BOOL)allowInvalidCertificate | |
{ | |
_allowInvalidCertificate = allowInvalidCertificate; | |
} | |
- (BOOL)isEqual:(id)object { | |
if (![object isKindOfClass:[ParseClientConfiguration class]]) { | |
return NO; | |
} | |
ParseClientConfiguration *other = object; | |
return ([PFObjectUtilities isObject:self.applicationId equalToObject:other.applicationId] && | |
[PFObjectUtilities isObject:self.clientKey equalToObject:other.clientKey] && | |
[self.server isEqualToString:other.server] && | |
self.localDatastoreEnabled == other.localDatastoreEnabled && | |
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] && | |
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] && | |
self.networkRetryAttempts == other.networkRetryAttempts && | |
self.allowInvalidCertificate == other.allowInvalidCertificate); | |
} | |
- (instancetype)copyWithZone:(NSZone *)zone { | |
return [ParseClientConfiguration configurationWithBlock:^(ParseClientConfiguration *configuration) { | |
// Use direct assignment to skip over all of the assertions that may fail if we're not fully initialized yet. | |
configuration->_applicationId = [self->_applicationId copy]; | |
configuration->_clientKey = [self->_clientKey copy]; | |
configuration->_server = [self.server copy]; | |
configuration->_localDatastoreEnabled = self->_localDatastoreEnabled; | |
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy]; | |
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy]; | |
configuration->_networkRetryAttempts = self->_networkRetryAttempts; | |
configuration->_allowInvalidCertificate = self->_allowInvalidCertificate; | |
}]; | |
} | |
@end | |
// PFURLSession.m | |
#import "Parse.h" | |
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler | |
{ | |
ParseClientConfiguration *config = [Parse currentConfiguration]; | |
if (config.allowInvalidCertificate) { | |
NSURLCredential *cred = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; | |
completionHandler(NSURLSessionAuthChallengeUseCredential, cred); | |
} else { | |
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: