All posts by JazzerCoder

iPhone Device & Screen Sizes and Resolutions +Xr

To get the screen dimensions (in points) of the current device:

Objective-C: 
CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
// Macros: 
#define screen_width [ [ UIScreen mainScreen ] bounds ].size.width #define screen_height [ [ UIScreen mainScreen ] bounds ].size.height 
Swift: 
let screenBounds = UIScreen.main.bounds 
let screen_width = screenBounds.width 
let screen_height = screenBounds.height 

To get the screen scale:

Objective-C: 
CGFloat screenScale = [[UIScreen mainScreen] scale]; Swift: let screenScale = UIScreen.main.scale

Non-retina devices have a scale of 1.0. Retina devices have a scale of 2.0 or 3.0.

Some dimensions common to all screen sizes:

Status Bar
20 pts
Navigation Bar44 pts
Nav Bar/Toolbar Icon20 x 20 pts (transparent PNG)
Tab Bar49 pts
Tab Bar Icon30 x 30 pts (transparent PNGs)

Points vs. Pixels

Apple introduced retina displays starting with the iPhone 4. You don’t have to modify your code to support high-res displays; the iOS coordinate system uses points rather than pixels, so the dimensions and position in points of all UI elements remains the same across all devices.

iOS supports high resolution displays via the scale property on UIScreen, UIView, UIImage, and CALayer classes. If you load an image from a file whose name includes the @2x modifier, its scale property is set to 2.0. Similarly an image with a @3x modifier has a scale of 3.0. Otherwise the scale defaults to 1.0.

Retina Graphics

To support high-resolution graphics on devices with retina displays, you need @2x and @3x sized images:

@1x:
button.png
60 x 20
@2x:
button@2x.png
120 x 40
@3x:
button@3x.png
180 x 60

To refer to an image in your code (or in Interface Builder), just use the filename of the standard sized image. iOS will automatically detect and use the @2x or @3x version if the device supports it:

Objective-C: 
<br>imageView.image = [UIImage imageNamed: @"button.png"]; Swift: imageView.image = UIImage(named: @"button.png") 

Send an Email from App with Android Intent

Send an Email from App with Android Intent

Intent emailIntent = new Intent(Intent.ACTION_SEND); 

// The intent does not have a URI, so declare the "text/plain" MIME type
emailIntent.setType("text/html");emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"david@google.com"}); // recipientsemail

Intent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text...");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));    

// You can also attach multiple items by passing an ArrayList of Uris
startActivity(emailIntent);Log.v("*** MainActivity ***","Intent method called");

How to use Swift classes all across an Objective-C project

The most universal solution is to change SWIFT_OBJC_INTERFACE_HEADER_NAME (“Objective-C Generated Interface Header Name”) under Project’s, not Targets, Build Settings, to:

  • $(PROJECT_NAME)-Swift.h — one per project;
  • $(SWIFT_MODULE_NAME)-Swift.h — one per module (default value).

Then import the Project Header, example:

#import MyProjectName-Swift.h

Sizes of iPhone UI Elements

Taken from idev101 more in: http://www.idev101.com/code/User_Interface/sizes.html

iPhone Development 101: User Interface:
Sizes of iPhone UI Elements

How to detect the current device size and kind

Element iPhone 4S (and earlier) iPhone 5 iPhone 6 & 7 iPhone 6 Plus & 7 Plus
Window (including status bar area) 320 x 480 pts 320 x 568 pts 375 x 667 pts 414 x 736 pts
iOS8 Portrait Keyboard (English)
with QuickType
320 x 253 pts 320 x 253 pts 375 x 258 pts 414 x 271 pts
iOS8 Portrait Keyboard (English)
without QuickType
320 x 224 pts 320 x 224 pts 375 x 225 pts 414 x 236 pts
iOS8 Landscape Keyboard (English)
with QuickType
480 x 193 pts 568 x 193 pts 667 x 194 pts 736 x 194 pts
iOS8 Landscape Keyboard (English)
without QuickType
480 x 170 pts 568 x 170 pts 667 x 171 pts 736 x 171 pts
Launch Image Sizes 640 x 960 pixels 640 x 1136 pixels 750 x 1334 (@2x) portrait
1334 x 750 (@2x) landscape
1242 x 2208 (@3x) portrait
2208 x 1242 (@3x) landscape

Other dimensions common to all screen sizes:

Status Bar
(How to hide the status bar)
20 pts
Navigation Bar 44 pts
Nav Bar/Toolbar Icon white icon – up to 20 x 20 pts (transparent PNG)
Tab Bar 49 pts
Tab Bar Icon up to 30 x 30 pts (transparent PNGs)
Text Field 31 pts

Points vs. Pixels

Apple introduced retina displays starting with the iPhone 4. You don’t have to modify your code to support high-res displays; the iOS coordinate system uses points rather than pixels, so the dimensions and position in points of all UI elements remains the same across all devices.

iOS supports high resolution displays via the scale property on UIScreen, UIView, UIImage, and CALayer classes. If you load an image from a file whose name includes the @2x modifier, its scale property is set to 2.0. Similarly an image with a @3x modifier has a scale of 3.0. Otherwise the scale defaults to 1.0.

Vary for Traits in Xcode 8

Vary for Traits in Xcode 8

The UI to select the size classes has changed in xcode 8. Now it is as follows:

Any constraints that you add without selecting Vary for Traits options will be considered as for all the size classes( Any Any previously)

enter image description here

To add a constraint to a specific size class add it by selecting:

  • The device at the bottom and by selecting Vary for Traits options of that size class.
  • When you are adding constraints to a specific size class the bar turns blue as below

enter image description here

How to debug deep linking in iOS

What is deep linking?

Deep linking basically enables you to open an app from another app or a website while passing on parameters. This mechanism works with custom URL schemes which you can define in the .plist of your app.

Why deep linking?

If you have an app with lookup functionality (e.g. an app for finding song lyrics) you might want to perform a lookup without having to navigate to the designated screen and typing in the query. This can be achieved by opening the following example URL MyAppScheme://myAction=lookup&myQuery=What%20is%20the%20meaning%20of%20life in another app of the browser. In this case the app is programmed to read the ‘myAction’-parameter on startup to determine the action and the ‘myQuery’-parameter for the search query. Often, deep linking doesn’t go as planned and you might need to debug your app.

Why is debugging this difficult?

Deep linking might occur in three kinds of scenarios:

  1. The app hasn’t been installed yet
  2. The app is running in the background (or foreground for that matters)
  3. The app is installed but not running at all

Scenario 1 is irrelevant in this case, so we’ll skip this one. Scenario 2 should be no problem since the app is running on the device (or simulator) while in a debug session. Scenario 3 requires you not to have the app running, but you need the debug session to be able to debug. This is a problem because Xcode starts the app automatically when starting a debug session. Instead of letting Xcode start the app, you want to do it yourself using the deep link URL.

So… tell me how to do this

There is a convenient option which enables you to start a debug session by manually starting the app. To achieve this, go to the ‘Edit scheme’ screen and tick the ‘Wait for MyApp.app to be launched manually’ option on in the ‘Run’ configuration.

Image

Whenever you run the target in Xcode, the app doesn’t automatically starts and the debug session will only start when you manually start the app.