UIAppUtils

UIAppUtils 是一个强大的 UIKit 实用工具库,专注于应用范围内的功能,旨在简化应用开发。

功能特性

安装

Swift Package Manager (SPM) 使用 Xcode

要使用 Swift Package Manager 将 UIAppUtils 集成到您的 Xcode 项目中,请按照以下步骤操作

  1. 在 Xcode 中打开您的项目。
  2. 导航到菜单栏并点击 File > Swift Packages > Add Package Dependency...
  3. 在新出现的窗口的搜索栏中,粘贴以下 URL:https://github.com/rayhaanalykhan/UIAppUtils.git
  4. 按照屏幕上的指示选择包选项和您想要集成的版本。
  5. 完成后,Xcode 将下载包并将其添加到项目导航器中。

用法

要在您的项目中使用 UIAppUtils,您可以直接调用其静态方法

  1. 检索应用版本和构建号

    获取应用的当前版本和构建号

    print("App Version: \(UIAppUtils.appVersion)")
    print("App Build: \(UIAppUtils.appBuild)")
  2. 获取顶层视图控制器

    检索应用当前主窗口视图层级结构中最顶层的视图控制器。

    if let topVC = UIAppUtils.getTopMostViewController() {
        // replace UIViewController() with the controller you want to show 
        topVC.present(UIViewController(), animated: true, completion: nil) 
     } else { 
        print("No view controller found.")
     }
    // Alternate
    // replace UIViewController() with the controller you want to show
    UIAppUtils.getTopMostViewController()?.present(UIViewController(), animated: true, completion: nil) 
  3. 截取屏幕截图

    捕获应用当前主窗口视图层级结构的屏幕截图。

    if let screenshot = UIAppUtils.takeScreenshot() { 
        // replace UIImageView() with the your imageView.
        UIImageView().image = screenshot 
        print("Screenshot taken.") 
    } else { 
        print("Could not take screenshot.") 
    }
    // Alternate
    // replace UIImageView() with the your imageView.
    UIImageView().image = UIAppUtils.takeScreenshot() ?? UIImage() 
  4. 打开设置

    打开主设置应用或应用特定的设置。

    UIAppUtils.goToSettingsApp() // Open the 'Settings' app of your device
    
    UIAppUtils.goToAppSettings() // Open the settings of your app
  5. 打开 URL

    此功能允许您的应用打开各种类型的 URL,包括外部 URL(例如,网络链接)和自定义 URL schemes。

    let url = URL(string: "https://github.com/rayhaanalykhan")!
    
    if UIAppUtils.openURL(url: url) {
        // Do something if url is opened
    } else {
        // Do something if url is cannot be opened
    }
    // Alternate
    let url = URL(string: "https://github.com/rayhaanalykhan")!
        
    UIAppUtils.openURL(url: url) // we can safely discard the result here.
  6. 请求应用评价

    提示用户为您的应用评分。

    UIAppUtils.requestReview()
  7. 发送电子邮件

    允许用户直接从应用撰写和发送电子邮件。包含一个委托来报告电子邮件状态(已保存、已发送、已取消等)。如果设备无法使用默认邮件应用发送电子邮件,或者如果未配置,则提供重定向到外部电子邮件应用的选项。有关更多详细信息,请参阅 ExternalMailOptions 部分。

    UIAppUtils.openEmailIntent(externalMailOption: .showConfirmationPrompt, with: ["rayhaanalykhan@gmail.com"], subject: "", body: "") { result in
            
    switch result {
        case .cancelled:
            print("Cancelled")
        case .saved:
            print("Saved")
        case .sent:
            print("Sent")
        case .failed:
            print("Failed")
        @unknown default:
            print("Default")
        }
    }

    注意:当使用外部邮件应用发送电子邮件时,委托方法不适用。

    // Alternate
    // You can even ignore the completion entirely.
    UIAppUtils.openEmailIntent(externalMailOption: .showConfirmationPrompt, with: ["rayhaanalykhan@gmail.com"], subject: "", body: "")
  8. 检查媒体权限

    检查摄像头(视频)或麦克风(音频)权限并处理响应。

    该功能处理所有情况下的所有授权状态(已授权、未决定、首次拒绝、之前拒绝、受限和未知),并显示适当的警报消息(如果必要/指定)。如果用户在第一次尝试时拒绝权限,他们可以使用 PreviouslyDeniedOption 枚举选择他们偏好的操作。

    UIAppUtils.checkMediaPermission(mediaType: .video, previouslyDeniedOption: .showGoToSettingsOption) { granted in
        if granted { 
            print("Camera access granted.") 
        } else { 
            print("Camera access denied.") 
        } 
    }
  9. 检查通知权限

    检查通知权限并处理响应。

    该功能处理所有情况下的所有授权状态(已授权、未决定、首次拒绝、之前拒绝、临时、短暂和未知),并显示适当的警报消息(如果必要/指定)。如果用户在第一次尝试时拒绝权限,他们可以使用 PreviouslyDeniedOption 枚举选择他们偏好的操作。

    UIAppUtils.checkNotificationPermission(previouslyDeniedOption: .showAlert) { granted in
        if granted { 
            print("Notification access granted.") 
        } else { 
            print("Notification access denied.") 
        } 
    }
  10. 检查定位权限

    检查定位权限并处理响应。

    该功能处理所有情况下的所有授权状态(始终允许、使用应用期间允许、未决定、首次拒绝、之前拒绝、受限和未知),并显示适当的警报消息(如果必要/指定)。如果用户在第一次尝试时拒绝权限,他们可以使用 PreviouslyDeniedOption 枚举选择他们偏好的操作。

    UIAppUtils.checkLocationPermission(previouslyDeniedOption: .doNothing) { granted in
        if granted { 
            print("Location access granted.") 
        } else { 
            print("Location access denied.") 
        } 
    }

ExternalMailOption

ExternalMailOption 枚举提供了用于管理电子邮件场景的选项

使用这些选项可以根据您的应用需求增强用户体验。

PreviouslyDeniedOption

PreviouslyDeniedOption 枚举提供了用于处理先前被拒绝的权限的选项

使用这些选项可以根据应用中的权限状态管理用户体验。

许可

UIAppUtils 在 MIT 许可证下发布。有关更多详细信息,请参阅 LICENSE 文件。

贡献

欢迎贡献者 fork 该项目并提交 pull request。如果可能,请为任何新的或现有功能包含单元测试。 此外,请相应地更新 README。

联系方式

如需更多信息,请通过电子邮件联系我 rayhaanalykhan@gmail.com