Skip to content

Instantly share code, notes, and snippets.

View daljeetseera's full-sized avatar

Daljeet Singh daljeetseera

View GitHub Profile
@daljeetseera
daljeetseera / KeychainWrapper.Swift
Created August 7, 2020 03:35
Wrapper created for keychain
import KeychainSwift
struct KeychainWrapper {
static let shared = KeychainWrapper()
let keychain = KeychainSwift()
func store(key: String, value: String) {
keychain.set(value, forKey: key)
}
@daljeetseera
daljeetseera / AuthorizationState.Swift
Created August 7, 2020 03:31
Check logged in apple id state
func checkAuthorizationState() {
if #available(tvOS 13.0, *) {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let identifier = KeychainWrapper.shared.getValueFor(key: "user")
if identifier.isEmpty {
return
@daljeetseera
daljeetseera / Registration.Swift
Last active August 7, 2020 03:29
Save details to keychain
func registration(credentials: ASAuthorizationAppleIDCredential) {
let emailSuccess = KeychainWrapper.shared.set(key: "email", value: credentials.email!)
let userSuccess = KeychainWrapper.shared.set(key: "user", value: credentials.user)
let name = (credentials.fullName?.givenName ?? "") + (credentials.fullName?.familyName ?? "")
KeychainWrapper.shared.set(key: "name", value: name)
if emailSuccess, userSuccess {
signInSucceeded(emailSuccess)
}
@daljeetseera
daljeetseera / AuthorizationDelegate.Swift
Created August 7, 2020 03:27
Apple Sign in authorization delegate function
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
switch authorization.credential {
case let cred as ASAuthorizationAppleIDCredential:
if cred.email != nil, cred.fullName != nil {
registration(credentials: cred)
} else {
signInWithExistingAccount(credential: cred)
}
@daljeetseera
daljeetseera / SignInApple.Swift
Created August 7, 2020 03:24
Apple Authorization controller request
private func performSignIn(using requests: [ASAuthorizationRequest]) {
appleSignInDelegates = AppleSignInDelegate(window: window) { success in
if success {
// update UI
self.actionState = 1
print("Update UI")
} else {
// show the user an error
print("error in perform signin")
}
func signInTapped() {
let request = ASAuthorizationAppleIDProvider().createRequest()
request.requestedScopes = [.fullName, .email]
performSignIn(using: [request])
}
@daljeetseera
daljeetseera / SessionManger.swift
Last active May 17, 2020 18:17
Alamofire Session Manager
static let sharedManager: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
configuration.timeoutIntervalForRequest = 20
configuration.timeoutIntervalForResource = 20
let manager = Alamofire.SessionManager(configuration: configuration)
let requestRet = NetworkRequestRetrier()
manager.retrier = requestRet
return manager
@daljeetseera
daljeetseera / Alamofire API Manager
Created November 13, 2019 19:59
Alamofire API Manager
import Alamofire
class ApiManager:NSObject
{
private var retriedRequests: [String: Int] = [:]
public typealias successHandler = (Data) -> ()
public typealias failureHandler = (Error) -> ()
static let sharedManager: SessionManager = {
@daljeetseera
daljeetseera / RequestRetrier.swift
Last active July 28, 2021 04:55
Network request retrier for Alamofire
import Alamofire
class NetworkRequestRetrier: RequestRetrier {
let retry = 3 // set the count for number of retries
// [Request url: Number of times retried]
private var retriedRequests: [String: Int] = [:]
func should(_ manager: SessionManager,
@daljeetseera
daljeetseera / PHAssetHelper
Last active August 30, 2019 17:34
I have created an extension of PHAsset in order to get image from asset url or asset.
extension PHAsset {
static let imageSize = CGSize(width: 400, height: 400)
class func getImageFrom(url : String, completion : @escaping (_ image : UIImage?) -> ()) {
// retrieve the list of matching results for your asset url
let assetUrl = URL(string: url)!