Recommended way to test for iOS Version specific feature at runtime

↘锁芯ラ 提交于 2019-12-10 03:04:02

问题


I'm targetting IOS 4.3 and 5.0 with an app built against the 5.0 SDK and would like to add support for the Twitter functionality introduced in iOS5 only when the app runs on a iOS5 device. What is the recommended way to reliably test for the availability of these OS features at runtime without having your app crash?

I know you do this using respondsToSelector in Objective-C but how is it done in C#?


回答1:


With recent MonoTouch versions you can use the following code:

        if (UIDevice.CurrentDevice.CheckSystemVersion (5, 0)) {
            window.RootViewController = navigation;
        } else {
            window.AddSubview (navigation.View);
        }

Otherwise you can get a string from UIDevice.CurrentDevice.SystemVersion and do some checks with your own code.




回答2:


Follow up to comments, including mine...

If you want to check by feature you can do something like:

        MonoTouch.Twitter.TWRequest req = new MonoTouch.Twitter.TWRequest ();
        if (req.Handle == IntPtr.Zero) {
            Console.WriteLine ("No Twitter support before iOS5");
        }

What happens is that the selector to create the TWRequest instance will return null and the .NET object will be created in an invalid (unusable) state that you can query with the Handle property. Again YMMV, testing is key :-)



来源:https://stackoverflow.com/questions/8311284/recommended-way-to-test-for-ios-version-specific-feature-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!