如何使代码兼容IOS 5和IOS 4 [iphone]

mia*_*mia 1 iphone

我正在开展一个新项目.我希望我的代码兼容IOS4和IOS5 SDK.我需要在IOS4和IOS5中都能运行所有功能.也就是说,我不打算在IOS5中使用新功能(功能明智)并禁用IOS4的该功能.

我有2个选择.让我知道哪个最好?

  1. IOS4的目标和代码.我认为,这也将在IOS5中正常运行.
  2. BaseSDK IOS5和目标IOS4.(我现在不计划使用ARC或故事板).

我认为使用方法#2,我必须在使用每个用法的同时要格外小心,因为我没有使用故事板和ARC,没有任何好处.所以希望#1更好.

让我知道专家意见.

注意:将来如果需要切换到IOS5的新功能,希望只有这个ARC会被阻塞,这也是一个可选的东西,我可以轻松切换rt?

Him*_*ngh 5

我使用此代码在多个iOS版本(3.0,4.0,5.0)上支持我的应用程序.

把它放在顶部(连同进口)

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

然后,如果有一些特定于操作系统的功能,请使用它们(我使用AlertView作为示例.在iOS5之前,UIAlertView不支持其中的自定义textView,因此我有自己的自定义AlertView.在iOS5中,该黑客不会工作,我必须使用UIAlertView,因为它支持自定义textViews):

if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {

    TextAlertView *alert = [[TextAlertView alloc] initWithTitle:@"xxxYYzz" 
                                                        message:@"" 
                                                       delegate:self cancelButtonTitle:@"Add"
                                              otherButtonTitles:@"Cancel", nil];
    alert.textField.keyboardType = UIKeyboardTypeDefault;
    alert.tag = 1;
    self.recipeNameTextField = alert.textField;
    [alert show];
    [alert release];
}
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"xxYYzz"
                                               message:@"" 
                                               delegate:self cancelButtonTitle:@"Add"
                                      otherButtonTitles:@"Cancel", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
self.recipeNameTextField = [alert textFieldAtIndex:0];

[alert show];
[alert release];
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你