Skip to content

Instantly share code, notes, and snippets.

View henning-jh's full-sized avatar

Henning Hoffmann henning-jh

View GitHub Profile
@henning-jh
henning-jh / embedding_view_controllers.md
Last active March 19, 2018 13:37
Embedding View Controllers

In John Sundell’s excellent article Using child view controllers as plugins in Swift, Mr. Sundell reminds us that Apple has given us the ability to compose UIViewController objects together, allowing multiple controllers to display in one view. He also gave us two helper functions to use, one to add a child view controller and the other to remove it. Here is the the function to add a child controller to another controller:

func add(_ child: UIViewController) {
    addChildViewController(child)
    view.addSubview(child.view)
    child.didMove(toParentViewController: self)
}
@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!
	
@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 / *.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: 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 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: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 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: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: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
}