Skip to content

Instantly share code, notes, and snippets.

View henning-jh's full-sized avatar

Henning Hoffmann henning-jh

View GitHub Profile

Keybase proof

I hereby claim:

  • I am henning-jh on github.
  • I am henningh (https://keybase.io/henningh) on keybase.
  • I have a public key ASAmRDGCvjc8UVEqP98qf_-JdHy8LLdSl1AQzjusIHO6rwo

To claim this, I am signing this object:

@henning-jh
henning-jh / *.swift
Last active June 7, 2019 20:38
Sign In with Apple: notifications
let center = NotificationCenter.default
let name = NSNotification.Name.ASAuthorizationAppleIDProviderCredentialRevoked
let observer = center.addObserver(forName: name, object: nil, queue: nil) { (notification) in
// sign user out
// optionally guide them to sign in again
}
@henning-jh
henning-jh / *.swift
Last active June 7, 2019 20:38
Sign In with Apple: check login status
let userIdentifier = // get saved userIdentifier - sample app uses Keychain
appleIDProvider.getCredentialState(forUserID: userIdentifier) { (credentialState, error) in
switch credentialState {
case .authorized:
// The Apple ID credential is valid.
break
case .revoked:
// The Apple ID credential is revoked.
break
case .notFound:
@henning-jh
henning-jh / *.swift
Last active June 7, 2019 20:39
Sign In with Apple: handle login result
extension MyLoginViewController: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController,
didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
let userIdentifier = appleIDCredential.user
let fullName = appleIDCredential.fullName
let email = appleIDCredential.email
// Now we have the account information.
@henning-jh
henning-jh / *.swift
Last active June 7, 2019 20:39
Sign In with Apple: error handling
extension MyLoginViewController: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController,
didCompleteWithError error: Error) {
// Handle error.
}
}
@henning-jh
henning-jh / *.swift
Last active November 29, 2022 20:42
Sign In with Apple: presentation anchor
extension MyLoginViewController: ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}
@henning-jh
henning-jh / *.swift
Last active June 7, 2019 20:40
Sign In with Apple: initial view login check
/// Prompts the user if an existing iCloud Keychain credential or Apple ID credential is found.
func performExistingAccountSetupFlows() {
// Prepare requests for both Apple ID and password providers.
let requests = [ASAuthorizationAppleIDProvider().createRequest(),
ASAuthorizationPasswordProvider().createRequest()]
// Create an authorization controller with the given requests.
let authorizationController = ASAuthorizationController(authorizationRequests: requests)
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
@henning-jh
henning-jh / *.swift
Last active June 7, 2019 20:40
Sign In with Apple: button press
@objc
func handleAuthorizationAppleIDButtonPress() {
let request = ASAuthorizationAppleIDProvider().createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController =
ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
@henning-jh
henning-jh / *.swift
Last active June 7, 2019 20:40
Sign In with Apple: create button
import AuthenticationServices
func createButton() {
let authorizationButton = ASAuthorizationAppleIDButton()
authorizationButton.addTarget(self, action:
#selector(handleAuthorizationAppleIDButtonPress),
for: .touchUpInside)
myView.addSubview(authorizationButton)
}
@henning-jh
henning-jh / collectionview.md
Last active December 9, 2018 08:53
Basic UICollectionView with evenly spaced items.

Just as a reference, here's a UICollectionView with some nice evenly spaced cells.

class ViewController: UIViewController, UICollectionViewDataSource /*, UICollectionViewDelegate */ {

	let kReuseIdentifier = "CollectionCellID"
	let kNumCellsPerRow:CGFloat = 3.0
	let kSpacer:CGFloat = 10.0
	var collectionView : UICollectionView!