隨著2019年蘋果發(fā)布會(huì)的發(fā)布磨镶,iPadOS與iOS13也帶來(lái)了新的架構(gòu)體系碎乃,那就是以UIScene為核心的分屏概念则酝。在iOS9之后悔据,ipad不同的應(yīng)用間是可以多開的庄敛,而在iOS13和ipadOS系統(tǒng)中,同一應(yīng)用也支持多開蜜暑。新系統(tǒng)之前铐姚,UIAppication管理者app和UI的生命周期,而新版本之后肛捍,UIAppication則更加專心的負(fù)責(zé)app的生命周期隐绵,而UI的管理則放在了UIWindowScene中,與之對(duì)應(yīng)的是拙毫,UIAppication的Delegate中所有與UI有關(guān)的方法和屬性都移除了(或者說(shuō)不建議被使用)依许。
即使你的app沒有適配分屏,但是在iOS13中也會(huì)被UIScene所管理缀蹄,但是還會(huì)走原來(lái)的回調(diào)峭跳。
當(dāng)然膘婶,這只是一部分。
而蛀醉,UIAppication中所有有關(guān)window的屬性如:
@property(nullable, nonatomic,readonly) UIWindow *keyWindow API_DEPRECATED("Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes", ios(2.0, 13.0));
@property(nonatomic,readonly) NSArray<__kindof UIWindow *> *windows;
- (BOOL)sendAction:(SEL)action to:(nullable id)target from:(nullable id)sender forEvent:(nullable UIEvent *)event;
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible API_UNAVAILABLE(tvos) API_DEPRECATED("Provide a custom network activity UI in your app if desired.", ios(2.0, 13.0));
@property(readonly, nonatomic) UIStatusBarStyle statusBarStyle API_UNAVAILABLE(tvos) API_DEPRECATED("Use the statusBarManager property of the window scene instead.", ios(2.0, 13.0)); // default is UIStatusBarStyleDefault
@property(readonly, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden API_UNAVAILABLE(tvos) API_DEPRECATED("Use the statusBarManager property of the window scene instead.", ios(2.0, 13.0));
@property(readonly, nonatomic) UIInterfaceOrientation statusBarOrientation API_UNAVAILABLE(tvos) API_DEPRECATED("Use the interfaceOrientation property of the window scene instead.", ios(2.0, 13.0));
都在新系統(tǒng)中廢棄悬襟。
具體的可以去了解一下關(guān)于UIScene更多的知識(shí)這里不再多講。
但是UIAppication給出兩個(gè)屬性:
#pragma mark -- UIScene --
// All of the currently connected UIScene instances
@property(nonatomic, readonly) NSSet<UIScene *> *connectedScenes API_AVAILABLE(ios(13.0));
// All of the representations that currently have connected UIScene instances or had their sessions persisted by the system (ex: visible in iOS' switcher)
@property(nonatomic, readonly) NSSet<UISceneSession *> *openSessions API_AVAILABLE(ios(13.0));
// returns YES if the application both declares multiple scene support in its info.plist and the executing environment allows multiple scenes for at least one system type. NO otherwise.
@property(nonatomic, readonly) BOOL supportsMultipleScenes API_AVAILABLE(ios(13.0));
可以看到我們得到的scene是一個(gè)集合.
那么我們?nèi)绻跀?shù)據(jù)模型中要使用到showWindow屬性的時(shí)候如何獲取呢拯刁?
我的一個(gè)解決方法是hook UIWindow的這個(gè)方法:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
1.記錄操作window
在每一次點(diǎn)擊UIView的時(shí)候脊岳,通過(guò)UIView的window屬性獲取當(dāng)前操作的window,記錄下來(lái)垛玻。
2.獲取window
在獲取window的時(shí)候遍歷connectedScenes割捅,如果只有一個(gè)scene處于前臺(tái),當(dāng)前在前臺(tái)的window處理事件的優(yōu)先級(jí)最高帚桩,當(dāng)做showWindow亿驾,其他的我們就用上次操作的window來(lái)處理事件。