Proper method to detect device model (iPhone/iPod Touch)? - iphone

Is this the proper way to detect which device a user is running?
NSString *currentModel = [[UIDevice currentDevice] model];
if ([currentModel isEqualToString:#"iPhone"]) {
// The user is running on iPhone so allow Call, Camera, etc.
} else {
// The user is running on a different device (iPod / iPad / iPhone Simulator) disallow Call.
}

It is not a general solution but Apple in many cases provides API calls to check wether specific feature is supported or not. Examples could be:
+isSourceTypeAvailable: and +availableMediaTypesForSourceType: in UIImagePickerController allowing you to check if camera is available for the current device.
+canSendMail in MFMailComposeViewController to check if device is configured to send mail.
-canOpenURL in UIApplication class to check if URL can be opened. For example it can be used to check if it is possible to make a phone call:
if (![[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:#"tel://"]])
//We cannot make a call - hide call button here
If such API calls are available for your purpose I would use them rather then rely on hardcoded string identifiers.

I'm not sure I'd want to generalize that much (ie, there may eventually be an iPod with a camera, and I don't know that the iPhone will ALWAYS be called "iPhone"), but yes, this is the accepted way.

Related

best practice to write app both for ios4.3, ios5 and ipad

I am just started write app for ios.
I have 2 questions.
I know there are some codes which work fine in ios4.3 but don't work in ios5 and the opposite is also true.I want to know,which is the best practice for writing app both for ios4.3 and ios5?Is it acceptable run-time to check the version for specific parts of code??
I am also should write same app for ipad. So, I want to know which is the best practice for writing app for iphone which works also on ipad??
Thanks
iOS 5 or iOS 4
Another way to detect the version is to use the respondsToSelector: message on objects. The advantage compared to the version method is that you don't need to know what the next versions of iOS are gonna be to maintain your application. (What if, for instance, a new 5.0.2 version shows up? Your app should know that the "5.0.2" string is newer than "5.0"? I agree it would not be complicated to code, but using respondsToSelector: is much more convenient)
iPhone/iPod Touch or iPad
My usual way to code universal app (ie iPhone and iPad) is to define a basic implementation of my custom UIViewController classes, and then implement an HD version of it, inheriting the default behavior but customizing it for the iPad (overriding methods works well but the delegate pattern might be better: you get compilation warnings if you forget to implement methods).
If you use a different .xib file from the beginning of your application lifecycle (that's what you get by universal Xcode-provided templates), you may end up defining classes just from Interface Builder, and won't need to implement any runtime test in your code to know if you're running on an iPhone or an iPad.
You can get the version using:
[[UIDevice currentDevice] systemVersion];
The systemVersion returns a string, like "5.0". You can then compare the strings using string comparison. For example:
NSString *requiredVersion = #"5.0";
NSString *currentVersion = [[UIDevice currentDevice] systemVersion];
if ([currentVersion compare:requiredVersion options:NSNumericSearch] != NSOrderedAscending)
isItSupported = TRUE;
The documentation indicates that you can use the following to determine if you have an iPhone or iPad:
[[UIDevice currentDevice] model];
I've not used this though.
See the documentation.

How to silence iPhone camera shutter sound?

I can snap a picture with the iPhone programmatically by calling [UIImagePickerController takePicture:], but when I do the iPhone plays a loud recording of a shutter click. When I google for how to turn off the click, I find advice to rename the sound file that the iPhone plays. It seems to me for my app to do that would lead to it being rejected from the App store for accessing system frameworks. Is there a programmatic way to shut off that sound? The nature of my app demands that the camera be silent.
I assume you have solved it since but your app supposed to fail on the Appstore validation as it doesn't comply with iOS Dev License agreement. See below:
Section 3.3.8: Any form of user or device data collection, or image,
picture or voice capture or recording (collectively "Recordings"), and
any form of data, content or information collection, processing,
maintenance, uploading, syncing, storage, transmission, sharing,
disclosure or use performed by, through or in connection with Your
Application must comply with all applicable privacy laws and
regulations as well as any related Program Requirements, including but
not limited to any notice or consent requirements. In particular, a
reasonably conspicuous audio, visual or other indicator must be
displayed to the user as part of the Application to indicate that a
Recording is taking place.
Not sure if you would want to do it...
The sound is there to let someone know a photo is being taken. The idea is to ensure privacy and safety of the public, especially children," something that Japan has already required of their snap-happy citizens
Japan and Korea already have laws that require this sound when taking pictures.
http://abcnews.go.com/Technology/story?id=6750825&page=1
excerpt:
"In Japan and Korea, Segan pointed out, in response to mounting reports of "underskirting," governments have passed laws similar to the one King proposes."
Renaming the sound file wouldn't be using a "private API"; it's simply not possible from within the sandbox (assuming you haven't broken out of the sandbox somehow).
However, on 4.0+, you can use AVCapture to take pictures instead. I'm not sure if AVCaptureStillImageOutput plays a shutter sound; a workaround is to use video frames.
I have to wonder what you mean by "the nature of my app" though. If you're trying to do some sort of live image processing, then video frames are a much better way to go in the first place. If you're trying to take pictures silently with the user's permission, then the user should be able to silence the shutter sound anyway. If you're trying to take pictures without the user's permission, you're probably violating some agreement with Apple.
For what it's worth, I was able to get this to work by using this code in the snapStillImage method of AVCapture framework using AVCaptureStillImageOutput. It works perfectly for me on iOS 8.3 iPhone 5. I have also confirmed that Apple won't reject your app if you use this:
MPVolumeView* volumeView = [[MPVolumeView alloc] init];
//find the volumeSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:#"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}
[volumeViewSlider setValue:0.0f animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
Swift 4:
var volumeView = MPVolumeView()
//find the volumeSlider
var volumeViewSlider: UISlider? = nil
for view: UIView in volumeView.subviews {
if (view.self.description == "MPVolumeSlider") {
volumeViewSlider = view as? UISlider
break
}
}
volumeViewSlider?.setValue(0.0, animated: true)
volumeViewSlider?.sendActions(for: .touchUpInside)

Turing off functionality dpending on device iPhone, or iPod touch

I have what i thought was a relatively simple question but i cannot find an answer to it yet. I have an iPhone app that uses GPS on one of its screens. I want to disable this screen using code when the app loads,so disable it when a iPod touch is being used. This is so it can still be useful on a iPod touch as there is a lot of functionality that a iPod touch user can use.
Thanks.
You can get there with #Aaron's answer, but that's not the way to do it. Use [CLLocationManager locationServicesEnabled]; to tell if you can determine the users's location. This is a lot more robust than making decisions based on the device model.
to get the device info..
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html
NSString *deviceType = [UIDevice currentDevice].model;
I think if you are just checking for GPS then you will need to access the CLLocationManager to see if it is on or off

Presenting a walkway from position A to B in my app

So I have an iPhone app which should aid the user to find a convenient walkway from his/her own position to a given destination. As I have learnt, MKMapView does not provide an easy way to infer a preferred walking route from A to B.
I can live with terminating my own app and launch the native map application on the iPhone, but in that case I would like to equip the map application with two coordinates so that the user can find his/her way.
Any suggestions to how I should go about this task?
To launch the native map app use:
NSString *googleMapsURL = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURL];
where start is the user location and destination is, well, the destination. For walking directions, you can add &dirflg=w (still in beta according to wiki). Here are some more parameters you can use.

iPhone/iPod capabilities detection?

Does anyone know the recommended way to check whether the device supports specific capabilities, for example the camera. I know I can detect the device the app is on with UIDevice but I was wondering if there is a way to enumerate the device's capabilities?
I'm not sure if there is a way to enumerate all of the device's capabilities. Usually, this check is done on a capability by capability basis.
So, to use your example, if you would like to know if the device you are running on has the capability to take a picture, you would:
[UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera];
This would return true (YES) for any iPhone, and false (NO) for any iPod Touch (at least at the time of this writing).