Drivit 8.1.2-alpha2

Drivit 8.1.2-alpha2

Alan Ostanik维护。



 
依赖
Sentry= 8.22.4
Firebase/Messaging= 10.23.1
 

Drivit 8.1.2-alpha2

  • Adão Rodrigues

Drivit

Version CocoaPods Swift 5 Platform

这是一个示例项目,概述了将Drivit iOS SDK集成到您的应用程序中的关键步骤。如果您有任何疑问,请随时通过[email protected]联系我们。

入门

使用SDK

要求

  • iOS 10.0+

以下表格显示了您应使用哪个版本的Drivit来为您的Swift版本

Xcode功能 Swift Drivit
12 5 4.3.1 - 4.6.0
11 5 3.3.0 - 4.2.0
10 4 1.0.0 - 3.2.0

Xcode功能

将以下功能添加到您的Xcode项目中

  • 后台模式
    • 位置更新 - 允许我们在后台运行位置更新
    • 远程通知 - 允许我们处理后台更新通知
    • 后台获取 - 允许我们定期在后台运行,以便更新其内容
    • 后台处理 - 允许我们定期在后台运行,以便执行一些操作
  • 推送通知

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令进行安装

$ gem install cocoapods

要使用 CocoaPods 将 Drivit 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'

target '<Your Target Name>' do
    pod 'Drivit', '~> 4.6.0'
end

然后,运行以下命令

$ pod install

使用方法

1. 在 Plist 源代码中添加 Drivit API 密钥

将以下代码片段添加到您的 app 的 Plist 文件中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<key>DRIVIT_API_KEY</key>
<string>YOUR_API_KEY</string>
...
</dict>
</plist>

2. 为您的应用代理做准备

首先,让我们将以下代码添加到您的 App Delegate 中,以便 Drivit 能够知道应用为何启动(如果有),例如通知和位置更新

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
	Drivit.default.register(withOptions: launchOptions)

	return true
}

3. 允许 SDK 在后台执行操作

然后,我们需要设置后台应用刷新。这将允许 SDK 定期在后台运行,以便更新其内容。

3.1. iOS13及以上:启用后台任务

要启用后台任务,请在您的App plist文件中包含以下代码

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
	<key>BGTaskSchedulerPermittedIdentifiers</key>
	<array>
		<string>com.drivit.core.task.sync</string>
		<string>com.drivit.core.task.refresh</string>
	</array>
...
</dict>
</plist>

请注意,将BGTaskSchedulerPermittedIdentifiers键添加到Info.plist中会在iOS 13及更高版本中禁用application(_:performFetchWithCompletionHandler:)setMinimumBackgroundFetchInterval(_:)

3.2. iOS12及以下:启用后台获取(已弃用)

要启用后台获取,请在您的App Delegate中包含以下代码

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
	Drivit.default.performFetch { (result) in
		switch result {
			case .newData:
				completionHandler(.newData)
			case .noData:
				completionHandler(.noData)
			case .failed:
				completionHandler(.failed)
		}
	}
}

3.3. 启用后台传输服务

要启用后台传输服务,请在您的App Delegate中包含以下代码

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        Drivit.default.handleEventsForBackgroundURLSession(withIdentifier: identifier, completionHandler: completionHandler)
    }

4. 推送通知

为了保持内容更新,注册设备令牌并订阅推送通知非常重要。这样,每当我们有一些新的内容/数据准备改进用户体验时,我们可以让应用程序知道

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
	Drivit.default.registerForRemoteNotifications(withDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
	Drivit.default.didReceiveRemoteNotification(userInfo: userInfo, completionHandler: completionHandler)
}

5. 登录/注册您的用户

现在我们已具备了所有要件,在开始记录行程之前,我们需要将用户登录到SDK中。为此,请创建DILoginDISignup实例,并向其提供用户信息

let regular = DISignup.regular(email: "email", password: "password",
															 firstName: "first", lastName: "last")
// OR
let advance = DISignup.advance(secret: "secret")

Drivit.authentication.signup(type: regular) { result in                
	switch(result) {
		case let .success(user): 
			print("Welcome " + user.firstName)
		case let .failure(error): 
			print("An error ocurred: " + error.localizedDescription)
	}
}
let regular = DILogin.regular(email: "email", password: "password")
// OR
let advance = DILogin.advance(secret: "secret")

Drivit.authentication.login(type: advance) { result in                
	switch(result) {
		case let .success(user): 
			print("Welcome " + user.firstName)
		case let .failure(error): 
			print("An error ocurred: " + error.localizedDescription)
	}
}

6. 添加相关的Google Maps API密钥

为了提供更好的用户体验,Drivit使用Google API。请根据您的架构灵活设置此密钥

Drivit.settings.googleAPIKey = "YOU_API_KEY"

7. 为用户添加车辆

所有用户都必须有一个关联的车辆才能进行行程。请将此车辆提供给SDK,以便记录行程

let vehicle = Drivit.user.createVehicle(guid: "195258", year: 2020)

Drivit.user.add(vehicleDetails: vehicle) { (result) in
	switch result {
		case .success:
			print("Vehicle added successfully")
		case .failure:
			print("Add vehicle request has failed!")
	}
}

就是这些!祝您行程安全!

Drivit团队

文档

您可以在以下位置查看完整的参考文档 这里