UnityAppController.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #import "UnityAppController.h"
  2. #import "UnityAppController+ViewHandling.h"
  3. #import "UnityAppController+Rendering.h"
  4. #import "iPhone_Sensors.h"
  5. #import <CoreGraphics/CoreGraphics.h>
  6. #import <QuartzCore/QuartzCore.h>
  7. #import <QuartzCore/CADisplayLink.h>
  8. #import <Availability.h>
  9. #import <OpenGLES/EAGL.h>
  10. #import <OpenGLES/EAGLDrawable.h>
  11. #import <OpenGLES/ES2/gl.h>
  12. #import <OpenGLES/ES2/glext.h>
  13. #include <mach/mach_time.h>
  14. // MSAA_DEFAULT_SAMPLE_COUNT was moved to iPhone_GlesSupport.h
  15. // ENABLE_INTERNAL_PROFILER and related defines were moved to iPhone_Profiler.h
  16. // kFPS define for removed: you can use Application.targetFrameRate (30 fps by default)
  17. // DisplayLink is the only run loop mode now - all others were removed
  18. #include "CrashReporter.h"
  19. #include "UI/OrientationSupport.h"
  20. #include "UI/UnityView.h"
  21. #include "UI/Keyboard.h"
  22. #include "UI/SplashScreen.h"
  23. #include "Unity/InternalProfiler.h"
  24. #include "Unity/DisplayManager.h"
  25. #include "Unity/EAGLContextHelper.h"
  26. #include "Unity/GlesHelper.h"
  27. #include "Unity/ObjCRuntime.h"
  28. #include "PluginBase/AppDelegateListener.h"
  29. #include <assert.h>
  30. #include <stdbool.h>
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. #include <sys/sysctl.h>
  34. #import "IOSPlatformSDK.h"
  35. //#import <IOSToUnitySDK/IOSPlatformSDK.h>
  36. UnityAppController* _UnityAppController = nil;
  37. // Standard Gesture Recognizers enabled on all iOS apps absorb touches close to the top and bottom of the screen.
  38. // This sometimes causes an ~1 second delay before the touch is handled when clicking very close to the edge.
  39. // You should enable this if you want to avoid that delay. Enabling it should not have any effect on default iOS gestures.
  40. #define DISABLE_TOUCH_DELAYS 1
  41. // we keep old bools around to support "old" code that might have used them
  42. bool _ios42orNewer = false, _ios43orNewer = false, _ios50orNewer = false, _ios60orNewer = false, _ios70orNewer = false;
  43. bool _ios80orNewer = false, _ios81orNewer = false, _ios82orNewer = false, _ios83orNewer = false, _ios90orNewer = false, _ios91orNewer = false;
  44. bool _ios100orNewer = false, _ios101orNewer = false, _ios102orNewer = false, _ios103orNewer = false;
  45. bool _ios110orNewer = false, _ios111orNewer = false, _ios112orNewer = false;
  46. // was unity rendering already inited: we should not touch rendering while this is false
  47. bool _renderingInited = false;
  48. // was unity inited: we should not touch unity api while this is false
  49. bool _unityAppReady = false;
  50. // see if there's a need to do internal player pause/resume handling
  51. //
  52. // Typically the trampoline code should manage this internally, but
  53. // there are use cases, videoplayer, plugin code, etc where the player
  54. // is paused before the internal handling comes relevant. Avoid
  55. // overriding externally managed player pause/resume handling by
  56. // caching the state
  57. bool _wasPausedExternal = false;
  58. // should we skip present on next draw: used in corner cases (like rotation) to fill both draw-buffers with some content
  59. bool _skipPresent = false;
  60. // was app "resigned active": some operations do not make sense while app is in background
  61. bool _didResignActive = false;
  62. // was startUnity scheduled: used to make startup robust in case of locking device
  63. static bool _startUnityScheduled = false;
  64. bool _supportsMSAA = false;
  65. #if UNITY_SUPPORT_ROTATION
  66. // Required to enable specific orientation for some presentation controllers: see supportedInterfaceOrientationsForWindow below for details
  67. NSInteger _forceInterfaceOrientationMask = 0;
  68. #endif
  69. @implementation UnityAppController
  70. @synthesize unityView = _unityView;
  71. @synthesize unityDisplayLink = _displayLink;
  72. @synthesize rootView = _rootView;
  73. @synthesize rootViewController = _rootController;
  74. @synthesize mainDisplay = _mainDisplay;
  75. @synthesize renderDelegate = _renderDelegate;
  76. @synthesize quitHandler = _quitHandler;
  77. #if UNITY_SUPPORT_ROTATION
  78. @synthesize interfaceOrientation = _curOrientation;
  79. #endif
  80. - (id)init
  81. {
  82. if ((self = _UnityAppController = [super init]))
  83. {
  84. // due to clang issues with generating warning for overriding deprecated methods
  85. // we will simply assert if deprecated methods are present
  86. // NB: methods table is initied at load (before this call), so it is ok to check for override
  87. NSAssert(![self respondsToSelector: @selector(createUnityViewImpl)],
  88. @"createUnityViewImpl is deprecated and will not be called. Override createUnityView"
  89. );
  90. NSAssert(![self respondsToSelector: @selector(createViewHierarchyImpl)],
  91. @"createViewHierarchyImpl is deprecated and will not be called. Override willStartWithViewController"
  92. );
  93. NSAssert(![self respondsToSelector: @selector(createViewHierarchy)],
  94. @"createViewHierarchy is deprecated and will not be implemented. Use createUI"
  95. );
  96. }
  97. return self;
  98. }
  99. - (void)setWindow:(id)object {}
  100. - (UIWindow*)window { return _window; }
  101. - (void)shouldAttachRenderDelegate {}
  102. - (void)preStartUnity {}
  103. - (void)startUnity:(UIApplication*)application
  104. {
  105. NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");
  106. UnityInitApplicationGraphics();
  107. // we make sure that first level gets correct display list and orientation
  108. [[DisplayManager Instance] updateDisplayListInUnity];
  109. UnityLoadApplication();
  110. Profiler_InitProfiler();
  111. [self showGameUI];
  112. [self createDisplayLink];
  113. UnitySetPlayerFocus(1);
  114. }
  115. extern "C" void UnityDestroyDisplayLink()
  116. {
  117. [GetAppController() destroyDisplayLink];
  118. }
  119. extern "C" void UnityRequestQuit()
  120. {
  121. _didResignActive = true;
  122. if (GetAppController().quitHandler)
  123. GetAppController().quitHandler();
  124. else
  125. exit(0);
  126. }
  127. #if UNITY_SUPPORT_ROTATION
  128. - (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
  129. {
  130. // No rootViewController is set because we are switching from one view controller to another, all orientations should be enabled
  131. if ([window rootViewController] == nil)
  132. return UIInterfaceOrientationMaskAll;
  133. // Some presentation controllers (e.g. UIImagePickerController) require portrait orientation and will throw exception if it is not supported.
  134. // At the same time enabling all orientations by returning UIInterfaceOrientationMaskAll might cause unwanted orientation change
  135. // (e.g. when using UIActivityViewController to "share to" another application, iOS will use supportedInterfaceOrientations to possibly reorient).
  136. // So to avoid exception we are returning combination of constraints for root view controller and orientation requested by iOS.
  137. // _forceInterfaceOrientationMask is updated in willChangeStatusBarOrientation, which is called if some presentation controller insists on orientation change.
  138. return [[window rootViewController] supportedInterfaceOrientations] | _forceInterfaceOrientationMask;
  139. }
  140. - (void)application:(UIApplication*)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration
  141. {
  142. // Setting orientation mask which is requiested by iOS: see supportedInterfaceOrientationsForWindow above for details
  143. _forceInterfaceOrientationMask = 1 << newStatusBarOrientation;
  144. }
  145. #endif
  146. #if !PLATFORM_TVOS
  147. - (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification
  148. {
  149. AppController_SendNotificationWithArg(kUnityDidReceiveLocalNotification, notification);
  150. UnitySendLocalNotification(notification);
  151. }
  152. #endif
  153. #if UNITY_USES_REMOTE_NOTIFICATIONS
  154. - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
  155. {
  156. AppController_SendNotificationWithArg(kUnityDidReceiveRemoteNotification, userInfo);
  157. UnitySendRemoteNotification(userInfo);
  158. }
  159. - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
  160. {
  161. AppController_SendNotificationWithArg(kUnityDidRegisterForRemoteNotificationsWithDeviceToken, deviceToken);
  162. UnitySendDeviceToken(deviceToken);
  163. }
  164. #if !PLATFORM_TVOS
  165. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
  166. {
  167. AppController_SendNotificationWithArg(kUnityDidReceiveRemoteNotification, userInfo);
  168. UnitySendRemoteNotification(userInfo);
  169. if (handler)
  170. {
  171. handler(UIBackgroundFetchResultNoData);
  172. }
  173. }
  174. #endif
  175. - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
  176. {
  177. AppController_SendNotificationWithArg(kUnityDidFailToRegisterForRemoteNotificationsWithError, error);
  178. UnitySendRemoteNotificationError(error);
  179. // alas people do not check remote notification error through api (which is clunky, i agree) so log here to have at least some visibility
  180. ::printf("\nFailed to register for remote notifications:\n%s\n\n", [[error localizedDescription] UTF8String]);
  181. }
  182. #endif
  183. - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
  184. {
  185. NSMutableArray* keys = [NSMutableArray arrayWithCapacity: 3];
  186. NSMutableArray* values = [NSMutableArray arrayWithCapacity: 3];
  187. #define ADD_ITEM(item) do{ if(item) {[keys addObject:@#item]; [values addObject:item];} }while(0)
  188. ADD_ITEM(url);
  189. ADD_ITEM(sourceApplication);
  190. ADD_ITEM(annotation);
  191. #undef ADD_ITEM
  192. NSDictionary* notifData = [NSDictionary dictionaryWithObjects: values forKeys: keys];
  193. AppController_SendNotificationWithArg(kUnityOnOpenURL, notifData);
  194. [[IOSPlatformSDK sharedInstance]startWithUrl:url];
  195. return YES;
  196. }
  197. - (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  198. {
  199. return YES;
  200. }
  201. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  202. {
  203. ::printf("-> applicationDidFinishLaunching()\n");
  204. // send notfications
  205. #if !PLATFORM_TVOS
  206. if (UILocalNotification* notification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey])
  207. UnitySendLocalNotification(notification);
  208. if ([UIDevice currentDevice].generatesDeviceOrientationNotifications == NO)
  209. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  210. #endif
  211. UnityInitApplicationNoGraphics([[[NSBundle mainBundle] bundlePath] UTF8String]);
  212. [self selectRenderingAPI];
  213. [UnityRenderingView InitializeForAPI: self.renderingAPI];
  214. _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
  215. _unityView = [self createUnityView];
  216. [DisplayManager Initialize];
  217. _mainDisplay = [DisplayManager Instance].mainDisplay;
  218. [_mainDisplay createWithWindow: _window andView: _unityView];
  219. [self createUI];
  220. [self preStartUnity];
  221. // if you wont use keyboard you may comment it out at save some memory
  222. [KeyboardDelegate Initialize];
  223. #if !PLATFORM_TVOS && DISABLE_TOUCH_DELAYS
  224. for (UIGestureRecognizer *g in _window.gestureRecognizers)
  225. {
  226. g.delaysTouchesBegan = false;
  227. }
  228. #endif
  229. //以下是几种打开App方式
  230. if(launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){
  231. NSLog(@"打开方式 远程推送打开");
  232. }else if(launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]){
  233. NSLog(@"打开方式 本地推送打开");
  234. }else if(launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey]){
  235. NSLog(@"打开方式 UniversalLinks打开");
  236. }else if(launchOptions[UIApplicationLaunchOptionsURLKey]){
  237. //我们需要在此处处理通过 scheme 打开App并截获参数。
  238. NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
  239. NSLog(@"打开方式 通过URL打开的 ===== >> %@",url);
  240. IOSPlatformSDK * sdk = [IOSPlatformSDK sharedInstance];
  241. [sdk startWithUrl:url];
  242. }else if (!launchOptions){
  243. NSLog(@"打开方式 手动点击打开");
  244. [IOSPlatformSDK sharedInstance];
  245. }
  246. return YES;
  247. }
  248. - (void)applicationDidEnterBackground:(UIApplication*)application
  249. {
  250. ::printf("-> applicationDidEnterBackground()\n");
  251. }
  252. - (void)applicationWillEnterForeground:(UIApplication*)application
  253. {
  254. ::printf("-> applicationWillEnterForeground()\n");
  255. // applicationWillEnterForeground: might sometimes arrive *before* actually initing unity (e.g. locking on startup)
  256. if (_unityAppReady)
  257. {
  258. // if we were showing video before going to background - the view size may be changed while we are in background
  259. [GetAppController().unityView recreateRenderingSurfaceIfNeeded];
  260. }
  261. }
  262. - (void)applicationDidBecomeActive:(UIApplication*)application
  263. {
  264. ::printf("-> applicationDidBecomeActive()\n");
  265. [self removeSnapshotView];
  266. if (_unityAppReady)
  267. {
  268. if (UnityIsPaused() && _wasPausedExternal == false)
  269. {
  270. UnityWillResume();
  271. UnityPause(0);
  272. }
  273. if (_wasPausedExternal)
  274. {
  275. if (UnityIsFullScreenPlaying())
  276. TryResumeFullScreenVideo();
  277. }
  278. UnitySetPlayerFocus(1);
  279. }
  280. else if (!_startUnityScheduled)
  281. {
  282. _startUnityScheduled = true;
  283. [self performSelector: @selector(startUnity:) withObject: application afterDelay: 0];
  284. }
  285. _didResignActive = false;
  286. }
  287. - (void)removeSnapshotView
  288. {
  289. // do this on the main queue async so that if we try to create one
  290. // and remove in the same frame, this always happens after in the same queue
  291. dispatch_async(dispatch_get_main_queue(), ^{
  292. if (_snapshotView)
  293. {
  294. [_snapshotView removeFromSuperview];
  295. _snapshotView = nil;
  296. // Make sure that the keyboard input field regains focus after the application becomes active.
  297. [[KeyboardDelegate Instance] becomeFirstResponder];
  298. }
  299. });
  300. }
  301. - (void)applicationWillResignActive:(UIApplication*)application
  302. {
  303. ::printf("-> applicationWillResignActive()\n");
  304. if (_unityAppReady)
  305. {
  306. UnitySetPlayerFocus(0);
  307. _wasPausedExternal = UnityIsPaused();
  308. if (_wasPausedExternal == false)
  309. {
  310. // Pause Unity only if we don't need special background processing
  311. // otherwise batched player loop can be called to run user scripts.
  312. if (!UnityGetUseCustomAppBackgroundBehavior())
  313. {
  314. // Force player to do one more frame, so scripts get a chance to render custom screen for minimized app in task manager.
  315. // NB: UnityWillPause will schedule OnApplicationPause message, which will be sent normally inside repaint (unity player loop)
  316. // NB: We will actually pause after the loop (when calling UnityPause).
  317. UnityWillPause();
  318. [self repaint];
  319. UnityPause(1);
  320. // this is done on the next frame so that
  321. // in the case where unity is paused while going
  322. // into the background and an input is deactivated
  323. // we don't mess with the view hierarchy while taking
  324. // a view snapshot (case 760747).
  325. dispatch_async(dispatch_get_main_queue(), ^{
  326. // if we are active again, we don't need to do this anymore
  327. if (!_didResignActive)
  328. {
  329. return;
  330. }
  331. _snapshotView = [self createSnapshotView];
  332. if (_snapshotView)
  333. [_rootView addSubview: _snapshotView];
  334. });
  335. }
  336. }
  337. }
  338. _didResignActive = true;
  339. }
  340. - (void)applicationDidReceiveMemoryWarning:(UIApplication*)application
  341. {
  342. ::printf("WARNING -> applicationDidReceiveMemoryWarning()\n");
  343. UnityLowMemory();
  344. }
  345. - (void)applicationWillTerminate:(UIApplication*)application
  346. {
  347. ::printf("-> applicationWillTerminate()\n");
  348. Profiler_UninitProfiler();
  349. UnityCleanup();
  350. extern void SensorsCleanup();
  351. SensorsCleanup();
  352. }
  353. @end
  354. void AppController_SendNotification(NSString* name)
  355. {
  356. [[NSNotificationCenter defaultCenter] postNotificationName: name object: GetAppController()];
  357. }
  358. void AppController_SendNotificationWithArg(NSString* name, id arg)
  359. {
  360. [[NSNotificationCenter defaultCenter] postNotificationName: name object: GetAppController() userInfo: arg];
  361. }
  362. void AppController_SendUnityViewControllerNotification(NSString* name)
  363. {
  364. [[NSNotificationCenter defaultCenter] postNotificationName: name object: UnityGetGLViewController()];
  365. }
  366. extern "C" UIWindow* UnityGetMainWindow() {
  367. return GetAppController().mainDisplay.window;
  368. }
  369. extern "C" UIViewController* UnityGetGLViewController() {
  370. return GetAppController().rootViewController;
  371. }
  372. extern "C" UIView* UnityGetGLView() {
  373. return GetAppController().unityView;
  374. }
  375. extern "C" ScreenOrientation UnityCurrentOrientation() { return GetAppController().unityView.contentOrientation; }
  376. bool LogToNSLogHandler(LogType logType, const char* log, va_list list)
  377. {
  378. NSLogv([NSString stringWithUTF8String: log], list);
  379. return true;
  380. }
  381. static void AddNewAPIImplIfNeeded();
  382. // From https://stackoverflow.com/questions/4744826/detecting-if-ios-app-is-run-in-debugger
  383. static bool isDebuggerAttachedToConsole(void)
  384. // Returns true if the current process is being debugged (either
  385. // running under the debugger or has a debugger attached post facto).
  386. {
  387. int junk;
  388. int mib[4];
  389. struct kinfo_proc info;
  390. size_t size;
  391. // Initialize the flags so that, if sysctl fails for some bizarre
  392. // reason, we get a predictable result.
  393. info.kp_proc.p_flag = 0;
  394. // Initialize mib, which tells sysctl the info we want, in this case
  395. // we're looking for information about a specific process ID.
  396. mib[0] = CTL_KERN;
  397. mib[1] = KERN_PROC;
  398. mib[2] = KERN_PROC_PID;
  399. mib[3] = getpid();
  400. // Call sysctl.
  401. size = sizeof(info);
  402. junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
  403. assert(junk == 0);
  404. // We're being debugged if the P_TRACED flag is set.
  405. return ((info.kp_proc.p_flag & P_TRACED) != 0);
  406. }
  407. void UnityInitTrampoline()
  408. {
  409. #if ENABLE_CRASH_REPORT_SUBMISSION
  410. SubmitCrashReportsAsync();
  411. #endif
  412. InitCrashHandling();
  413. _ios42orNewer = _ios43orNewer = _ios50orNewer = _ios60orNewer = _ios70orNewer = true;
  414. NSString* version = [[UIDevice currentDevice] systemVersion];
  415. #define CHECK_VER(s) [version compare: s options: NSNumericSearch] != NSOrderedAscending
  416. _ios80orNewer = CHECK_VER(@"8.0"), _ios81orNewer = CHECK_VER(@"8.1"), _ios82orNewer = CHECK_VER(@"8.2"), _ios83orNewer = CHECK_VER(@"8.3");
  417. _ios90orNewer = CHECK_VER(@"9.0"), _ios91orNewer = CHECK_VER(@"9.1");
  418. _ios100orNewer = CHECK_VER(@"10.0"), _ios101orNewer = CHECK_VER(@"10.1"), _ios102orNewer = CHECK_VER(@"10.2"), _ios103orNewer = CHECK_VER(@"10.3");
  419. _ios110orNewer = CHECK_VER(@"11.0"), _ios111orNewer = CHECK_VER(@"11.1"), _ios112orNewer = CHECK_VER(@"11.2");
  420. #undef CHECK_VER
  421. AddNewAPIImplIfNeeded();
  422. #if !TARGET_IPHONE_SIMULATOR
  423. // Use NSLog logging if a debugger is not attached, otherwise we write to stdout.
  424. if (!isDebuggerAttachedToConsole())
  425. UnitySetLogEntryHandler(LogToNSLogHandler);
  426. #endif
  427. }
  428. // sometimes apple adds new api with obvious fallback on older ios.
  429. // in that case we simply add these functions ourselves to simplify code
  430. static void AddNewAPIImplIfNeeded()
  431. {
  432. if (![[CADisplayLink class] instancesRespondToSelector: @selector(setPreferredFramesPerSecond:)])
  433. {
  434. IMP CADisplayLink_setPreferredFramesPerSecond_IMP = imp_implementationWithBlock(^void(id _self, NSInteger fps) {
  435. typedef void (*SetFrameIntervalFunc)(id, SEL, NSInteger);
  436. UNITY_OBJC_CALL_ON_SELF(_self, @selector(setFrameInterval:), SetFrameIntervalFunc, (int)(60.0f / fps));
  437. });
  438. class_replaceMethod([CADisplayLink class], @selector(setPreferredFramesPerSecond:), CADisplayLink_setPreferredFramesPerSecond_IMP, CADisplayLink_setPreferredFramesPerSecond_Enc);
  439. }
  440. if (![[UIScreen class] instancesRespondToSelector: @selector(nativeScale)])
  441. {
  442. IMP UIScreen_NativeScale_IMP = imp_implementationWithBlock(^CGFloat(id _self) {
  443. return ((UIScreen*)_self).scale;
  444. });
  445. class_replaceMethod([UIScreen class], @selector(nativeScale), UIScreen_NativeScale_IMP, UIScreen_nativeScale_Enc);
  446. }
  447. if (![[UIScreen class] instancesRespondToSelector: @selector(maximumFramesPerSecond)])
  448. {
  449. IMP UIScreen_MaximumFramesPerSecond_IMP = imp_implementationWithBlock(^NSInteger(id _self) {
  450. return 60;
  451. });
  452. class_replaceMethod([UIScreen class], @selector(maximumFramesPerSecond), UIScreen_MaximumFramesPerSecond_IMP, UIScreen_maximumFramesPerSecond_Enc);
  453. }
  454. if (![[UIView class] instancesRespondToSelector: @selector(safeAreaInsets)])
  455. {
  456. IMP UIView_SafeAreaInsets_IMP = imp_implementationWithBlock(^UIEdgeInsets(id _self) {
  457. return UIEdgeInsetsZero;
  458. });
  459. class_replaceMethod([UIView class], @selector(safeAreaInsets), UIView_SafeAreaInsets_IMP, UIView_safeAreaInsets_Enc);
  460. }
  461. }