VWWPermissionKit 1.3.0

VWWPermissionKit 1.3.0

测试测试
Lang语言 Obj-CObjective C
许可证 MIT
释放最新版本2015年12月

Zakk Hoyt维护。



  • Zakk Hoyt

我们都有过这样的经历。当你开始编写最新的伟大应用程序时,你需要添加逻辑来提示用户在应用程序可以访问这些资源之前请求权限。许多用户除非你让他们相信你的应用程序值得信赖,否则会拒绝访问。如果他们拒绝访问,你必须试图说服他们去到 iOS 隐私设置中,找到你的应用程序,启用这些权限,然后回到你的应用程序,再次阅读权限,等等...

另一个困难:Apple 框架中的权限使用了许多不同的类,它们在状态方面不共享相同的数据类型。PhotoKit 使用 PHAuthorizationStatus,EventKit 使用 EKAuthorizationStatus,Core Location 使用 CLAuthorizationStatus,依此类推。

VWWPermissionKit 通过向用户展示一个易于阅读的权限列表,他们在iOS提示之前需要批准这些权限来解决这些问题。这使得用户能够简单地理解为什么他们应该批准权限。每个按钮都会触发一次iOS提示,所有提示都是从同一个中心视图中显示的。如果用户更改应用程序隐私设置中的设置,VWWPermissionKit 也会自动检测更改。

如何使用 VWWPermissionKit

虽然是用 Obj-C 编写的,但很容易从 Obj-C 和 Swift 中使用 VWWPermissionKit。为每种语言都包含了一个示例项目。

要开始使用,首先如果您使用 Obj-C,则导入 VWWPermissionKit 到您的文件,如果您使用 Swift,则导入您的 Bridging Header。

#import "VWWPermissionKit.h"

接下来创建一个 VWWPermission 类型的数组并显示权限窗口。一旦所有权限得到授权,表单就会消失,并调用 resultsBlock。在这里您可以检查每个权限。

Obj-C

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]);
                                }];
                            }];

Swift

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。它简单读取每个权限类型,它的好处是所有权限都共享相同的数据类型。

Obj-C

[VWWPermissionsManager readPermissions:permissions resultsBlock:^(NSArray *permissions) {
    [permissions enumerateObjectsUsingBlock:^(VWWPermission *permission, NSUInteger idx, BOOL *stop) {
        NSLog(@"%@ - %@", permission.type, [permission stringForStatus]);
    }];
}];

Swift

VWWPermissionsManager.readPermissions(permissions) { (permissions:[AnyObject]!) -> Void in
    for (_, permission) in permissions.enumerate(){
        if permission.status != VWWPermissionStatusAuthorized {
            // User didn't authorize this one
        }
    }
}

点击“隐私”按钮会将用户导航到您的 iOS 应用程序隐私设置,在那里他们可以更改权限。如果用户点击红色按钮,用户也会被导航到这里。一旦用户回到您的应用程序,权限就会被重新读取,屏幕也会更新。

示例图片

将 cocoa 框架添加到您的 iOS 项目

要将 VWWPermissionKit 添加到您的应用中,将 VWWPermissionKit.xcodeproj 项目文件拖动到您的应用的项目中。XCode 将添加 VWWPermissionKit 项目和源文件。

接下来,告诉 XCode 将 VWWPermissionKit 集成到您的应用中。转到您的应用的目标构建设置,并选择“常规”选项卡。在“嵌入的二进制文件”组下,添加 VWWPermissionKit。XCode 应自动为您添加链接引用。

最后,您需要告诉 XCode 在哪里可以找到正确的头文件。转到“构建设置”选项卡。在搜索栏中键入“header ”。然后转到“搜索路径”部分,并将 VWWPermissionKit 的文件路径添加到“头文件搜索路径”(递归:是)。

YSTK

要使用 CocoaPods 导入此框架,只需在您的 podfile 中添加此行

pod 'VWWPermissionKit', '~> 1.3.0'

功能权限类

  • VWWAssetLibraryPermission: AssetsLibrary
  • VWWCameraPermission: AVFoundation 和 UIImagePickerController
  • VWWCalendarsPermission: EventKit 框架
  • VWWCoreLocationAlwaysPermission: CoreLocation 框架
  • VWWCoreLocationWhenInUsePermission: CoreLocatoin 框架
  • VWWCoreMotionPermission: CoreMotion 框架
  • VWWMicrophonePermission: AVFoundation 框架
  • VWWNotificationsPermission: UIApplication(远程、用户和本地)
  • VWWRemindersPermission: EventKit 框架
  • VWWPhotosPermission: Photos 框架

正在开发中的权限类

  • VWWAccountsPermission: Accounts 框架
  • VWWBluetoothPermission: CoreBluetooth 框架
  • VWWCloudKitPermission: CloudKit 框架
  • VWWHealthPermission: HealthKit 框架
  • VWWHomePermission: HomeKit 框架

YSTK

  • 并非所有权限类型都在 iOS 模拟器中受支持
  • iOS 9(beta)无法在重新启动应用之前检测 iOS 隐私设置中的权限状态更改。预计这将在不久的将来得到修复。

本版本的新功能?

  • 已移除 ALAssetsLibrary,因为它已过时(使用新的 Photos API)
  • 用新 Contacts API 代替 AddressBook API
  • 支持 CoreMotion
  • 更新 UI 以获得更干净的外观
  • 遵循 +appearance 协议
  • 警告您的 Info.plist 中缺少条目
  • 显示不支持的服务
  • 支持动态字体大小