With the upcoming release of the iPhone X, I want to be able to display a different UI layout for the iPhone X (due to round corners and bottom line, which kinda works as the home button replacement).
I am using the following nuget package to retrieve the model information:
https://github.com/dannycabrera/Get-iOS-Model
It works perfectly fine, but all the different simulators (iPhone 7, 8, X) only come up as Simulator.
Is there a way to differentiate between the different iPhone Simulators in code within my Xamarin mobile app?
Many thanks,
Nik
Since the simulator is a weird animal, the screen size is as good as any other test after the other tests for iOS version and the availability of FaceID on a physical device:
public bool iPhoneX()
{
var version = new Version(ObjCRuntime.Constants.Version);
if (version < new Version(11, 0))
return false;
if (ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.DEVICE)
{
using (var context = new LocalAuthentication.LAContext())
{
if (context.BiometryType == LABiometryType.TypeFaceId)
return true;
}
return false;
}
if (UIScreen.MainScreen.PreferredMode.Size.Height == 2436)
return true;
return false;
}
Or an optimized property for repeated (binding) calls:
static bool? iPhoneX;
public bool isPhoneX
{
get
{
if (iPhoneX == null)
{
if (new Version(ObjCRuntime.Constants.Version) < new Version(11, 0))
iPhoneX = false;
else
{
if (ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.DEVICE)
{
using (var context = new LocalAuthentication.LAContext())
{
iPhoneX = context.BiometryType == LABiometryType.TypeFaceId;
}
}
else
iPhoneX = UIScreen.MainScreen.PreferredMode.Size.Height == 2436;
}
}
return (bool)iPhoneX;
}
}
You should simply use the Safe Area Layout Guide which will automatically increase the top/bottom margin on the iPhone X.
As others have pointed out you should definitely be using the Safe Area Layout Guide.
If you really really have a need to detect the model then look for the SIMULATOR_MODEL_IDENTIFIER environment variable. iPhone X will return iPhone10,3.
let model = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]
Related
Xamarin Android app that use the Xamarin Forms UI framework.
I detect the screen orientation and want to only allow portrait and landscape for a specific Page but portrait only for all other pages.
I tried to call RequestOrientation but that force the orientation to stay the same and doesn't fire my orientation change notification again.
Have a static property in your App.Xaml.cs
public static bool IsPortraitOnly { get; set; }
Set it to false in all pages and to true in the page you want it to be Portrait only.
In your MainActivity in android project, override the method OnConfigurationChanged :
public override void OnConfigurationChanged(Configuration newConfig)
{
//using to prevent the OnCreate from firing when rotating or screen size is changing
if (App.IsPortraitOnly)
{
RequestedOrientation = ScreenOrientation.Portrait;
newConfig.Orientation = Orientation.Portrait;
}
base.OnConfigurationChanged(newConfig);
}
The following are the available orientation values in android :
public enum Orientation
{
Landscape = 2,
Portrait = 1,
Square = 3,
Undefined = 0
}
To determine whether you’re in portrait or landscape mode is pretty easy:
static bool IsPortrait(Page p) { return p.Width < p.Height; }
you can use sizeChanged event.The SizeChanged event seems to get called exactly once as the user goes from portrait to landscape mode
SizeChanged += (sender, e) => Content = IsPortrait(this) ? portraitView : landscapeView;
RequestedOrientation = ScreenOrientation.Portrait;
If you write code on the code side, portrait mode is maintained.
In iOS 10, there is a new api which allows developers to make use of the taptic engine, UIFeedbackGenerator.
While this api is available in iOS 10, it only works on the new devices, iPhone 7 and 7 plus. It does not works on older devices including the 6S or 6S Plus, even those have a taptic engine. I guess the taptic engine on the 7 and 7 plus is a different more powerful one.
I can't seem to find a way to see if the device supports using the new api. I would like to replace some vibrate code with taptic code, where it makes sense.
Edit:
Adding the 3 concrete subclasses for search purposes:
UIImpactFeedbackGenerator
UINotificationFeedbackGenerator
UISelectionFeedbackGenerator
Edit 2:
I have a theory but no iPhone 7 device to test it so if you have one, give it a shot. UIFeedbackGenerator has a methods called prepare(). When printing out an instance of UIImpactFeedbackGenerator, I noticed that it printed a property named "prepared" which would show 0. Calling prepare() in simulator or on iPhone 6S and then printing out the instance still shows prepared as 0. Can someone call prepare() on an instance of UIImpactFeedbackGenerator from an iPhone7 and then print the instance to console to see if prepared is set to 1? This value is not exposed but there may be a way to get this info w/o using private apis.
So, apparently this can be done with a private API call.
Objective-C:
[[UIDevice currentDevice] valueForKey:#"_feedbackSupportLevel"];
Swift:
UIDevice.currentDevice().valueForKey("_feedbackSupportLevel");
... These methods seem to return:
0 = Taptic not available
1 = First generation (tested on an iPhone 6s) ... which does NOT support UINotificationFeedbackGenerator, etc.
2 = Second generation (tested on an iPhone 7) ... which does support it.
Unfortunately, there are two caveats here:
Using these could get your app rejected by Apple during the App Store's App Review, but there doesn't seem to be any other way currently.
We don't know what the actual values represent.
Special thanks to Tim Oliver and Steve T-S for helping test this with different devices. https://twitter.com/TimOliverAU/status/778105029643436033
Currently, the best way is to check the device's model using:
public extension UIDevice
public func platform() -> String {
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
}
The platform names for iPhone 7 and 7 plus are: "iPhone9,1", "iPhone9,3", "iPhone9,2", "iPhone9,4"
Source: iOS: How to determine the current iPhone/device model in Swift?
You can create a function:
public extension UIDevice {
public var hasHapticFeedback: Bool {
return ["iPhone9,1", "iPhone9,3", "iPhone9,2", "iPhone9,4"].contains(platform())
}
}
I have extended chrisamanse's answer. It extraxts the generation number from the model identifier and checks if it is equal or greater than 9. Should work with future iPhone models unless Apple decides to introduce a new internal naming scheme.
public extension UIDevice {
var modelIdentifier: String {
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
var hasHapticFeedback: Bool {
// assuming that iPads and iPods don't have a Taptic Engine
if !modelIdentifier.contains("iPhone") {
return false
}
// e.g. will equal to "9,5" for "iPhone9,5"
let subString = String(modelIdentifier[modelIdentifier.index(modelIdentifier.startIndex, offsetBy: 6)..<modelIdentifier.endIndex])
// will return true if the generationNumber is equal to or greater than 9
if let generationNumberString = subString.components(separatedBy: ",").first,
let generationNumber = Int(generationNumberString),
generationNumber >= 9 {
return true
}
return false
}
}
Use it like so:
if UIDevice.current.hasHapticFeedback {
// work with taptic engine
} else {
// fallback for older devices
}
class func isFeedbackSupport() -> Bool {
if let value = UIDevice.current.value(forKey: "_feedbackSupportLevel") {
let result = value as! Int
return result == 2 ? true : false
}
return false
}
Currently I am using following method to display a remote image of 100pt x 100pt (for example), I first detect iOS device scale and generate a dynamic URL, as following:
private func getImageURL() -> String {
let screen = UIScreen.mainScreen()
if (!screen.respondsToSelector(Selector("scale"))) {
return "http://cdn.domain.by/100x100/image.jpg"
}
if (screen.scale > 2) {
return "http://cdn.domain.by/300x300/image.jpg" // iPhone 6+
}
return "http://cdn.domain.by/200x200/image.jpg"
}
Above approach works fine, but since latest Swift update, I get following warning message:
Replace Selector("scale") with #selector(NSDecimalNumberBehaviors.scale)
Question 1: Is it safe to remove these lines:
if (!screen.respondsToSelector(Selector("scale"))) {
return "http://cdn.domain.by/100x100/image.jpg"
}
Question 2: Should I detect device scale? or even theres a better approach for generating such URL
While an Xcode UI Test is running, I want to know which device/environment is being used (e.g. iPad Air 2, iOS 9.0, Simulator).
How can I get this information?
Using Swift 3 (change .pad to .phone as necessary):
if UIDevice.current.userInterfaceIdiom == .pad {
// Ipad specific checks
}
Using older versions of Swift:
UIDevice.currentDevice().userInterfaceIdiom
Unfortunately there is no direct way of querying the current device. However you can work around by querying the size classes of the device:
private func isIpad(app: XCUIApplication) -> Bool {
return app.windows.elementBoundByIndex(0).horizontalSizeClass == .Regular && app.windows.elementBoundByIndex(0).verticalSizeClass == .Regular
}
As you can see in the Apple Description of size classes, only iPad devices (currently) have both vertical and horizontal size class "Regular".
You can check using the windows element frame XCUIApplication().windows.element(boundBy: 0).frame and check the device type.
You can also set an extension for XCUIDevice with a currentDevice property:
/// Device types
public enum Devices: CGFloat {
/// iPhone
case iPhone4 = 480
case iPhone5 = 568
case iPhone7 = 667
case iPhone7Plus = 736
/// iPad - Portraite
case iPad = 1024
case iPadPro = 1366
/// iPad - Landscape
case iPad_Landscape = 768
case iPadPro_Landscape = 0
}
/// Check current device
extension XCUIDevice {
public static var currentDevice:Devices {
get {
let orientation = XCUIDevice.shared().orientation
let frame = XCUIApplication().windows.element(boundBy: 0).frame
switch orientation {
case .landscapeLeft, .landscapeRight:
return frame.width == 1024 ? .iPadPro_Landscape : Devices(rawValue: frame.width)!
default:
return Devices(rawValue: frame.height)!
}
}
}
}
Usage
let currentDevice = XCUIDevice.currentDevice
Maybe someone would be come in handy the same for XCTest on Objective C:
// Check if the device is iPhone
if ( ([[app windows] elementBoundByIndex:0].horizontalSizeClass != XCUIUserInterfaceSizeClassRegular) || ([[app windows] elementBoundByIndex:0].verticalSizeClass != XCUIUserInterfaceSizeClassRegular) ) {
// do something for iPhone
}
else {
// do something for iPad
}
Swift: 5.2.4
Xcode: 11.6
var isiPad: Bool {
return UIDevice.current.userInterfaceIdiom == .pad
}
With iOS13+, you can now use UITraitCollection.current to get the complete set of traits for the current environment. (This is the "iOS interface environment for your app, including traits such as horizontal and vertical size class, display scale, and user interface idiom." doc)
In your case, you can access its property .userInterfaceIdiom to check for one of the device types in the UIUserInterfaceIdiom enumeration.
As an aside, if you just want to get the horizontal/vertical size classes of the trait collection, you can be more backwards compatible with your tests (Xcode 10.0+) just by accessing myXCUIElement.horizontalSizeClass and .verticalSizeClass within your test, as they are exposed via the XCUIElementAttributes protocol that all UI elements adopt. (Note though that I was getting .unspecified when calling off of XCUIApplication(); best to use a real UI element in a window. If you don't have one on hand, you can always still use something like app!.windows.element(boundBy: 0).horizontalSizeClass == .regular as mentioned in the past.)
I'm using Xamarin with MvvmCross to create an iPad application. In this application I use the PictureChooser plugin to take a picture with the camera. This all occurs in the way that can be seen in the related youtube video.
The code to accomplish this is fairly simple and can be found below. However when testing this on the actual device, the camera might be rotated.
private readonly IMvxPictureChooserTask _pictureChooserTask;
public CameraViewModel(IMvxPictureChooserTask pictureChooserTask)
{
_pictureChooserTask = pictureChooserTask;
}
private IMvxPictureChooserTask PictureChooserTask { get { return _pictureChooserTask; } }
private void TakePicture()
{
PictureChooserTask.TakePicture(400, 95,
async (stream) =>
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
var imageBytes = memoryStream.ToArray();
if (imageBytes == null)
return;
filePath = ProcessImage(imageBytes, FileName);
}
},
() =>
{
/* no action - we don't do cancellation */
}
);
}
This will lead to unwanted behavior. The camera should remain steady and be prevented in rotating within the App. I have been trying some stuff out, like preventing the app from rotating in the override bool ShouldAutorotate method while in camera mode, but unfortunately without any results.
Is there any setting that I forgot to set on the PictureChooser, or is the override method the item where I should perform some magic?
Thanks in advance.
Answer to this question has been raised in the comments of the question by user3455363, many thanks for this! Eventually it seemed to be a bug in iOS 8. The iOS 8.1 upgrade fixed this issue in my App!