SensingKit-iOS 库
这是一个 iOS 库,为您的应用提供了连续感应功能。更多信息请参阅项目网站。
支持传感器
以下移动传感器目前在 SensingKit-iOS 中得到支持(列在 SKSensorType 枚举中):
- 加速度计
- 陀螺仪
- 磁力计
- 设备运动(感应方位、重力、用户加速度、磁场、旋转)
- 运动活动
- 计步器
- 气压计
- 电池
- 位置
- 航向
- iBeacon™ 邻近性
- Eddystone™ 邻近性
- 麦克风
安装库
您可以使用流行的 Cocoa 项目的依赖管理器 Cocoapods 轻松地安装 SensingKit。以下是安装 Cocoapods 的命令
$ gem install cocoapods
要将 SensingKit 集成到您的 Xcode 项目中,请将其指定在您的 Podfile
中
target <MyApp> do
# Uncomment this line if you're using Swift or would like to use dynamic frameworks
use_frameworks!
pod 'SensingKit'
# For the latest development version, please use:
# pod 'SensingKit', :git => 'https://github.com/SensingKit/SensingKit-iOS.git', :branch => 'next'
end
然后,运行以下命令
$ pod install
有关 CocoaPods 的更多信息,请访问 https://cocoapods.org.cn。
使用库
如下所示导入和初始化 SensingKit
Objective-C
#import <SensingKit/SensingKit.h>
@property (nonatomic, strong) SensingKitLib *sensingKit;
- (void)viewDidLoad {
[super viewDidLoad];
self.sensingKit = [SensingKitLib sharedSensingKitLib];
}
Swift
import SensingKit
let sensingKit = SensingKitLib.shared()
检查设备中是否可用传感器
Objective-C
if ([self.sensingKit isSensorAvailable:Battery]) {
// You can access the sensor
}
Swift
if sensingKit.isSensorAvailable(SKSensorType.Battery) {
// You can access the sensor
}
注册传感器(例如电池传感器),以下为示例
Objective-C
[self.sensingKit registerSensor:Battery error:NULL];
Swift
do {
try sensingKit.register(SKSensorType.Battery)
}
catch {
// Handle error
}
订阅传感器数据处理器。您可以将数据对象转换为实际传感器数据对象以访问所有传感器数据属性
Objective-C
[self.sensingKit subscribeToSensor:Battery
withHandler:^(SKSensorType sensorType, SKSensorData *sensorData, NSError *error) {
if (!error) {
SKBatteryData *batteryData = (SKBatteryData *)sensorData;
NSLog(@"Battery Level: %f", batteryData.level);
}
} error:NULL];
Swift
do {
try sensingkit.subscribe(to: SKSensorType.Battery, withHandler: { (sensorType, sensorData, error) in
if (error != nil) {
let batteryData = sensorData as! SKBatteryData
print("Battery Level: \(batteryData)")
}
})
}
catch {
// Handle error
}
您可以使用以下命令启动和停止连续感测
Objective-C
// Start
[self.sensingKit startContinuousSensingWithSensor:Battery error:NULL];
// Stop
[self.sensingKit stopContinuousSensingWithSensor:Battery error:NULL];
Swift
// Start
do {
try sensingKit.startContinuousSensingWithSensor(SKSensorType.Battery)
}
catch {
// Handle error
}
// Stop
do {
try sensingKit.stopContinuousSensingWithSensor(SKSensorType.Battery)
}
catch {
// Handle error
}
有关我们API的完整描述,请参阅SensingKit网站的帮助页面:https://www.sensingkit.org/documentation/ios/
必需的Info.plist密钥
根据所使用的传感器及其配置,应用文件中应包含一些带有用户友好的描述的密钥info.plist
麦克风
- NSMicrophoneUsageDescription
Eddystone
- NSBluetoothPeripheralUsageDescription
位置
- NSLocationAlwaysUsageDescription
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysAndWhenInUseUsageDescription
运动活动
- NSMotionUsageDescription
许可证
Copyright (c) 2014. Kleomenis Katevas
Kleomenis Katevas, [email protected]
This file is part of SensingKit-iOS library.
For more information, please visit https://www.sensingkit.org
SensingKit-iOS is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SensingKit-iOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SensingKit-iOS. If not, see <https://gnu.ac.cn/licenses/>.
此库可根据 GNU lesser general public license 3.0 许可,允许在您的应用程序中使用此库。
如果您想帮助开源项目,请联系 [email protected]。