简体   繁体   English

如何拆分iOS 3.0和iOS 3.2的代码所以如果OS 3.2 ++我可以使用MPMoviePlayerViewController

[英]How to split Code for iOS 3.0 and iOS 3.2 so I can use MPMoviePlayerViewController if OS 3.2++

I have a video that I play. 我有一个我播放的视频。 To use full screen in iOS 3.2 I use the MPMoviePlayerViewController (seems to only work with that Class). 要在iOS 3.2中使用全屏我使用MPMoviePlayerViewController(似乎只适用于该类)。 But if I want to build for iOS 3.0 I obviously get several errors, because this class is not known in iOS 3.0. 但是,如果我想为iOS 3.0构建,我显然会遇到几个错误,因为这个类在iOS 3.0中是未知的。 I also know how to get what I want with MPMoviePlayerController in iOS 3.0, but I can only have one, either the code for iOS 3.0 or the code for iOS 3.2. 我也知道如何在iOS 3.0中使用MPMoviePlayerController获得我想要的东西,但我只能拥有一个,无论是iOS 3.0的代码还是iOS 3.2的代码。

How to cope with that ? 如何应对? - Solution found (see bottom of bundled edit) - 找到解决方案 (参见捆绑编辑的底部)

I guess I have to use multiple targets, do you have suggestions on how to do that (always when I tried multiple targets I got errors and gave up :) ) ? 我想我必须使用多个目标,你有关于如何做到这一点的建议(总是当我尝试多个目标我得到错误并放弃:))?


Edit bundled (multiple edits combined) 编辑捆绑 (多个编辑组合)

First I thought this would work. 首先我认为这会奏效。

#ifdef __IPHONE_3_0
// OS 3.0 specific
#endif

But it doesn't because in the iOS's Availability.h files you have all OS's defined from 2.0 up to your current one. 但事实并非如此,因为在iOS的Availability.h文件中,您所有的操作系​​统都是从2.0到现在定义的。 So if you compile for iOS 3.2 the #ifdef __IPHONE_3_0 will return true as well. 因此,如果您为iOS 3.2编译,#ifdef __IPHONE_3_0也将返回true。

Then I thought this would work 然后我认为这会奏效

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_3_2
  // Code for older iOS
#else
  // Code for iOS 3.2 up
#end

But it doesn't also. 但它也没有。 Because in iOS 3 for example __IPHONE_3_2 is undefined. 因为在iOS 3中例如__IPHONE_3_2未定义。

So I thought I would have to create some more intelligent if/elseif/else block but then I (finally :D) read the comment above the __IPHONE_X_X in apples AvailabilityInternal.h file definitions: 所以我想我必须创建一些更智能的if / elseif / else块然后我(最后:D)读取苹果AvailabilityInternal.h文件定义中__IPHONE_X_X上方的注释:

It says that you can use __IPHONE_OS_VERSION_MIN_REQUIRED for exactly that kind of problem, but that you shouldn't use the __IPHONE_X_X constants because of what just happened to me... they simply might not be defined thus evaluating to 0. So they recommend to use the values instead. 它说你可以使用__IPHONE_OS_VERSION_MIN_REQUIRED来解决这类问题,但你不应该使用__IPHONE_X_X常量因为刚刚发生在我身上的事情......他们根本就没有被定义,因此评估为0.所以他们建议使用相反的价值观。 So I have a working selector now like this... 所以我现在有一个像这样的工作选择器......

Solution that I found 我发现的解决方案

(Now this really works 100 %) (现在这真的很有效)

#if __IPHONE_OS_VERSION_MIN_REQUIRED < 30200
  // code for iOS below 3.2
#else
  // code for iOS 3.2 ++
#endif

I'm not sure if this is what you are trying to do, but as per Apple's recommendation for universal apps in the iPad Programming Guide, if you want to build for multiple OS versions with inconsistent APIs, you should use NSClassFromString, and go from there. 我不确定这是不是你想要做的,但是根据Apple对iPad编程指南中的通用应用程序的建议,如果你想构建具有不一致API的多个操作系统版本,你应该使用NSClassFromString,并从那里。 This way, you only have to have one target (the lowest OS you support) and through out your code have things like 这样,您只需拥有一个目标(您支持的最低操作系统),并通过您的代码完成类似的操作

Class mplayerControllerClass = NSClassFromString(@"MPMoviePlayerViewController");
if(mplayerControllerClass != nil) {
   //Code for 3.2, e.g. [mplayerControllerClass alloc]
} else {
   //Code for pre-3.2 OSes
}

Try this... 试试这个...

#ifdef IPHONE_OS_3.2

/* iOS 3.2 code */

#endif

#ifdef IPHONE_OS_3.0

/* iOS 3.0 code */

#endif

...the offending code will get removed before the compiler sees it. ...在编译器看到之前,将删除有问题的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM