Skip to content

Instantly share code, notes, and snippets.

View andresr-dev's full-sized avatar

Andres Raigoza andresr-dev

View GitHub Profile
@andresr-dev
andresr-dev / AccessibilityValueExample.swift
Created April 26, 2022 17:18
This is an example of how you can modify values using accessibility.
import SwiftUI
struct AccessibilityValueExample: View {
@State private var value = 10
var body: some View {
VStack(spacing: 20) {
Text("Value: \(value)")
Button("Increment") {
@andresr-dev
andresr-dev / DifferentiateWithoutColorExample.swift
Created April 26, 2022 17:33
This is an example of how you can support differentiation without color in your applications.
import SwiftUI
struct DifferentiateWithoutColorExample: View {
@Environment(\.accessibilityDifferentiateWithoutColor) var differentiateWithoutColor
var body: some View {
HStack {
if differentiateWithoutColor {
Image(systemName: "checkmark.circle")
}
@andresr-dev
andresr-dev / ReduceTransparencyExample.swift
Created April 26, 2022 17:51
This is an example of how you support reduce transparency accessibility in your applications
import SwiftUI
struct ReduceTransparencyExample: View {
@Environment(\.accessibilityReduceTransparency) var reduceTransparency
var body: some View {
Text("Hello, World!")
.padding()
.background(reduceTransparency ? .black : .black.opacity(0.5))
.foregroundColor(.white)
@andresr-dev
andresr-dev / ImagePicker+UIViewController.swift
Last active November 17, 2022 23:31
This is how you can pick an Image from your photo library in SwiftUI
import PhotosUI
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
@Binding var image: UIImage?
class Coordinator: NSObject, PHPickerViewControllerDelegate {
let parent: ImagePicker
init(_ parent: ImagePicker) {
@andresr-dev
andresr-dev / CameraManager.swift
Created April 27, 2022 15:35
This is how you can open the camera of your iPhone to take a picture from your App
import SwiftUI
struct CameraManager: UIViewControllerRepresentable {
@Binding var image: UIImage?
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let parent: PictureManager
init(_ parent: PictureManager) {
self.parent = parent
@andresr-dev
andresr-dev / CoreImageExample.swift
Last active April 27, 2022 17:43
This is an example of how you can use CoreImage Filters in your SwiftUI Apps
import SwiftUI
import CoreImage
import CoreImage.CIFilterBuiltins
struct CoreImageExample: View {
@State private var image: Image?
// We can have a filter picker in order to make this property dynamic
@State private var currentFilter: CIFilter = CIFilter.sepiaTone()
@andresr-dev
andresr-dev / ImageSaver.swift
Last active April 27, 2022 19:55
This is a handy class that you can use to save an image to your photos library and below there is an example of how to use it. IMPORTANT: You must provide a message in "Privacy - Photo Library Additions Usage Description" of your target info in order to be able to use this class!
import UIKit
class ImageSaver: NSObject {
var successHandler: (() -> Void)?
var errorHandler: ((Error) -> Void)?
func writeToPhotoAlbum(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(saveCompleted), nil)
}
@andresr-dev
andresr-dev / MapExample.swift
Created April 27, 2022 21:24
This is an example of how you can use Map in SwiftUI
import MapKit
import SwiftUI
struct Location: Identifiable {
let id: UUID
var name: String
let latitude: Double
let longitude: Double
var coordinate: CLLocationCoordinate2D {
@andresr-dev
andresr-dev / LocationFetcher.swift
Created April 28, 2022 17:05
This is a class that helps you get the current location of the user and below there's an example of how to use it. IMPORTANT: You must provide a description in target/info/"Privacy - Location When In Use Usage Description"
import CoreLocation
class LocationFetcher: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
var location: ((CLLocation) -> Void)?
var authorizationChanged: ((Bool) -> Void)?
override init() {
@andresr-dev
andresr-dev / OpenSettings.swift
Created April 28, 2022 17:09
This is how you can open settings from your application.
func openSettings() {
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl) { success in
print("Settings opened: \(success)")
}
}
}