Onfleet Driver SDK allows you to use Onfleet services directly in your iOS app.
- iOS 13+
- Swift 5
- Xcode 12.5+
- Onfleet application key
This repository contains auto-generated documentation. See onfleet-driver-sdk/Docs/index.html.
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website.
To integrate Onfleet Driver SDK into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'OnfleetDriver', :git => 'https://github.com/onfleet/ios-driver-sdk.git'Simply drag and drop OnfleetDriver.xcframework into your project and make sure it's linked and embeded with signing into your application's target.
Unfortunatelly we don't currently support SPM or Carthage, but we will do so in the near future.
Import SDK into the source code where needed
import OnfleetDriverOnfleet SDK should be initialized at the earliest convenience, ideally in application(_:didFinishLaunchingWithOptions:) function.
For example in your app delegate file:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//initiate SDK
let config = ApplicationConfig(appKey: "<#app key here#>", appVersion: "<#App version here#>", appName: "<#App name here#>")
driver.initSDK(with: config, environment: .production(useApnSandbox: true), app: application, loggers: [OSLogDestination(logSeverity: .warning)])
return true
}When SDK is initialized it automatically registers for remote notifications. Host app is however responsible for managing push notifications and delivering them to the SDK through methods defined in DriverContext class. If push notifications will not be forwarded several features will stop working (including device verification) or will not work as expected.
For example in your app delegate file update code below initialization like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//initiate SDK
//enable push notification
UNUserNotificationCenter.current().delegate = self
center.requestAuthorization(options: [.alert, .sound]) { granted, error in
print("user notification alerts and sound: \(granted ? "granted" : "denied")")
}
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//forward device token into SDK
driver.setRemoteNotificationsDeviceToken(deviceToken)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
//forward push notification userInfo into SDK if possible
if driver.canHandleRemoteNotification(userInfo: userInfo) {
driver.handleRemoteNotification(userInfo: userInfo)
} else {
// process non-onfleet notifications
}
}
// please note that also user notifications (alerts/banners) must be forwarded into SDK from UNUserNotificationCenterDelegate's method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if driver.canHandleRemoteNotification(userInfo: notification.request.content.userInfo) {
driver.handleRemoteNotification(notification, completionHandler: completionHandler)
} else {
// provide logic for non-onfleet banners
completionHandler([.alert, .sound])
}
}Please refer to Sample app for full integration example.
Apps powered by Onfleet SDK require collecting user location while the driver is on duty. Often iOS system kills backgrounded apps due to memory preassure so in case of background termination app must be woken up on the backgorund asap and continue collecting locations. This is achieved by combination of multiple techniques especially background location updates, silent push notifications and correct location permissions granted by user.
To achieve correct results pls enable in your Xcode project under your app schemes following background mode capabilities:
- Location Updates
- Remote Notifications
- Background Fetch
Following location permissions are required:
- Location -> Allow location access -> Always
- Precise Location -> On This will be referred as full location permissions going forward.
Following privacy description must be set in Info.plist
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Onfleet only tracks your location when on-duty in order to provide analytics and dispatch work to you.</string>
Unfortunatelly, current iOS permissions policy does not open apps in the background if location access is set to While in Use or Once. This requires apps to ask full location permissions. Asking this is sensitive and it is up to SDK integrator to design a flow that is suitable for their users.
Please follow these rules when implementing your own flow:
- enforce "Always" no sooner than when going on duty. Don't allow going on duty unless full location permissions are granted.
- if driver revokes full location permissions while on duty make sure she goes either off duty or grants full location permissions.
Good practice is to verify permissions before driver attempts to go on duty and present a screen with reasons why these permissions are required. Then app should request access when in use or always, then guide user to open Settings app and manually select location access Always. If precise location is off, similar flow should ask for precise location to be on.
Here are reasons why Onfleet requires full location permissions:
- dispatchers can see drivers in real time on web Dashboard
- delivery recipients can see drivers in real time
- task delivery ETA is continually recalculated to provide better precisions to recipients
- locations are used for driver analytics, especially distance driven. Some drivers may be paid by according to this data so precision must be as accurate as possible.
- SDK never tracks driver's location when off duty
- system can open suspended application on background so it can continue sending locations to our server
Protocol in LocationManaging.swift provides a conveniece wrapper for observing location permissions. Our Sample app contains an example flow.
Please refer to Sample app for full integration example.
Due to security reasons each device must be verified. Device Provision is performed only once and the process is done automatically first time the driver logs in. Please note, that push notifications are used to deliver tokens securely therefore push notifications must be set up properly.
Because of push notifications don't work in iOS simulators (not true starting macos Ventura) we must bypass apn servers and deliver tokens manually. This can be done exclusively on iOS simulators using provision.sh script. Please consult the documentation of the script for more details simply by running the script in Terminal.
Please note that bypassing apn servers with the provision script will work only for test accounts. These are Onfleet organizations with special flag. The following criteria must be met before an org can be flagged as test account:
- Test drivers must exist in a single organization for which the sdk/application id above is provisioned.
- If an account is test account and the driver is added to multiple organizations, the account no longer qualifies as a test account.
- Similarly, if an account is a test account and the driver is removed from that organization, the account no longer qualifies as a test account.
If you meet the criteria above and you wish to flag your driver account as test account, please contact Onfleet support at support@onfleet.com.
This repository contains Sample App project that integrates Driver SDK. It provides an overview to core features and guides on how to use them in code. Following features are currently supported:
- Log in, log out, reset password
- Accepting / rejecting invitations
- Setting duty status
- Fetching data
- Tasks list
- Task detail (claiming, starting, completing tasks)
- clone
onfleet/ios-driver-sdkrepository usinggit clone git@github.com:onfleet/ios-driver-sdk.git - open root directory and install pods using
pod install - open
SampleApp.xcworkspacein Xcode - in
AppDelegate.swiftfile add your Onfleet application_id - in target's Signing & Capabilities update bundle identifier and team, please make sure that push notifications work
- build and run using
SampleAppscheme - when logging in for the first time please perform the Device Provision.
