Swiftlet Utilities

Swiftlet Utilities provides several useful functions that are common across many apps written in SwiftUI and Xcode. These include features such as testing to see if an app can connect to the internet, etc.
NOTE: Swiftlet Utilities is a replacement for our Action Utilities library specifically designed to work with SwiftUI. Several other features have been modernized and improved as well.
Additionally, many built-in types (such as Color
, String
and Data
) have been extended with useful features such as converting a color to and from a hex string and moving images easily between Data
and String
types to support Swift’s Codable
protocol.
Swiftlet Utilities are support on iOS, tvOS, watchOS and macOS.
Installation
Swift Package Manager (Xcode 11 and above)
- Select File > Swift Packages > Add Package Dependency… from the File menu.
- Paste
https://github.com/Appracatappra/SwiftletUtilities.git
in the dialog box.
- Follow the Xcode’s instruction to complete the installation.
Why not CocoaPods, or Carthage, or blank?
Supporting multiple dependency managers makes maintaining a library exponentially more complicated and time consuming.
Since, the Swift Package Manager is integrated with Xcode 11 (and greater), it’s the easiest choice to support going further.
General Extensions
General Utilities provide several, general utility classes to handle thing such as network connectivity.
Provided Extensions
The following extensions are provided:
- HTMLEntity - Decodes the given string converting any HTML Entity codes into their resulting characters.
- Reachability - Test to see if we have an active network connection.
Hardware Extensions
Hardware Utilities provide commonly used information about the device an app is running on such as the device type (for example iPhoneX
), the OS version (for example iOS 11.1
) and if the app can connect to the internet.
Provided Extensions
The following extensions are provided:
- AppleHardwareType - Used to convert an Apple device model name (in the form
iPhone10,3
) to a human readable form (such as iPhoneX
). This enum works with the HardwareInformation
class to get the type of device the app is running on.
- HardwareInformation - Defines a set of convenience properties and functions when working on Apple devices, such as checking the device model name (
iPhone10,3
), getting the device type (iPhoneX
), getting the OS version (iOS 11.1
), the current device orientation and internet connection state:
- isPhone - Returns
true
if the app is running on an iPhone, else returns false
.
- isPad - Returns
true
if the app is running on an iPad, else returns false
.
- isTV - Returns
true
if the app is running on an Apple TV, else returns false
.
- isCar - Returns
true
if the app is running on a CarPlay connected device, else returns false
.
- isWatch - Returns
true
if the app is running on an Apple Watch, else returns false
.
- isMac - Returns
true
if the app is running on a Mac laptop or desktop, else returns false
.
- modelName - Returns the model name of the device the app is running on. For example,
iPhone10,3
or iPhone10,6
for the iPhone X
.
- deviceType - Returns the human-readable type of the device that the app is running on (for example
iPhoneX
) or unknown
if the type cannot be discovered.
- osVersion - Returns the version number (for example
iOS 11.1
) of the OS installed on the device that the app is running on.
Swiftlet Extensions
Swiftlet Extensions provide several useful features to common, built-in SwiftUI and Swift data types such as converting Color
to/from hex strings (for example #FF0000
), creating Image
instances from Base64 encoded strings stored in Data
objects and encoding Images
as Base64 strings or Data
objects.
SwiftUI Examples:
// Assign a color from a string
let color: Color ~= "#FF0000"
// Initialize a color from a hex string
let green = Color(fromHex: "00FF00")
// Convert color to a hex string
let white = Color.white.toHex()
Provided Extensions
The following extensions are provided:
- Color - Extends
Color
to support the Action Data controls and adds convenience methods for working with colors in a Codable
, Encodable
or Decodable
class.
- Data - Extends
Data
to support the Action Data controls and adds convenience methods for working with data properties in a Codable
, Encodable
or Decodable
class.
- Image - Extends
Image
to support the Action Data controls and adds convenience methods for working with image properties in a Codable
, Encodable
or Decodable
class.
- String - Extends
String
to support the Action Data controls and adds convenience methods for working with Image
and Color
properties in a Codable
, Encodable
or Decodable
class.
- Array - Extends
Array
with new functions for working with collections.
- Double - Extends
Double
with several useful features.
- Int - Extends
Int
with several useful features.
- @Published - Allows properties that are marked
@Published
and allows them to be Codable
with Encode
and Decode
conformance.
- View - Conditionally apply modifiers depending on the target operating system.
Swiftlet Utilities
Swiftlet Utilities provides several useful functions that are common across many apps written in SwiftUI and Xcode. These include features such as testing to see if an app can connect to the internet, etc.
Additionally, many built-in types (such as
Color
,String
andData
) have been extended with useful features such as converting a color to and from a hex string and moving images easily betweenData
andString
types to support Swift’sCodable
protocol.Swiftlet Utilities are support on iOS, tvOS, watchOS and macOS.
Installation
Swift Package Manager (Xcode 11 and above)
https://github.com/Appracatappra/SwiftletUtilities.git
in the dialog box.Supporting multiple dependency managers makes maintaining a library exponentially more complicated and time consuming.
Since, the Swift Package Manager is integrated with Xcode 11 (and greater), it’s the easiest choice to support going further.
General Extensions
General Utilities provide several, general utility classes to handle thing such as network connectivity.
Provided Extensions
The following extensions are provided:
Hardware Extensions
Hardware Utilities provide commonly used information about the device an app is running on such as the device type (for example
iPhoneX
), the OS version (for exampleiOS 11.1
) and if the app can connect to the internet.Provided Extensions
The following extensions are provided:
iPhone10,3
) to a human readable form (such asiPhoneX
). This enum works with theHardwareInformation
class to get the type of device the app is running on.iPhone10,3
), getting the device type (iPhoneX
), getting the OS version (iOS 11.1
), the current device orientation and internet connection state:true
if the app is running on an iPhone, else returnsfalse
.true
if the app is running on an iPad, else returnsfalse
.true
if the app is running on an Apple TV, else returnsfalse
.true
if the app is running on a CarPlay connected device, else returnsfalse
.true
if the app is running on an Apple Watch, else returnsfalse
.true
if the app is running on a Mac laptop or desktop, else returnsfalse
.iPhone10,3
oriPhone10,6
for theiPhone X
.iPhoneX
) orunknown
if the type cannot be discovered.iOS 11.1
) of the OS installed on the device that the app is running on.Swiftlet Extensions
Swiftlet Extensions provide several useful features to common, built-in SwiftUI and Swift data types such as converting
Color
to/from hex strings (for example#FF0000
), creatingImage
instances from Base64 encoded strings stored inData
objects and encodingImages
as Base64 strings orData
objects.SwiftUI Examples:
Provided Extensions
The following extensions are provided:
Color
to support the Action Data controls and adds convenience methods for working with colors in aCodable
,Encodable
orDecodable
class.Data
to support the Action Data controls and adds convenience methods for working with data properties in aCodable
,Encodable
orDecodable
class.Image
to support the Action Data controls and adds convenience methods for working with image properties in aCodable
,Encodable
orDecodable
class.String
to support the Action Data controls and adds convenience methods for working withImage
andColor
properties in aCodable
,Encodable
orDecodable
class.Array
with new functions for working with collections.Double
with several useful features.Int
with several useful features.@Published
and allows them to beCodable
withEncode
andDecode
conformance.