PerseusGeoLocationKit — Xcode 10.1+
PerseusGeoLocationKit 同时作为 macOS 和 iOS 应用组件发布。
集成能力
概要
易于处理原生位置服务的工具集合。
特性
- 通过 订阅 通过通知中心发送位置数据。
- 自定义许可计算 以控制位置服务访问。
- PerseusLocationDealer 作为单例。
需求
- macOS 10.13.6+
- Xcode 10.1+
- Swift 4.2+
- iOS: 9.3+, UIKit SDK
- macOS: 10.9+, AppKit SDK
第一方软件
第三方软件
- SwiftLint Shell Script Runner
- SwiftLint / 0.31.0: Busy Laundromat for macOS High Sierra
安装
强烈建议使用“精确”与版本字段一同使用。
独立版
复制文件 PerseusGeoLocationSingle.swift
,然后将其放入主机项目的所需位置。
Carthage
Cartfile 应包含以下内容
github "perseusrealdeal/PerseusGeoLocationKit" == 0.1.0
一些 Carthage 使用技巧放在这里。
CocoaPods
Podfile 应包含以下内容
target "ProjectTarget" do
use_frameworks!
pod 'PerseusGeoLocationKit', '0.1.0'
end
Swift Package Manager
- 作为包依赖,所以 Package.swift 应包含以下声明
dependencies: [
.package(url: "https://github.com/perseusrealdeal/PerseusGeoLocationKit.git",
.exact("0.1.0"))
],
- 作为 Xcode 项目的依赖项
在导航器 > 包依赖 > 添加包依赖项
强烈建议使用“精确”与版本字段一同使用。
使用方法
步骤 1:
准备使用位置服务
Info.plist | iOS | macOS | PerseusLocationDealer 的方法 |
---|---|---|---|
NSLocationUsageDescription | 可选 | askForAuthorization() | |
NSLocationAlwaysUsageDescription | 必需 | askForAuthorization() | |
NSLocationWhenInUseUsageDescription | 必需 | askForAuthorization(.whenInUse) |
仅限 macOS 的建议
PerseusLocationDealer 应在 macOS 的启动时间内加载。
为此,在启动时分配的类(如 AppDelegate)中创建 PerseusLocationDealer 实例的引用作为属性。查看以下示例语句。
class AppDelegate: NSObject, NSApplicationDelegate {
let locationDealer = PerseusLocationDealer.shared // Once only is enough.
func applicationDidFinishLaunching(_ aNotification: Notification) {
...
步骤 2:
创建通知观察者,然后请求一个值
通知名称 | PerseusLocationDealer 的方法 | 值 |
---|---|---|
.locationDealerCurrentNotification | askForCurrentLocation(_ :) | 当前位置 |
.locationDealerUpdatesNotification | askToStartUpdatingLocation(_ :) | 位置变化 |
.locationDealerStatusChangedNotification | askForAuthorization(_ :, _ :) | 权限 |
.locationDealerErrorNotification | 错误 |
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
NotificationCenter.default.addObserver(
self,
selector: #selector(locationDealerCurrentHandler(_:)),
name: .locationDealerCurrentNotification,
object: nil
)
}
@objc private func locationDealerCurrentHandler(_ notification: Notification) {
guard
let result = notification.object as? Result<PerseusLocation, LocationDealerError>
else { return }
switch result {
case .success(let data):
log.message("\(data)")
case .failure(let error):
log.message("\(error)", .error)
}
}
第 3 步:
请求值、授权和当前位置
@IBAction func buttonLocationPermissionTapped(_ sender: NSButton) {
PerseusLocationDealer.shared.askForAuthorization { permit in
let text = "[\(type(of: self))].\(#function) — It's already determined .\(permit)"
log.message(text, .error)
}
}
@IBAction func buttonCurrentLocationTapped(_ sender: NSButton) {
try? PerseusLocationDealer.shared.askForCurrentLocation()
}
MIT 协议
此仓库中所有文件均根据 MIT 协议授出。
版权所有 © 7531 新西伯利亚的米哈伊尔·齐古林。
- 根据斯拉夫日历,年份从世界创世纪开始。
- 斯拉夫年9月1日
LICENSE 了解详细信息。
作者
PerseusGeoLocationKit
是由新西伯利亚的米哈伊尔·齐古林(也就是我)编写的。[email protected].