问题
I'm making an app that uses HealthKit. The app must not work on an iPad, and as such my viewDidLoad
method contains an if/then/else statement to show an alert to iPad users. This is my code:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0") && [HKHealthStore isHealthDataAvailable] == 1) {
...
}
else {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Whoops!" message:@"Looks like your device doesn't support HealthKit :(" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:1 completion:^(){
NSLog(@"Showed error alert because of unsupported device.");
}];
}
The SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")
bit is from this gist.
The UIAlertController should show when the device is an iPad, is not running iOS 8.0+, or just (for some other reason) cannot use HealthKit. This is all good on paper, but when I run the app on the iPad 2 simulator running iOS 8, the app launches as normal and does not show the alert. For the record, I know the alert has no buttons but I don't want it to go away. It should only show on an iPad or device with less than iOS 8, and as such shouldn't need to go away when it's shown.
So why is my app not showing the alert view on iPad? The console shows no errors.
EDIT: The notification without buttons will not be in the final product, just in testing. The point still remains, however, as the alert should still be showing up.
回答1:
Have you checked that the device family is set to universal? If it is set to iPhone only the user idiom will never be iPad. Making the app universal seems to have resolved a similar problem asked in this question
PS, Apologies for my first answer not reading the question properly.
回答2:
If you want to target the iPhone only, the proper way to go about this is to set it in your deployment targets
I've changed this one to match the iPhone only
Now, that being the case, you'll still wind up with iPads running iPhone applications via the "scale up" method ( by scaling the iPhone version up to match the iPad ).
If you still want that alert in this case, then you can drop this in your ViewDidLoad
if (self.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
NSLog(@"I'm an ipad");
UIAlertController *alert = [ UIAlertController alertControllerWithTitle:@"Sorry dude, no iPads" message:@"go buy an iphone" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:^{}];
}
In this case i'm using the new traitCollection property to determine the interface idiom.
If you're just wanting to avoid larger screens, then I recommend focusing on size classes as per iOS 8 this will definitely be the best route.
http://www.learnswift.io/blog/2014/6/12/size-classes-with-xcode-6-and-swift
one place to get a start on it,
and of course apple's trait collection reference
https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UITraitSet_ClassReference/index.html
回答3:
Try something like this:
NSString *modelString = (NSString *)[UIDevice currentDevice].model;
if ([modelString hasPrefix:@"iPad"])
{
// iPad
return YES;
}
I believe this should work even when running iPhone only app on iPad.
来源:https://stackoverflow.com/questions/26414349/cannot-detect-device-as-ipad