I want to present unique content on the external connected display if mirroring is not supported by the device (original iPad) but want to use screen mirroring if it's iPad 2. Now when I try to code this as follows:
if ([UIScreen instancesRespondToSelector:#selector(mirroredScreen)] && [[UIScreen mainScreen] mirroredScreen] == nil) {
// Mirroring not supported. Present unique content on external display
}
[[UIScreen mainScreen] mirroredScreen] always returns nil.
Am I doing something wrong?
As I understand the documentation, mirroredScreen will reference the main screen if you access the property on a secondary screen that actually is the mirrored screen. As in:
if ([[UIScreen screens] count] > 1) {
UIScreen *secondaryScreen = [[UIScreen screens] objectAtIndex:1];
NSLog(#"%#", secondaryScreen.mirroredScreen); // will reference the mainScreen
}
[[UIScreen mainScreen] mirroredScreen] would then always return nil because the mainScreen does not mirror itself.
Apple have a recommendation about the way to detect if the screen is mirrored or not here : http://developer.apple.com/library/ios/#qa/qa1738/_index.html
UIScreen *aScreen;
NSArray *screens = [UIScreen screens];
for (aScreen in screens)
{
if ([aScreen respondsToSelector:#selector(mirroredScreen)]
&& [aScreen mirroredScreen] == [UIScreen mainScreen])
{
// The main screen is being mirrored.
}
else
{
// The main screen is not being mirrored, or
// you are not running on a compatible device.
}
}
Related
This question already has answers here:
Detect Retina Display
(14 answers)
Closed 9 years ago.
Its easy to check whether device is iPhone 5 or iPhone, by checking its height, as given below
if([UIScreen mainScreen].bounds.size.height == 568){
// iPhone 5
} else{
// Regular iPhone
}
However, I want to know, after coming in else body 480 height , I want to check whether its iPhone or iPhone retina?
How can do that?
My main target is to set navigation, as given in my another question
iOS XIB
Thanks
If you really need this, you can use something like this:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
if ([[UIScreen mainScreen] scale] >= 2.0) {
// retina
}
else {
// not retina
}
}
Define as macro in pch file as below
#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:#selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))
I guess you have to check whether the screen responds to the scale message and its value is 2.0
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]
&&
[[UIScreen mainScreen] scale] == 2.0)
{
//Retina
}
else
{
//Not Retina
}
Use this method..
Return YES it means its retina otherwise non-retina,
+(BOOL)iPhoneRetina
{
return ([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) ;
}
I am setting up an application where I want to make a number of changes to the view layout based on the type of iPhone in use. Specifically I want to know if the vertical screen resolution is 1136 (iPhone5) or 960 (iPhone4). One of the things I want to do is adjust the height of my UITableViewCells in a UITableView so that no partial cells are displayed when the app is run on either phone.
My question is: what is the best way to detect the phone hardware type in use so that I can make the appropriate changes to my view layout?
I dont have iphone 5 but on the other phone this code worked great:
NSMutableDictionary *devices = [[NSMutableDictionary alloc] init];
[devices setObject:#"simulator" forKey:#"i386"];
[devices setObject:#"iPod Touch" forKey:#"iPod1,1"];
[devices setObject:#"iPod Touch Second Generation" forKey:#"iPod2,1"];
[devices setObject:#"iPod Touch Third Generation" forKey:#"iPod3,1"];
[devices setObject:#"iPod Touch Fourth Generation" forKey:#"iPod4,1"];
[devices setObject:#"iPhone" forKey:#"iPhone1,1"];
[devices setObject:#"iPhone 3G" forKey:#"iPhone1,2"];
[devices setObject:#"iPhone 3GS" forKey:#"iPhone2,1"];
[devices setObject:#"iPad" forKey:#"iPad1,1"];
[devices setObject:#"iPad 2" forKey:#"iPad2,1"];
[devices setObject:#"iPhone 4" forKey:#"iPhone3,1"];
[devices setObject:#"iPhone 4S" forKey:#"iPhone4"];
[devices setObject:#"iPhone 5" forKey:#"iPhone5"];
- (NSString *) getDeviceModel
{
struct utsname systemInfo;
uname(&systemInfo);
return [devices objectForKey:[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]];
}
Remember to import:
#import "sys/utsname.h"
You should really use the autolayout features to make your views adapt to whatever format the screen is.
However, if you only want to know if the device is an iPhone 5 (ie has 1136px in height) you could use:
[ [ UIScreen mainScreen ] bounds ].size.height
Could ofcourse be turned into a macro.
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
define this macro in some constant.h file and use it any where to check
if(IS_IPHONE5)
{
// iphone 5
}
else
{
// earlier
}
From your question ,it seems you want to correct UI depending on device and resolution. In that case, it is fine and you can use below given code to identify the device. But for non UI things, like looking for a device capability which is present in one device, absent in other, It is always advised to check for whether that capability is present, rather than which is the device model.
From apple's official forum. (login required)
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)]) {
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 960){
NSLog(#"iphone 4, 4s retina resolution");
}
if(result.height == 1136){
NSLog(#"iphone 5 resolution");
}
}else{
NSLog(#"iphone standard resolution");
}
}else{
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)]) {
NSLog(#"ipad Retina resolution");
}else{
NSLog(#"ipad Standard resolution");
}
}
I've been checking aspect ratio. Not as general as some of the other methods but still more flexible for future devices than checking a specific resolution.
// 0.563380 = 16x9 (iPhone 5)
// 0.666667 = 3x2 (iPhone 4S and previous)
CGFloat aspectRatio = [UIScreen mainScreen].bounds.size.width / [UIScreen mainScreen].bounds.size.height;
// Tall Screen
if (aspectRatio > 0.55 && aspectRatio < 0.57) {
// Tall screen stuff.
}
// Short Screen
} else if (aspectRatio > 0.64 && aspectRatio < 0.68) {
// Short screen stuff.
}
}
Is this possible? I want the number of inches, not the number of pixels. I know it is approximately 160 ppi. But not exactly.
There isn't an API that will give you this. Your best bet is to look at the device's screen size (in points) and from that surmise if it's an iPad or iPhone etc., and then use hard-coded values for the screen sizes.
Here's some code to get the screen size:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Be aware that width and height might be swapped, depending on device orientation.
If it were available it would be in UIScreen or UIDevice but it is not there.
You can infer it from info in Erica's UIDevice-extension and the specs for each device listed here on Wikipedia.
Here's a short method that estimates the device screen size. It's updated as to the latest devices, but may fail on future ones (as all methods of guessing might). It will also get confused if the device is being mirrored (returns the device's screen size, not the mirrored screen size)
#define SCREEN_SIZE_IPHONE_CLASSIC 3.5
#define SCREEN_SIZE_IPHONE_TALL 4.0
#define SCREEN_SIZE_IPAD_CLASSIC 9.7
+ (CGFloat)screenPhysicalSize
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height < 500)
return SCREEN_SIZE_IPHONE_CLASSIC; // iPhone 4S / 4th Gen iPod Touch or earlier
else
return SCREEN_SIZE_IPHONE_TALL; // iPhone 5
}
else
{
return SCREEN_SIZE_IPAD_CLASSIC; // iPad
}
}
You might need use [UIScreen mainScreen].scale;
CGFloat scale = [UIScreen mainScreen].scale;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat physicalWidth = screenRect.size.width * scale;
CGFloat physicalHeight = screenRect.size.height * scale;
Maybe Jeff Hay's code can be adapted to include iPad Mini. The trick is to get the device's model identifier. The most recent non-retina iPad is "iPad2,4" and the first iPad mini is "iPad2,5". Now all you need to check is if the screen scaling is 1.0 (non-retina)
Although this code is not future-proof, you can always add more rules for model identifiers.
#import <sys/utsname.h>
#define SCREEN_SIZE_IPAD_MINI 7.9
struct utsname systemInfo;
uname(&systemInfo);
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && strcasecmp(systemInfo.machine, "iPad2,5") >= 0 [[UIScreen mainScreen] scale] == 1.0)
return SCREEN_SIZE_IPAD_MINI
The "formula" I use is
#define IS_iPhone5 ( fabs( (double)[ [ UIScreen mainScreen ] bounds ].size.height - (double)568 ) < DBL_EPSILON )
Since this question has been asked, I’ve created an open-source library to handle this problem: IRLSize. It can be used in either direction: to measure the size of a view (or the whole screen) in real-world dimensions, or to set the size of a view to a specific real-world dimension.
note: screen rotation matters here
extension UIScreen {
var physicalSize:CGSize {
return CGSize(width: bounds.width*scale, height: bounds.height*scale)
}
}
using:
print(UIScreen.main.physicalSize)
Here is a Swift way to get screen sizes:
var screenWidth: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.width
} else {
return UIScreen.mainScreen().bounds.size.height
}
}
var screenHeight: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height
} else {
return UIScreen.mainScreen().bounds.size.width
}
}
var screenOrientation: UIInterfaceOrientation {
return UIApplication.sharedApplication().statusBarOrientation
}
These are included as a standard function in:
https://github.com/goktugyil/EZSwiftExtensions
Nobody said about fixedCoordinateSpace. In Swift 3 to get the screen dimensions in a portrait-up orientation you should use: UIScreen.main.fixedCoordinateSpace.bounds
CGFloat scale = [UIScreen mainScreen].scale;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width * (scale/100.0f);
CGFloat screenHeight = screenRect.size.height * (scale/100.0f);
scale is persent!
I thought I was doing something relatively simple, but I guess not.
Running:
NSLog(#"%f",[[UIScreen mainScreen] scale];
returns 0.000000
The problem is that I am trying to check for a retina display and the:
if([UIScreen respondsToSelector:#selector(scale)] &&
[[UIScreen mainScreen] scale] == 2.0) {
// does not get called on an iPhone 4
}
Doesnt get called. I have tried this both in the simulator as well as on the device.
I guess the answer is How to call [[UIScreen mainScreen] scale] in a universal app for the iphone and ipad
Hi I'm wondering if there's a way to get the width programmatically.
I'm looking for something general enough to accomodate iphone 3gs, iphone 4, ipad. Also, the width should change based on if the device is portrait or landscape (for ipad).
Anybody know how to do this?? I've been looking for a while... thanks!
Take a look at UIScreen.
eg.
CGFloat width = [UIScreen mainScreen].bounds.size.width;
Take a look at the applicationFrame property if you don't want the status bar included (won't affect the width).
UPDATE: It turns out UIScreen (-bounds or -applicationFrame) doesn't take into account the current interface orientation. A more correct approach would be to ask your UIView for its bounds -- assuming this UIView has been auto-rotated by it's View controller.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGFloat width = CGRectGetWidth(self.view.bounds);
}
If the view is not being auto-rotated by the View Controller then you will need to check the interface orientation to determine which part of the view bounds represents the 'width' and the 'height'. Note that the frame property will give you the rect of the view in the UIWindow's coordinate space which (by default) won't be taking the interface orientation into account.
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
//Bonus height.
CGFloat height = CGRectGetHeight(screen);
This can be done in in 3 lines of code:
// grab the window frame and adjust it for orientation
UIView *rootView = [[[UIApplication sharedApplication] keyWindow]
rootViewController].view;
CGRect originalFrame = [[UIScreen mainScreen] bounds];
CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil];
use:
NSLog(#"%f",[[UIScreen mainScreen] bounds].size.width) ;
As of iOS 9.0 there's no way to get the orientation reliably. This is the code I used for an app I design for only portrait mode, so if the app is opened in landscape mode it will still be accurate:
screenHeight = [[UIScreen mainScreen] bounds].size.height;
screenWidth = [[UIScreen mainScreen] bounds].size.width;
if (screenWidth > screenHeight) {
float tempHeight = screenWidth;
screenWidth = screenHeight;
screenHeight = tempHeight;
}
Use this code it will help
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width
Here is a Swift way to get screen sizes, this also takes current interface orientation into account:
var screenWidth: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.width
} else {
return UIScreen.mainScreen().bounds.size.height
}
}
var screenHeight: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height
} else {
return UIScreen.mainScreen().bounds.size.width
}
}
var screenOrientation: UIInterfaceOrientation {
return UIApplication.sharedApplication().statusBarOrientation
}
These are included as a standard function in:
https://github.com/goktugyil/EZSwiftExtensions