我正在尝试将此代码添加到 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/