Nous avons mis à jour notre SDK vers iOS 8.3, et tout à coup, notre méthode de détection de l'iPad ne fonctionne pas correctement:
+ (BOOL) isiPad { #ifdef UI_USER_INTERFACE_IDIOM return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad; #endif return NO; }
le bloc ifdef
n'est jamais entré, et donc return NO;
est toujours exécuté. Comment puis-je détecter si l'appareil est un iPad sans utiliser UI_USER_INTERFACE_IDIOM()
?
J'utilise:
Dans 8.2
UserInterfaceIdiom()
est
#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
Dans 8.3
UserInterfaceIdiom()
est
static inline UIUserInterfaceIdiom UI_USER_INTERFACE_IDIOM() { return ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone); }
Donc, #ifdef UI_USER_INTERFACE_IDIOM
est toujours faux dans 8.3
Notez que l'en-tête indique
La fonction UI_USER_INTERFACE_IDIOM () est prévue pour être utilisée lors du deployment sur une version de l'iOS inférieure à 3.2. Si la première version d'iPhone / iOS pour laquelle vous allez déployer est 3.2 ou supérieure, vous pouvez utiliser – [UIDevice userInterfaceIdiom] directement.
Alors suggérez-vous de refactoriser
+ (BOOL) isiPad { static BOOL isIPad = NO; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ isIPad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad; }); return isIPad; }