Is there any property or other mechanism in iPhone OS to check during runtime whether application is running on iPad 3G or iPad Wi-Fi?
Seems like UIDevice class does not provide anything like that.
My application is using internet access extensively and I would like to explicitly warn user that on 3G delays or additional costs can be expected OR even ban application from running on iPad 3G with some fancy popup.
I assume that other than 3G networking capabilities, there is no need to make a difference. Using Reachability.h class provided by Apple you can check if internet connection is available and if it is Mobile network or Wireless network.
Sample code here:
http://developer.apple.com/iphone/library/samplecode/Reachability/Introduction/Intro.html
The Reachability class provides the following values:
ReachableViaCarrierDataNetwork, ReachableViaWiFiNetwork or NotReachable.
You can differentiate between WiFi and 3G iPads if your app is running on a second generation iPad:
+ (NSString *) iPadModelName
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
free(machine);
if ([platform isEqualToString:#"iPad2,1"]) return #"iPad 2 (WiFi)";
if ([platform isEqualToString:#"iPad2,2"]) return #"iPad 2 (GSM)";
if ([platform isEqualToString:#"iPad2,3"]) return #"iPad 2 (CDMA)";
return platform;
}
Related
I'm developing an app which can only be installed in iPhone 4 and later versions.I also go through UIRequiredDeviceCapabilities and Device Compatibility Matrix.I need more solution.
Well, first of all you can receive the model as a string using the UIDevice class:
[[UIDevice currentDevice] model]
This will return something like "iPod touch" or "iPhone".
You can get the exact model platform using the following code:
(you have to #include <sys/types.h> and <sys/sysctl.h>)
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = #(machine); // Old syntax: [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]
free(machine);
Now platform is a string containing the generation of the device, e.g.:
iPhone1,1 for the iPhone 2G (the first iPhone)
iPhone1,2 for the iPhone 3G
iPhone2,1 for the iPhone 3GS
iPhone3,1 or iPhone3,2 for the iPhone 4
iPhone4,1 for the iPhone 4S
iPhone5,1 or iPhone5,2 for the iPhone 5
Note that the number in front of the comma of the iPhone 4 platform is actually 3, not 4.
Using this string you can isolate this number and check if it is greater than or equal to 3:
if ([[[UIDevice currentDevice] model] isEqualToString:#"iPhone"]) {
if ([[[platform componentsSeparatedByString:#","][0] substringFromIndex:6] intValue] >= 3) {
// Device is iPhone 4 or newer
} else {
// Device is older than iPhone 4
}
}
However: You can actually check the screen's scale, since the iPhone 4 is the first iPhone with a retina display:
[[UIScreen mainScreen] scale] // Returns 2.0f on the iPhone 4 and newer and 1.0f on older devices
Actually I can detect the iOS version but it is possible to detect a iPhone model 3G or 3GS inside iOS?
Thanks
You need to check for device capabilities. The 3g does not support video recorder and the 3gs does not support front facing camera.
Take a look to this question.
You can find the exact model string using this post (which I think is also a duplicate question):
#include <sys/types.h>
#include <sys/sysctl.h>
#implementation UIDevice (Hardware)
/*
Platforms
iPhone1,1 -> iPhone 1G
iPhone1,2 -> iPhone 3G
iPod1,1 -> iPod touch 1G
iPod2,1 -> iPod touch 2G
*/
-(NSString *) platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}
While it's not good practise to find out and use this information in your application, is there a way to find what model of iPhone / iPod / iPad you have. For example: 2G/3GS/4G etc
Try:
char deviceString[256];
size_t size = 255;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
if (size > 255) { size = 255; }
sysctlbyname("hw.machine", deviceString, &size, NULL, 0);
if (strcmp(deviceString,"iPhone1,1") == 0) { etc... } // 2G
1,2 is a 3G,
2,1 is a 3GS,
3,1 is an i4, etc.
I think that this is already answered here: Determine device (iPhone, iPod Touch) with iPhone SDK, though I've added a bit to it:
- (NSString *) platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:#"iPhone1,1"]) return #"iPhone 1G";
if ([platform isEqualToString:#"iPhone1,2"]) return #"iPhone 3G";
if ([platform isEqualToString:#"iPhone2,1"]) return #"iPhone 3GS";
if ([platform isEqualToString:#"iPhone3,1"]) return #"iPhone 4";
if ([platform isEqualToString:#"iPod1,1"]) return #"iPod Touch 1G";
if ([platform isEqualToString:#"iPod2,1"]) return #"iPod Touch 2G";
if ([platform isEqualToString:#"iPod3,1"]) return #"iPod Touch 3G";
if ([platform isEqualToString:#"iPod4,1"]) return #"iPod Touch 4G";
if ([platform isEqualToString:#"iPad1,1"]) return #"iPad";
if ([platform isEqualToString:#"i386"]) return #"iPhone Simulator";
return platform;
}
to account for recent additions to the family. You can checkout everyipod.com, for example, specs for iPhone 4 to get platform strings.
-[UIDevice model], but I'm not sure if it returns anything more specific than "iPhone" or "iPod Touch".
I thought iTunes/Xcode Organizer did this already for some reason (at least I seem to remember it correctly identifying my old iPod Touch as a 1st gen), that's definitely not the case for my iPhone 3GS. Nor does the iPhone Configuration Utility help.
So I fired up System Profiler to see if the device shows up on the USB list; it does. It also shows the "Product ID" (in my case, 0x1294). I typed that into Google and came up with this:
http://theiphonewiki.com/wiki/index.php?title=Normal_Mode
Device IDs
It appears that it uses different device IDs:
iPhone - 0x1290
iPod touch - 0x1291
iPhone 3G - 0x1292
iPod touch 2G - 0x1293
iPhone 3GS - 0x1294
iPod touch 3G - 0x1299
iPad - 0x129a
iPhone 4 -
iPod touch 4G - 0x129e
Apple TV 2G -
There are many articles on how to detect if a microphone is connected to an iPod touch G2 via AudioSessionGetProperty / kAudioSessionProperty_AudioInputAvailable, but I have not seen any articles related to detection of headphones connected to an iPod touch G1.
To review:
iPod touch G2 hardware differs from iPod touch G1 hardware in the following ways:
iPod touch G2 has an internal speaker
iPod touch G2 is able to use microphone off of headphone port
I have an app that needs to play sound to be useful and I want to be nice and have a detector that shows that the app is useful once they connect up some headphones.
My initial trials show that the AudioSession APIs (and specifically the AudioSessionGetProperty with the kAudioSessionProperty_AudioRoute constant) always reports back 'Headphone' even if headphones are not connected to an iPod touch G1.
Am I missing something? Do I have something cross wired with my AudioSession calls? If anyone has tried this on an iPod touch G1 and got a different result? Is there another way to weave through AudioSession APIs and get what I am after?
This is all against iPhone OS 3.0 and the iPhone OS 3.0 SDK on real iPod touch G1 hardware.
Thanks in advance,
--Batgar
you can easily get with this method:
- (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
if (!error && (route != NULL) && ([route isEqual:#"HeadsetInOut"])) {
return YES;
}
return NO;
}
Check it.
http://developer.apple.com/iphone/library/samplecode/SpeakHere/index.html#//apple_ref/doc/uid/DTS40007802
Here is the source code of sound recording which have support for pausing playback when headphones are removed, this may help you.
The kAudioSessionProperty_AudioRoute will always return headphones in the 1st gen since there are no other routes. The 2nd gen and iphone and above all will support another route (speaker) when the headphones are unplugged, but there's note another route in the 1st gen.
At least with this documented API call you are using, you will not be able to detect the 1st gen ipod headphone state.
From SpeakHere
error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
btn_record.enabled = (inputAvailable) ? YES : NO;
Above answer does not work as it Does not compile , So i am posting this as this might help some one . All you need to do is to find the audio route . Following are the possible routes for the audio
Known values of route:
"Headset"
"Headphone"
"Speaker"
"SpeakerAndMicrophone"
"HeadphonesAndMicrophone"
"HeadsetInOut"
"ReceiverAndMicrophone"
"Lineout"
Hope this helps
- (BOOL)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
if (!error && (route != NULL)) {
NSString* routeStr = (__bridge NSString*)route; //Convert CFStringRef to NSString
NSRange routeRange = [routeStr rangeOfString:#"Head"];
if (routeRange.location != NSNotFound){
return YES;
}
}
return NO;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Determine device (iPhone, iPod Touch) with iOS
I am making a game that utilizes the peer-to-peer bluetooth capabilities of the iPhone (and probably the iPod touch 2nd generation). However, to stop the users from trying to play a multiplayer on an iPod 1st gen and iPhone 2G I need to check for the specific device model.
[[UIDevice currentDevice] model] will only tell me if the device is an "iPhone" or an "iPod touch". Is there a way to check for the specific device model, like: "iPhone 3GS", "iPod touch 1st generation" or something.
EDIT:
There is a category to UIDevice (I think it's created by Erica Sadun, I don't take credit for it) that uses the following code to get the specific device model. You can find the whole category here along with other useful stuff: https://github.com/erica/uidevice-extension
#include <sys/types.h>
#include <sys/sysctl.h>
#implementation UIDevice (Hardware)
/*
Platforms
iPhone1,1 -> iPhone 1G
iPhone1,2 -> iPhone 3G
iPod1,1 -> iPod touch 1G
iPod2,1 -> iPod touch 2G
*/
- (NSString *) platform
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}
This works and apps using this have been lately approved in the AppStore.
You can get the device model number using uname from sys/utsname.h. For example:
#import <sys/utsname.h>
NSString*
machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
The result should be:
#"i386" on the simulator
#"iPod1,1" on iPod Touch
#"iPod2,1" on iPod Touch Second Generation
#"iPod3,1" on iPod Touch Third Generation
#"iPod4,1" on iPod Touch Fourth Generation
#"iPhone1,1" on iPhone
#"iPhone1,2" on iPhone 3G
#"iPhone2,1" on iPhone 3GS
#"iPad1,1" on iPad
#"iPad2,1" on iPad 2
#"iPad3,1" on iPad 3 (aka new iPad)
#"iPhone3,1" on iPhone 4
#"iPhone4,1" on iPhone 4S
#"iPhone5,1" on iPhone 5
#"iPhone5,2" on iPhone 5
Most complete UIDevice (Hardware) category probably is http://github.com/erica/uidevice-extension/ (by Erica Sadun):
[[UIDevice currentDevice] platformType] // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: #"iPhone 4G"
How about this code, if new version was released, you will identifier with the last know device
#include <sys/types.h>
#include <sys/sysctl.h>
- (NSString *)getModel {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *model = malloc(size);
sysctlbyname("hw.machine", model, &size, NULL, 0);
NSString *sDeviceModel = [NSString stringWithCString:model encoding:NSUTF8StringEncoding];
free(model);
if ([sDeviceModel isEqual:#"i386"]) return #"Simulator"; //iPhone Simulator
if ([sDeviceModel isEqual:#"iPhone1,1"]) return #"iPhone1G"; //iPhone 1G
if ([sDeviceModel isEqual:#"iPhone1,2"]) return #"iPhone3G"; //iPhone 3G
if ([sDeviceModel isEqual:#"iPhone2,1"]) return #"iPhone3GS"; //iPhone 3GS
if ([sDeviceModel isEqual:#"iPhone3,1"]) return #"iPhone4 AT&T"; //iPhone 4 - AT&T
if ([sDeviceModel isEqual:#"iPhone3,2"]) return #"iPhone4 Other"; //iPhone 4 - Other carrier
if ([sDeviceModel isEqual:#"iPhone3,3"]) return #"iPhone4"; //iPhone 4 - Other carrier
if ([sDeviceModel isEqual:#"iPhone4,1"]) return #"iPhone4S"; //iPhone 4S
if ([sDeviceModel isEqual:#"iPhone5,1"]) return #"iPhone5"; //iPhone 5 (GSM)
if ([sDeviceModel isEqual:#"iPod1,1"]) return #"iPod1stGen"; //iPod Touch 1G
if ([sDeviceModel isEqual:#"iPod2,1"]) return #"iPod2ndGen"; //iPod Touch 2G
if ([sDeviceModel isEqual:#"iPod3,1"]) return #"iPod3rdGen"; //iPod Touch 3G
if ([sDeviceModel isEqual:#"iPod4,1"]) return #"iPod4thGen"; //iPod Touch 4G
if ([sDeviceModel isEqual:#"iPad1,1"]) return #"iPadWiFi"; //iPad Wifi
if ([sDeviceModel isEqual:#"iPad1,2"]) return #"iPad3G"; //iPad 3G
if ([sDeviceModel isEqual:#"iPad2,1"]) return #"iPad2"; //iPad 2 (WiFi)
if ([sDeviceModel isEqual:#"iPad2,2"]) return #"iPad2"; //iPad 2 (GSM)
if ([sDeviceModel isEqual:#"iPad2,3"]) return #"iPad2"; //iPad 2 (CDMA)
NSString *aux = [[sDeviceModel componentsSeparatedByString:#","] objectAtIndex:0];
//If a newer version exist
if ([aux rangeOfString:#"iPhone"].location!=NSNotFound) {
int version = [[aux stringByReplacingOccurrencesOfString:#"iPhone" withString:#""] intValue];
if (version == 3) return #"iPhone4"
if (version >= 4) return #"iPhone4s";
}
if ([aux rangeOfString:#"iPod"].location!=NSNotFound) {
int version = [[aux stringByReplacingOccurrencesOfString:#"iPod" withString:#""] intValue];
if (version >=4) return #"iPod4thGen";
}
if ([aux rangeOfString:#"iPad"].location!=NSNotFound) {
int version = [[aux stringByReplacingOccurrencesOfString:#"iPad" withString:#""] intValue];
if (version ==1) return #"iPad3G";
if (version >=2) return #"iPad2";
}
//If none was found, send the original string
return sDeviceModel;
}
BOOL hasHighResScreen = NO;
if ([UIScreen instancesRespondToSelector:#selector(scale)]) {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0) {
hasHighResScreen = YES;
}
}
iPhone 4 is iPhone3,1 and iPhone3,2
iPhone 4S is iPhone4,1
iPad 2 is iPad2,1 iPad2,2 and iPad2,3 depending on version (GSM etc)
iPad 3 is iPad3,1 iPad3,2 and iPad3,3 depending on version (GSM etc)
See Iphone secrets (scroll down to "internal product codes")
Another good source is:
everyiphone.com
NSString* valueDevice = [[UIDevice currentDevice] model];
and then check if the string is equal to whatever device you are looking for like :
if(value==#"iPod1,1" )
{}
and you should be good to go