A collection of Swift functions, extensions, and SwiftUI and UIKit Views.
Legend:
- πΈ UIKit
- πΉ SwiftUI
- πΊ Shared
- In XCode 12 go to
File -> Swift Packages -> Add Package Dependencyor in XCode 13File -> Add Packages - Paste in the repo's url:
https://github.com/JemAlvarez/JsHelper
import JsHelperimport Onboarder// Without margins
yourSubview.addConstraints(equalTo: yourParentView)
// With margins
yourSubview.addConstraintsWithMargins(equalTo: yourParentView)- Add the following code in SceneDelegate
- Follow steps in
makeWindowfunction documnetation
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let window = makeWindow(for: scene, with: ViewController()) {
self.window = window
}
}UIColor.blue.getHexString()UIColor(hex: "#000000")// From
var view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
// To
@TAMIC var view = UIView()Color.blue.getHexString()Color(hex: "#000000")if let window = UIApplication.shared.currentUIWindow() {
// handle ui window
}Button("Present Sheet") {
presentShareSheet(with: ["Strings", Images, URLs])
}Button("Request Review") {
requestReview()
}@Environment(\.openURL) var openURL
Button("Open Link") {
// Takes in an `OpenURLAction` doesn't have to be `openURL`
openLink("https://apple.com", with: openURL)
}yourView
.cornerRadius(50, corners: [.bottomLeft, .topRight])yourView
.getViewSize { width, height in
// Now you can add your width and height to your state and use it
}Button("Haptic") {
// Default HapticStyle is `soft`
haptic()
// Other HapticStyles: light, medium, heavy, rigid, soft, success, error, warning, selection
haptic(.heavy)
}ProgressView("Progress", value: 69, total: 420)
.progressViewStyle(
GaugeProgressViewStyle(
strokeColor: .red,
strokeWidth: 15,
size: (50, 50)
// backgroundColor: Color? = nil
)
)@State var showing = false
var body: some View {
ZStack {
// iOS 14 - Color for background
AlertBubbleView14(
showing: $showing,
title: "Title",
subtitle: "Subtitle",
systemImage: "heart.fill",
background: .red
)
// iOS 15 - ShapeStyle for background
AlertBubbleView(
showing: $showing,
title: "Title",
subtitle: "Subtitle",
systemImage: "heart.fill",
background: .ultraThinMaterial
)
}
}@State var emoji = "π"
var body: some View {
EmojiPickerView(emoji: $emoji)
}// UIImage picked from user
@State var image: UIImage? = nil
// Presenting Image Picker
@State var showingPicker = false
var body: some View {
VStack{
// unwrap image and display
if let image = image {
Image(uiImage: image)
.resizable()
}
// show image picker
Button("Pick photo") {
showingPicker.toggle()
}
}
// image picker sheet
.sheet(isPresented: $showingPicker) {
ImagePickerView(image: $image)
}
}// Preset formats
Date().getString(with: .commaWithWeekday)
// Or custom formats
Date().getString(with: "MMMM yyyy")Date().numberOfDays(until: anotherDate)"your date in string".toDate(with: "MM/dd/yyyy")
// Or use the Date.Formats
"your date in string".toDate(with: Date.Formats.slashes.rawValue)"emoji π".hasEmoji()error.printError(for: "Your label")
// Output
// Error occurred: Your label
// Localized error description
// Full error// iOS 15 Product
yourProduct.localizedPrice ?? "optional string"
// SKProduct
yourSKProduct.localizedPrice ?? "optional string"UserDefaults.standard.reset(for: ["key1", "key2"])let data = Bundle.main.loadLocalJSON(with: "fileName")URL(string: .rateOnAppStoreLink(with: "yourAppId"))UNUserNotificationCenter.requestPermission(for: [.alert, .badge, .sound]) {
// Handle success
}.onAppear {
// Triggers a notification 14 (your amount of days) days from the last time the user opened the app
// With your given notification content
UNUserNotificationCenter.bringBackUser(with: notificationContent, in: 14)
}// iOS 13+
let urlData: Data? = await URL(string: "yourURLEndpoint")!.requestData()
// Pre-iOS 13
let urlData: Data? = URL(string: "yourURLEndpoint")!.requestData { data in
// Use your data
}// iOS 13+
let decodedURLData: YourType = await URL(string: "yourURLEndpoint")!.requestDataAndDecode()
// Pre-iOS 13
URL(string: "yourURLEndpoint")!.requestDataAndDecode { (decodedData: YourType) in
// Use your data
}UIApplication.requestStoreReview()Bundle.main.appBuild
Bundle.main.appVersionLong
Bundle.main.appVersionShortFor detailed usage information on Onboarder, checkout the the Onboarder repo here.
Default values for the following:
Font Sizes - Includes all predefined fonts in their CGFloat size
.font(.system(size: .body))Opacities - Low, Medium, High
.opacity(.opacityLow)URL - Default unwrapped url
URL(string: "Invalid URL") ?? .defaultURLDate - Current values (day, month, year, hour, min, etc.)
Date.currentYear- SwiftLint.md file has steps on how to setup SwiftLint, usage, and a config file. For more information check here.
- Fonts.md file has a table of the predefined font sizes
- GitIgnore.md file includes a files to ignore in XCode projects
- NavigationControllerPopGesture shows hot to bring back pop gesture after hiding navigation back button
Jem Alvarez β @official_JemAl β contact@jemalvarez.com
Distributed under the MIT license. See LICENSE for more information.