GeoLogger
示例
用于记录位置事件的包。
安装
GeoLogger 可通过 CocoaPods 获得。要安装它,请简单地将以下行添加到您的 Podfile 中
pod 'GeoLoggerSDK'
将以下内容添加到 info.plist
文件中
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>The app needs both when in use and background location permission</string>
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<dict>
<key>TemporaryAccuracyAuthPurposeKey</key>
<string>This app needs accurate location so it can verify that you're in a supported region.</string>
</dict>
<key>NSLocationUsageDescription</key>
<string>The app needs always location permission</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>The app needs when in use location permission</string>
这些键包括后台、前台和精确位置权限键。您可以根据项目和需求修改这些键的字符串
用法
iOS 和 MacOS
需要额外的步骤来添加后台位置访问。
- 转到您的项目 -> 签名 & 能力 -> + 能力,并添加
后台模式
能力 - 勾选
位置更新
复选框
需求
最低 iOS 构建目标 - iOS14.4
最低 MacOS 构建目标 - MacOS 11.0
实现
在文件顶部添加以下导入
import GeoLogger
- 对于 SwiftUI 应用,将以下设置代码添加到您的
@main
struct
var body: some Scene {
WindowGroup {
ContentView()
}.onAppear {
GeoLogger.setup()
}
}
- 对于基于故事板的 应用,将以下代码添加到
AppDelegate.swift
中的application(_:didFinishLaunchingWithOptions:)
GeoLogger.setup()
下一步
您需要请求用户的位置权限。根据您的应用程序和用例,您可以选择何时何地进行请求。要请求位置权限,请从SDK中调用 requestPermission
方法。
GeoLogger.requestPermission(true, requestTemporaryFullAccuracy: true) { (locationPermissionDenied, locationServicesDisabled) in }
参数
allowBackgroundLocationTracking
:传递true将请求用户的后台权限requestTemporaryFullAccuracy
:传递true将请求用户的临时精确位置权限(如果未提供)
在回调函数中,您将根据位置访问情况获得2个参数。
locationPermissionDenied
:当用户完全拒绝应用程序的位置权限时为truelocationServicesDisabled
:当设备上的位置服务被禁用时为true
您可以使用这些参数来显示警报,并在iOS的情况下打开设置,提示用户向您应用程序授权位置权限。
如果用户已为您的应用程序提供位置权限(即使是最低权限),则不会调用此回调。
最后,调用 log
方法以注册当前位置记录
GeoLogger.log(api: "<Your POST api url>") { (success, retryCount) in }
此方法将创建一个指向提供的URL的POST请求,并发送当前位置坐标以及时间戳。
此库实现了批量上传,并内置了一个持久的 Realm
存储系统,其中存储所有记录,并在时间适合时最多同步3条记录。
如果在出现错误的情况下尝试重新注册日志的次数达到 retryOnErrorCount
次时,SDK将尝试重新注册该日志。
如果日志注册失败,经过 retryOnErrorCount
(或为0)后,日志将从这个存储中删除。
在 callback
中,您将能够监视注册的日志的 success
和 retryCount
。
作者
nishiths23, [email protected]
许可
GeoLogger 在MIT许可下可用。有关更多信息,请参阅LICENSE文件。