반응형
방법 1
// Swift
let sd = self.view.window.windowScene.delegate
// Objective-C
SceneDelegate *sd = self.view.window.windowScene.delegate;
방법 2
Swift :
//
// UIResponder+Scene.swift
//
import UIKit
@available(iOS 13.0, *)
extension UIResponder {
@objc var scene: UIScene? {
return nil
}
}
@available(iOS 13.0, *)
extension UIScene {
@objc override var scene: UIScene? {
return self
}
}
@available(iOS 13.0, *)
extension UIView {
@objc override var scene: UIScene? {
if let window = self.window {
return window.windowScene
} else {
return self.next?.scene
}
}
}
@available(iOS 13.0, *)
extension UIViewController {
@objc override var scene: UIScene? {
// Try walking the responder chain
var res = self.next?.scene
if (res == nil) {
// That didn't work. Try asking my parent view controller
res = self.parent?.scene
}
if (res == nil) {
// That didn't work. Try asking my presenting view controller
res = self.presentingViewController?.scene
}
return res
}
}
Objective-C :
//
// UIResponder+Scene.h
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIResponder (Scene)
@property (nonatomic, readonly, nullable) UIScene *scene API_AVAILABLE(ios(13.0));
@end
NS_ASSUME_NONNULL_END
//
// UIResponder+Scene.m
//
#import "ViewController+Scene.h"
@implementation UIResponder (Scene)
- (UIScene *)scene {
return nil;
}
@end
@implementation UIScene (Scene)
- (UIScene *)scene {
return self;
}
@end
@implementation UIView (Scene)
- (UIScene *)scene {
if (self.window) {
return self.window.windowScene;
} else {
return self.nextResponder.scene;
}
}
@end
@implementation UIViewController (Scene)
- (UIScene *)scene {
UIScene *res = self.nextResponder.scene;
if (!res) {
res = self.parentViewController.scene;
}
if (!res) {
res = self.presentingViewController.scene;
}
return res;
}
@end
반응형
'개발 > iOS' 카테고리의 다른 글
ARC or Non-ARC Compile Flag 설정 (0) | 2021.02.05 |
---|---|
String to CGFloat (0) | 2021.01.06 |
앱 호출 (URL scheme) (0) | 2021.01.05 |
URL query 파싱 및 json string 변환 (0) | 2020.12.17 |
디버깅 차단 처리 (Anti Debug) (0) | 2020.12.15 |