ios - Swift 4 - 如何在 pod 类中导入 AppDelegate

标签 ios swift cocoapods

我正在尝试将此代码添加到 cocoapods 库内的 View Controller

 public override func viewWillDisappear(_ animated: Bool) {
     (UIApplication.shared.delegate as! AppDelegate).restrictRotation = .portrait
}

但出现 Use of undeclared type 'AppDelegate' 错误。如何将 AppDelegate 等项目文件导入到 pod 中?

最佳答案

Pod 应该可以在不同的项目中重用,因此您不会在 Pod 中包含项目文件。

您应该能够通过让您的 pod 定义一个协议(protocol),然后由您的项目应用程序委托(delegate)采用来实现您所追求的目标:

protocol RotationRestrictable {
    enum RotationRestriction {
        case .none
        case .portrait
        case .landscape
    }

    var restrictRotation: RotationRestriction
}

那么你可以说

public override func viewWillDisappear(_ animated: Bool) {
    if let delegate = UIApplication.shared.delegate as? RotationRestrictable {
        delegate.restrictRotation = .portrait
    }
}

在您的应用程序委托(delegate)中:

import YourPod

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, RotationRestrictable {

    var restrictRotation: RotationRestrictable.RotationRestriction = .none {
        didSet {
           // Whatever you need to do
        }
    }
 ...
}

关于ios - Swift 4 - 如何在 pod 类中导入 AppDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831335/

相关文章:

swift - 像 Paper.io 一样在形状中填充颜色

github - 如何在 GitLab 而不是 GitHub 上制作自己的 cocoapods?

用于世界地图 UI 的 iOS 库,如 Ocarina 或 UNIQLO Wake Up

ios - UISlider 添加到 UITableViewCell 奇怪的行为

swift - 字典顺序错误 - JSON

ios - 在开发 CocoaPod 时使用 CocoaPod 依赖项

swift - 编辑 CocoaPod 库的正确方法

ios - 基于 TCP 的客户端通信在 iOS 11 中不起作用?

ios - xcode 7 生成具有附加 CoreDataProperties 类别的核心数据实体

swift - 根据星期几显示文本