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
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { | |
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust else {return} | |
guard let serverTrustInfo = challenge.protectionSpace.serverTrust, | |
let certificate = serverSSLCertificate(serverTrustInfo) else {return} | |
// Either of the below methods "Public key pinning" or "Certificate pinning" can be done | |
// guard let publicKeySha256 = sha256(certificate) else {return} | |
// if pinnedPublicKeyHash == sha256(serverSSlPublicKey) { | |
// completionHandler(.useCredential, URLCredential.init(trust: serverTrustInfo)) | |
// }else{ |
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
func pinnedCertificate() -> SecCertificate? { | |
guard let certPath = Bundle.main.path(forResource: "bitmountn", ofType: "crt") else { return .none } | |
guard let fileData = try? Data(contentsOf: URL(fileURLWithPath: certPath)) else {return .none} | |
let certificate = SecCertificateCreateWithData(.none, fileData as CFData) | |
return certificate | |
} |
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
func sha256(_ certificate: SecCertificate) -> String? { | |
let certData = SecCertificateCopyData(certificate) as Data | |
let certStr = certData.base64EncodedString() | |
return sha256(certStr) | |
} | |
func sha256(_ data: Data) -> Data? { | |
guard let dataBuffer = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil } | |
CC_SHA256((data as NSData).bytes, CC_LONG(data.count), dataBuffer.mutableBytes.assumingMemoryBound(to: UInt8.self)) | |
return dataBuffer as Data | |
} |
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
func publicKey(_ certificate: SecCertificate) -> String? { | |
// Got public key in SecKey format, for equality check we need in Data/String format. | |
// Because our pinned public key hash is in String format. | |
let serverSSLPublicKey: SecKey? = SecCertificateCopyKey(certificate) | |
var publicKeyError: UnsafeMutablePointer<Unmanaged<CFError>?>? | |
guard let serverSSLPublicKeyData = SecKeyCopyExternalRepresentation(serverSSLPublicKey!, publicKeyError ) as Data?, publicKeyError == .none else {return .none} | |
// This is the final public key string. | |
let serverSSLPublicKeyString = serverSSLPublicKeyData.base64EncodedString() | |
return serverSSLPublicKeyString | |
} |
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
func test() { | |
let response = self.verifyMyNumber(num: 5) | |
switch response { | |
case .success(let number): | |
print(number) | |
break | |
case .failure(let error) : | |
print(error.localizedDescription) | |
break |
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
import Foundation | |
class Customer { | |
weak var creditCard: CreditCard? | |
func buyCreditCard(creditCard: CreditCard) { | |
self.creditCard = creditCard | |
} | |
deinit { | |
print("Customer is getting deallocated") | |
} | |
} |
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
//Flat Map | |
let nonFlatArr = [[1],[2,2],[3,3,3],[4,4,4,4]] | |
let flatMapped = nonFlatArr.flatMap { $0 } | |
print("flatMapped \(flatMapped)") | |
//OUTPUT | |
flatMapped [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] |
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
func circularViewWithShadow() { | |
let view = UIView() | |
view.frame = CGRect.init(x: 10, y: 100, width: 100, height: 100) | |
view.backgroundColor = UIColor.blue | |
view.layer.cornerRadius = view.bounds.height/2 | |
view.layer.shadowPath = UIBezierPath.init(roundedRect: view.bounds, cornerRadius: view.layer.cornerRadius).cgPath | |
view.layer.shadowOpacity = 0.5 | |
view.layer.shadowOffset = CGSize.init(width: 0, height: 10) | |
view.layer.masksToBounds = true |
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
func registerPushNotification() { | |
UNUserNotificationCenter.current().getNotificationSettings { settings in | |
if settings.authorizationStatus != .authorized { | |
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound,.badge]) { success, error in | |
print("Permission granted: \(success)") | |
} | |
}else{ | |
UIApplication.shared.registerForRemoteNotifications() | |
} | |
} |
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
#import <Foundation/Foundation.h> | |
NS_ASSUME_NONNULL_BEGIN | |
@interface BlocksEx : NSObject | |
typedef void (^CountBlock)(int); | |
typedef int (^CompletionBlock)(void); | |
-(void)sampleBlock: (void(^)(void)) block; | |
-(void)countBlock: (CountBlock) block; | |
-(void)completionBlock: (CompletionBlock) block; | |
-(void)multiParamBlock: (void(^)(int, float)) block; | |
@end |
NewerOlder