我们都有过这样的经历。当你开始编写最新的伟大应用程序时,你需要添加逻辑来提示用户在应用程序可以访问这些资源之前请求权限。许多用户除非你让他们相信你的应用程序值得信赖,否则会拒绝访问。如果他们拒绝访问,你必须试图说服他们去到 iOS 隐私设置中,找到你的应用程序,启用这些权限,然后回到你的应用程序,再次阅读权限,等等...
另一个困难:Apple 框架中的权限使用了许多不同的类,它们在状态方面不共享相同的数据类型。PhotoKit 使用 PHAuthorizationStatus,EventKit 使用 EKAuthorizationStatus,Core Location 使用 CLAuthorizationStatus,依此类推。
VWWPermissionKit 通过向用户展示一个易于阅读的权限列表,他们在iOS提示之前需要批准这些权限来解决这些问题。这使得用户能够简单地理解为什么他们应该批准权限。每个按钮都会触发一次iOS提示,所有提示都是从同一个中心视图中显示的。如果用户更改应用程序隐私设置中的设置,VWWPermissionKit 也会自动检测更改。
虽然是用 Obj-C 编写的,但很容易从 Obj-C 和 Swift 中使用 VWWPermissionKit。为每种语言都包含了一个示例项目。
要开始使用,首先如果您使用 Obj-C,则导入 VWWPermissionKit 到您的文件,如果您使用 Swift,则导入您的 Bridging Header。
#import "VWWPermissionKit.h"
接下来创建一个 VWWPermission 类型的数组并显示权限窗口。一旦所有权限得到授权,表单就会消失,并调用 resultsBlock。在这里您可以检查每个权限。
VWWCameraPermission *camera = [VWWCameraPermission permissionWithLabelText:@"This app lets your record videos, so we need to access your camera"];
VWWPhotosPermission *photos = [VWWPhotosPermission permissionWithLabelText:@"We can save recorded videos to your Photos library."];
VWWCoreLocationAlwaysPermission *locationAlways = [VWWCoreLocationAlwaysPermission permissionWithLabelText:@"For calculating your heading, altitude, speed, distance home, etc... This is a bunch of nonsense text to show that labels will grow with the size of the defined text. This text that you are reading right now. Period."];
NSArray *permissions = @[camera, locationAlways, photos];
[VWWPermissionsManager optionPermissions:permissions
title:@"Welcome to the VWWPermissionKitExample app. Our app uses many of your device's sensors. We'll help you set up some permissions, then get started."
fromViewController:self
resultsBlock:^(NSArray *permissions) {
[permissions enumerateObjectsUsingBlock:^(VWWPermission *permission, NSUInteger idx, BOOL *stop) {
NSLog(@"%@ - %@", permission.type, [permission stringForStatus]);
}];
}];
let photos = VWWPhotosPermission.permissionWithLabelText("In order to write to your Camera Roll")
let camera = VWWCameraPermission.permissionWithLabelText("In order to access your camera to record video.")
let coreLocationAlways = VWWCoreLocationAlwaysPermission.permissionWithLabelText("To calculate your heading, altitude, speed, distance home, etc...")
let permissions = [photos, camera, coreLocationAlways]
VWWPermissionsManager.requirePermissions(permissions, title: "Swift Test", fromViewController: self) { (permissions: [AnyObject]!) -> Void in
print("permission")
}
或者,还有一个只读权限函数,它不显示任何 GUI。它简单读取每个权限类型,它的好处是所有权限都共享相同的数据类型。
[VWWPermissionsManager readPermissions:permissions resultsBlock:^(NSArray *permissions) {
[permissions enumerateObjectsUsingBlock:^(VWWPermission *permission, NSUInteger idx, BOOL *stop) {
NSLog(@"%@ - %@", permission.type, [permission stringForStatus]);
}];
}];
VWWPermissionsManager.readPermissions(permissions) { (permissions:[AnyObject]!) -> Void in
for (_, permission) in permissions.enumerate(){
if permission.status != VWWPermissionStatusAuthorized {
// User didn't authorize this one
}
}
}
点击“隐私”按钮会将用户导航到您的 iOS 应用程序隐私设置,在那里他们可以更改权限。如果用户点击红色按钮,用户也会被导航到这里。一旦用户回到您的应用程序,权限就会被重新读取,屏幕也会更新。
要将 VWWPermissionKit 添加到您的应用中,将 VWWPermissionKit.xcodeproj 项目文件拖动到您的应用的项目中。XCode 将添加 VWWPermissionKit 项目和源文件。
接下来,告诉 XCode 将 VWWPermissionKit 集成到您的应用中。转到您的应用的目标构建设置,并选择“常规”选项卡。在“嵌入的二进制文件”组下,添加 VWWPermissionKit。XCode 应自动为您添加链接引用。
最后,您需要告诉 XCode 在哪里可以找到正确的头文件。转到“构建设置”选项卡。在搜索栏中键入“header ”。然后转到“搜索路径”部分,并将 VWWPermissionKit 的文件路径添加到“头文件搜索路径”(递归:是)。
要使用 CocoaPods 导入此框架,只需在您的 podfile 中添加此行
pod 'VWWPermissionKit', '~> 1.3.0'