Support of several iOS - application development - ios

With the release of iOS6, I am going to update my application using the new feature of iOS6, such as UICollectionView.
When I will deploy it on App Store, my application will be only available for devices on iOS6.
Question: How can I let the devices on iOS 5.1 using the older release which do not use UICollectionView?
I could go more deeper by asking: What is the strategy to upgrade an application without loosing the users which have not yet upgrade their OS?
Thank you for your advice.

You could check for availability of new iOS6 classes with Class tmp = NSClassFromString(#"UIActivityViewController");
if (tmp) {//use iOS6 class}else{//do something else}. Also, you could use some macros, like __IPHONE_5_0, put some code between #ifndef or #if defined(...). Good Luck!

This one will work perfectly:
NSString * systemVersion = [[UIDevice currentDevice] systemVersion];
if ([self.systemVersion compare:#"5.0" options:NSNumericSearch] != NSOrderedAscending)
{
osHigherThaniOS5 = TRUE;
}
if ([self.systemVersion compare:#"6.0" options:NSNumericSearch] != NSOrderedAscending)
{
osHigherThaniOS6 = TRUE;
}
Just determine the system version and set your conditions accordingly :)

Related

How to continue to support iOS 5.1 as functions became deprecated

I have an app that must work for iOS versions >= 5.1.
Apple docs say that "The AudioSession API has been completely deprecated in iOS 7.0". And one should use AVAudioSession class instead.
But the method that I need (- (BOOL)setCategory:(NSString *)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError) is only available starting from iOS 6.0.
So it seems that I have to continue usage of deprecated AudioSessionSetProperty to support iOS 5.1.
What is a correct way to handle such situations (my question is general, not only about this particular problem with audio)? Should I write something like
if ([[[UIDevice currentDevice] systemVersion] compare:#"6.0" options:NSNumericSearch] == NSOrderedAscending)
{
// use deprecated methods (AudioSessionSetProperty)
}
else
{
// use brand-new methods (AVAudioSession)
}
or maybe it's ok to just use AudioSessionSetProperty until it stops to compile?
Deprecated methods doesn't stops you from using it, but yes if its deprecated from your current selected iOS version then it'll give you warning. An example, if your app still supporting iOS < 6.0 and you're need to change alignment of UILabel then you can do it like this,
if ([[[UIDevice currentDevice] systemVersion] compare:#"6.0" options:NSNumericSearch] == NSOrderedDescending)
{
lbl.textAlignment = UITextAlignmentCenter;
//for iOS < 6.0, warning when deployment target is iOS 5.0
}
else
{
lbl.textAlignment = NSTextAlignmentCenter; //for iOS >= 6.0
}
OR
lbl.textAlignment = UITextAlignmentCenter; //with warning in iOS 6.0
OR
lbl.textAlignment = NSTextAlignmentCenter; //will crash in iOS 5.0
PS. better to support iOS 7.0 and higher (not even iOS 6.0 as Apple wouldn't allow apps with iOS6.0 from coming Feb as commented by #ahwulf.), as latest iOS 8 was released with lots of good features and most of the people updated their phone with this. This will help you in two cases, you'll not need to check for iOS version compatibility and your code looks nice without warnings :) but still its all depends your needs.
Update:
OR
as #rmaddy suggested one should check for existence of method using respondsToSelector:
UILabel *label;
if([label respondsToSelector:#selector(textAlignment)]) {
label.textAlignment = NSTextAlignmentCenter;
}
In above example, we're checking (but in real we're already know this) whether UILabel has textAlignment property or not.

iOS: How to distinguish between iPhone mode and iPad mode?

I'm building an universal iOS app and some config variables have different value between iPhone mode and iPad mode. Please notice that I said the Mode word.
As I know, users could install an iPhone app in iPad from AppStore, so there is something confused to me about the following code:
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType rangeOfString:#"iPad" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
fCurrentDeviceIsiPad = YES;
// ...
}
else // iPhone? iPod?
{
fCurrentDeviceIsiPad = NO;
// ...
}
If user install & run the iPhone version app in iPad, the fCurrentDeviceIsiPad will be YES (I tested the result with debugging it in my iPad for iPhone version, certainly, I'd changed the deployment device as "iPhone" only first).
So, my question is how could I distinguish the app mode without taking the device type as consideration?
Someone posted something about the OS macros, I've tried them in my app.
#if TARGET_OS_IPHONE
// iPhone specific code
#error A
#elif TARGET_OS_IPAD
// iPad specific code
#error B
#else
// Unknown target
#error C
#endif
This didn't hit my point, either. I switched the debug device to iPhone simulator and iPad simulator, all the compile errors are hit on error A. Did I misunderstand the meaning of these macros?
One more question, this will be the first time I submit an iOS to AppStore and it's an universal app. For the submit process, is it standalone process for uploading iPhone version binary and iPad version binary? You may get my new point: if the answer is yes, it's ok for me to build two versions manually with my custom-macros.
Thanks!
UPDATE: Problem solved!
The code [UIDevice currentDevice].model is NOT the same with the UI_USER_INTERFACE_IDIOM(), the former one always returns the real device type, the latter one is the current running app mode/version.
BOOL isIpad() {
return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
}
Create an NSObject file and name it Common. Delete the .m and delete all the code written in Common.h
Import the Common.h file in .pch file
Put this is the Common.h file
And then you can use it throughout your app.
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
Thanks.

How to set an enum not present in current iOS version?

I am trying to set linebreakmode for textLabel in UIButton.
self.scheduleButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
NSLineBreakByWordWrapping won't be available prior to iOS 6.0.
self.scheduleButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
will work prior to iOS6 but XCode is throwing an error saying, it doesn't recognize UILineBreakModeWordWrap.
How do I make sure that UILineBreakModeWordWrap code is completely ignored prior to iOS 6.
I have made a macro to check OS version, I would have used respondsToSelector, had it been a case with a method but this is an enum type.
SYSTEM_VERSION_LESS_THAN(#"6.0")
I am using this here but in an if-else scenario but it wont still work as XCode will still tell me that UILineBreakModeWordWrap is not recognizable.
Is there a way I can know OS version at pre-processing level ?
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
This macro that I have made will just add Objective C code to determine OS type which means the check will happen at runtime.
but because of this XCode will not ignore UILineBreakModeWordWrap.
Both NSLineBreakByWordWrapping and UILineBreakModeWordWrap have the same value. As long as your Base SDK is 6.0 or later then using NSLineBreakByWordWrapping will make the compiler happy and it will work for any version of iOS at runtime.
Option:1
You cane create your own macros to handle this:
#ifdef __IPHONE_6_0
# define LINE_BREAK_MODE_WORD_WRAP NSLineBreakByWordWrapping
#else
# define LINE_BREAK_MODE_WORD_WRAP UILineBreakModeWordWrap
#endif
Now in code you can use, LINE_BREAK_MODE_WORD_WRAP
Option:2
Directly use enum value instead of reference name, both NSLineBreakByWordWrapping and UILineBreakModeWordWrap are having same value - 0
Hence you can directly write this way as well -
self.scheduleButton.titleLabel.lineBreakMode = 0;
It will automatically picks up 0th value from iOS SDK enumeration reference.
Hope this helps.

iOS Simulator iOS Version

Is there a way to detect what iOS simulator you are running. i.e. The difference between running 5.1 vs. 6.1.
Using [[UIDevice currentDevice] systemVersion] return x86 so i can't determine the difference between the 6.1 simulator and the 7.0 simulator.
You should take a look at the Availability
The relevant declarations are all in
Availability.h
AvailabilityInternal.h
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
// Here we go with iOS 6++
#else
// This is the old way
#endif
[[UIDevice currentDevice] systemVersion] should give you the information you need. However, that is not something that should be used for logic. It is designed to be a user-presentable string.
If you want to make logic decisions, you should do it based on capabilities. For example, check for the existence of potentially-weak symbols at runtime before using them rather than making decisions based on the system version.
That's easy!
Swift version:
if #available(iOS 11.0, *) {
} else {
}
Objective-C version:
if (#available(iOS 11.0, *)) {
} else {
}

Is there a specific Xcode compiler flag that gets set when compiling for iPad?

Is there a specific Xcode compiler flag that gets set when compiling for iPad?
I want to conditionally compile iPad vs iPhone/iPod Touch code for example:
#ifdef TARGET_IPAD
code for iPad
#else
code for iPhone
#endif
I know there is already TARGET_OS_IPHONE and TARGET_CPU_ARM in TargetConditionals.h but anything that easily and specifically targets iPad?
-Rei
The correct API to use for run-time checking of iPad vs. iPhone/iPad Touch is:
BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
The UIDevice header filer also includes a convenient macro, UI_USER_INTERFACE_IDIOM(), which will be helpful if your deployment target is < iPhone 3.2.
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
So you could just say, negatively:
BOOL deviceIsPad = (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone);
Instead of using compile-time flags, you should use run-time check e.g. use NSClassFromString to see if a class exists because the same app can be installed on both devices.
And because of the possibility of running the app in both devices, there isn't a built-in compile-time check whether it targets iPad or not.
Currently I didn’t find anything that would let you check if you are on an iPad, but I’m also not sure if Apple recommends runtime checks. Here’s an excerpt from Apple:
In addition to your view controllers, any classes that are shared between iPhone and iPad devices need to include conditional compilation macros to isolate device-specific code. Although you could also use runtime checks to determine if specific classes or methods were available, doing so would only increase the size of your executable by adding code paths that would not be followed on one device or the other. Letting the compiler remove this code helps keep your code cleaner.
However, there is no place I could find more specific information about conditional compilation macros.
For multiple targets sharing the same project/code, I'm doing this by editing the C flags for my iPad target.
With the [myapp]-iPad target chosen as the active target, pick Project -> Edit Active Target [myapp]-iPad. Search for "c flags" and double-click. Add a flag for "-D TARGET_IPAD". Now the symbol TARGET_IPAD will be defined only for your iPad target.
Of course, this only works if you're using separate targets for iPad and iPhone. If you're running the same binary on both, obviously there's nothing the compiler can do for you. (However, the 3.2 SDK as of the end of January doesn't even support Universal apps yet.)
(Edited; I was confused about the terminology of "Universal" apps etc.)
Or -> just to be sure
-(BOOL)isDeviceAniPad
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
return deviceIsPad;
#endif
return NO;
}
I think this will do
-(BOOL)isDeviceAniPad
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
#endif
return NO;
}

Resources