根据 iOS 版本调用适当的 setStatusBarHidden

发布于 2024-09-05 13:38:15 字数 721 浏览 5 评论 0原文

今天我的应用程序获得了批准,但我收到用户发来的电子邮件说它崩溃了。我发现这

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];

就是问题所在,因为用户的固件为 3.1.x,所以该 API 无法正常工作并且应用程序崩溃。

所以我将其替换为

    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=3.2)
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
    else 
        [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];

我的问题...

我所做的是最好的解决方案吗?

当我设置 Traget OS 固件 3.0 时,为什么 XCODE 没有警告我 SetStatusBarHidden withAnimation 不在 3.0 中?

我是否必须检查每个 API 以查看它是否适用于我的目标操作系统?

谢谢

Today my app approved, but I got emails from users says it crash. I figured out that

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];

Is the problem, Because users have firmware 3.1.x this API is not working and app crash.

So I have replace it with

    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=3.2)
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
    else 
        [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];

My questions...

Is what I did the best solution?

Why XCODE did not warn me that SetStatusBarHidden withAnimation is not in 3.0 while I set my Traget OS firmware 3.0?

Do I have to check on every API to see if it is working with my Target OS?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初与友歌 2024-09-12 13:38:16

不幸的是,如果您使用模拟器使用 4.0 SDK 进行编译,上述解决方案会给您一个警告,默认情况下该警告被视为错误:

warning: 'setStatusBarHidden:animated:' is deprecated (declared at /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIApplication.h:267)

因此您可以在构建设置中将“将警告视为错误”设置为 false,或者您可以使用宏有条件地包含正确的代码。我的基本 SDK 设置为 Device 4.0,我的目标操作系统是 3.1,并且正在使用此宏:

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:YES];
#else
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
#endif
#endif

请注意,setStatusBarHidden:withAnimation 在 3.2 中可用。

Unfortunately, if you are compiling with the 4.0 SDK using the simulator, the above solutions will give you a warning, which by default is treated as an error:

warning: 'setStatusBarHidden:animated:' is deprecated (declared at /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIApplication.h:267)

So you can either set 'Treat warnings as errors' to false in the build settings, or you can use macros to conditionally include the correct code. I have my base SDK set to Device 4.0, my target os is 3.1, and am using this macro:

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:YES];
#else
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
#endif
#endif

Note that setStatusBarHidden:withAnimation came available in 3.2.

私野 2024-09-12 13:38:16

除了将您的应用程序限制为 >=3.2 之外,这可能是最好的选择。不管怎样,你的 xcode 应该给你一个警告,告诉你不支持 SetStatusBarHidden withAnimation 消息(“可能不会响应”)。

That's probably the best thing to do, other than limiting your application to >=3.2. anyways, you xcode should give you a warning that the SetStatusBarHidden withAnimation message is not supported ("may not respond to").

微暖i 2024-09-12 13:38:15

我建议您使用以下代码片段,而不是检查操作系统的版本,而是检查选择器当前是否可用。

if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
else 
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];

I'd recommend you to use the following snipplet of code instead of checking against the version of the os, rather check if a selector is currently available.

if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
else 
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
影子的影子 2024-09-12 13:38:15

仅使用

[[UIApplication sharedApplication] setStatusBarHidden:YES];

而不是

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];

它的工作原理没有任何警告

Use only

[[UIApplication sharedApplication] setStatusBarHidden:YES];

instead of

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];

It works with no warning

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文