A simple library providing device information for iOS apps. Includes services for device info, version info, and launch count info.
Import the module into any file where you need to display, capture, or record device information.
import DeviceInfo
A common need is to make adjustments in your app, or display different content for a user’s first launch of the app or a new version. You can use the LaunchCountService
to accomplish this easily.
import DeviceInfo
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var launchCountService = LaunchCountService()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if launchCountService.launchCountForAllVersions == 0 {
// Show welcome information
} else if launchCountService.launchCountForCurrentVersion == 0 {
// Show update information
}
launchCountService.incrementLaunchCountForCurrentVersion()
return true
}
}
Often you will want to capture basic information about the device and configuration of your users. You can use the DeviceInfoService
to make this simple.
import DeviceInfo
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var deviceInfoService = DeviceInfoService()
var networkAccess = NetworkAccess() // Your custom network access object
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
networkAccess.put("/devices", parameters: deviceInfoService.deviceInfoDictionary())
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let setToTrim = NSCharacterSet( charactersInString: "<>" )
let tokenString = deviceToken.description.stringByTrimmingCharactersInSet(setToTrim).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
networkAccess.put("/devices", parameters: deviceInfoService.deviceInfoDictionary(tokenString))
}
}
Note: This example shows both capturing device information in the application(_:didFinishLaunchingWithOptions:)
function as well as after registering for remote notifications. In a production app, you would typically do one or the other, but not both.
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate DeviceInfo into your Xcode project using Carthage, specify it in your Cartfile
:
github "benjaminsnorris/DeviceInfo" ~> 1.0
Run carthage update
to build the framework and drag the built DeviceInfo.framework
into your Xcode project.
You can use The Swift Package Manager to install DeviceInfo
by adding the proper description to your Package.swift
file:
import PackageDescription
let package = Package(
name: "YOUR_PACKAGE_NAME",
targets: [],
dependencies: [
.Package(url: "https://github.com/benjaminsnorris/DeviceInfo.git", majorVersion: 1)
]
)
Note that the Swift Package Manager is still in early design and development. For more information check out its GitHub Page
If you don't already have a .xcworkspace
for your project, create one. (Here's how)
Open up Terminal, cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:
$ git init
$ git submodule add https://github.com/benjaminsnorris/DeviceInfo.git Vendor/DeviceInfo
Open the new DeviceInfo
folder, and drag the DeviceInfo.xcodeproj
into the Project Navigator of your application's Xcode workspace.
It should not be nested underneath your application's blue project icon. Whether it is above or below your application's project does not matter.
Select DeviceInfo.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target.
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
In the tab bar at the top of that window, open the "General" panel.
Click on the +
button under the "Linked Frameworks and Libraries" section.
Select DeviceInfo.framework
inside the Workspace
folder.
Click on the +
button under the "Embedded Binaries" section.
Select DeviceInfo.framework
nested inside your project.
An extra copy of DeviceInfo.framework
will show up in "Linked Frameworks and Libraries". Delete one of them (it doesn't matter which one).
And that's it!
link |
Stars: 1 |
Last commit: 1 year ago |
Also improve Swift Package Manager support.
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics