简体   繁体   中英

Using different files for the same project on different devices in iOS?

Say if I have 3 animation files for a size for iPhone 5s, iPhone 6 and iPhone 6 Plus. What about ipad?

For the same view controller, how should I use these respective files for different iPhone sizes. Here's a specific example:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@”iphone5s” ofType:@”gif”];

but what if I want to load for iPhone 6, iPhone 6 plus called iPhone6.gif , iPhone6Plus.gif etc

I am using Objective-C.

You can use SDVersion to detect the current device information . SDVersion is :

Objective-C library for detecting the running device's model and screen size

So you can use it to get device model , device screen size , device name ,and iOS Version as follow:

  // Check for device model
  if ([SDVersion deviceVersion] == iPhone6)
       NSLog(@"You got the iPhone 6. Sweet 🍭!");
  else if ([SDVersion deviceVersion] == iPhone6Plus)
       NSLog(@"iPhone 6 Plus? Bigger is better!");
  else if ([SDVersion deviceVersion] == iPadAir2)
       NSLog(@"You own an iPad Air 2 🌀!");

  // Check for device screen size
  if ([SDVersion deviceSize] == iPhone4inch)
       NSLog(@"Your screen is 4 inches");

  // Get device name
  NSLog(@"%@", stringFromDeviceVersion([SDVersion deviceName]));
  /* e.g: Outputs 'iPhone 6 Plus' */

  // Check for iOS Version
  if (iOSVersionGreaterThanOrEqual(@"8"))
       NSLog(@"You are running iOS 8 or above!");

In your situation , you can:

NSString *name;
DeviceSize size = [SDVersion deviceSize];

if (size == iPhone35inch)
    name = @"iPhone4";
else if(size == iPhone4inch)
    name = @"iPhone5";
else if(size == iPhone47inch)
    name = @"iPhone6";
else if(size == iPhone55inch)
    name = @"iPhone6Plus";
else
    name = @"iPad";


NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];

Try using specific image file names with appropriate naming conventions. Like,

     - fileName.png -> iPhone (non-retina/fallback)
     - fileName@2x.png -> iPhone (retina)
     - fileName@3x.png -> iPhone 6 Plus (retina)
     - fileName~ipad.png -> iPad (non-retina)
     - fileName@2x~ipad.png -> iPad (retina)

So, in code we can use:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@”fileName” ofType:@”png”];

And internally OS will detect the device version and pick the respective image to showcase.

I have used this using storyboard image assigning, I have not tried with codebase, but you can give a try.

Hope this helps.

You can use uname from sys/utsname.h for getting device model.

Take a look at this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM