Does anybody know if there is an xcode unlock event that can track if somebody unlocks his iPhone?
Or is this only possible with jailbroken devices?
Thanks in advance.
Yes you can do this on a non-jailbroken device using CFNotificationCenterAddObserver.
Add an observer for the Darwin notification 'lockstate':
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
lockStateDidNotify,
CFSTR("com.apple.springboard.lockstate"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
and you will be notified each time the device locks/unlocks:
static void lockStateDidNotify(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
NSLog(#"The Device Locked/Unlocked");
}
Since this fires for both a lock and an unlock it is not entirely what you want, but you can observe com.apple.springboard.lockcomplete (which only fires during a lock) and check to see if you get both. If you don't get the lockcomplete you can assume an unlock just happened.
Related
I'm working on an iPhone app and I'd like to disable phone but keep wifi working. For that i am using the code below:
`-(void) disablePhone
{
void *libHandle = dlopen(”/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony”, RTLD_LAZY);
int (*enable)(int mode) = dlsym(libHandle, “CTPowerSetAirplaneMode”);
enable(1);
}
-(void) enablePhone
{
void *libHandle = dlopen(”/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony”, RTLD_LAZY);
int (*enable)(int mode) = dlsym(libHandle, “CTPowerSetAirplaneMode”);
enable(0);
}`
But it is getting crashed at enable(1) and enable(0) lines.
Can anybody help me why its getting crashed?
or
is there any other solution through which I can change the mode of iphone to flight mode without disable wifi.
Thanks in advance
Due to the sandboxing of iOS apps, I would doubt this is acceptable to Apple. I would instruct the user to do it manually; it seems like the only way. They can turn off the cellular network in settings without turning on airplane mode.
I've been going through the audio session categories and overrides, and it appears that you can either play audio regardless of whether the silent switch is set or the screen is locked (AVAudioSessionCategoryPlayback), OR you can respect the silent switch and the screen lock settings (AVAudioSessionCategorySoloAmbient).
Any idea how I can play audio that gets muted by the silent switch, but keeps playing when the screen is locked? All I want is to have my cake and eat it too.
To answer the inevitable "why would you ever want to circumvent the system" questions, this is for an app that shows words and plays music which is likely to be used in both a church and home setting. At church it would be bad if the app started playing music, hence I want to respect the mute switch. At home you might set the phone down to listen to music while doing something else, and you wouldn't want it to stop playing when the phone auto-locks.
You can use AVAudioSessionCategoryPlayback and use this code to check if silent switch is on or off:
- (BOOL)deviceIsSilenced
{
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) <= 0);
}
I'm making a game with gamecenter support. I want to diable a button for iPhone 3g or older devices. But my 3g test device says, that gamecenter is available and the matchmaking view show up. The user will never get authenticated. I use the snippet from apple to check, if gamecenter is available. It should return NO on devices older than 3gs
-(BOOL)isGameCenterAvailable {
// check for presence of GKLocalPlayer API
Class gcClass = (NSClassFromString(#"GKLocalPlayer"));
// check if the device is running iOS 4.1 or later
NSString *reqSysVer = #"4.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer
options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported); }
Does anyone do it better than apple?
Apparently, there is more to detecting Game Center support than just checking for the existence of the API. According to Optionally Supporting Game Center In Your Game, in addition to checking for the APIs you'll also need to authenticate the player:
iOS 4.1 may be installed on some
devices that do not support Game
Center. On those devices, the
isGameCenterAPIAvailable function
defined Listing 1-1 still returns YES.
The next step in confirming whether
Game Center may be used on the device
is to attempt to authenticate the
local player; on devices that do not
support game center, your game
receives a GKErrorNotSupported error.
I'm developing a camera app to snap photo when the volume button is pressed.
I used AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume, audioVolumeChangeListenerCallback, self); to successfully get notified when a volume button get pressed before my camera started. However, after I started the camera, this property listener no longer works. Is it get auto deactivated or something?
I have tried to implement a custom UIImagePickerController to include the Audio Session Property Listener inside the ImagePickerController but with no hope. Grateful if anyone can share your view. Thanks.
I'm sorry to say that this is something that is not supported in IOS4. If you managed to implement this successfully the app will get rejected by apple.
However is was announced at WWDC 2011 that this is fully supported in IO5 and will be default behaviour when using UIImagePickerController in any app.
Hope this helps.
Even in the default camera app on the iphone, turning up/down the volume is not supported. May be they block the input from those buttons when the camera is turned on. What they dont block is the silent switch. I added this notification and my callback was called even with the camera on:
AudioSessionInitialize(nil, nil, nil, nil);
AudioSessionSetActive(true);
AudioSessionAddPropertyListener(
kAudioSessionProperty_AudioRouteChange,
applicationAudioRouteDidChange,
self);
The problem you need to solve is that the 'applicationAudioRouteDidChange' method will also be called if someone inserts the headphones. You can check this as said in the AudioSessionProgrammingGuide:
When the system invokes a route-change callback, it provides the
information you need to figure out which action to take. Base your
callback on the AudioSessionPropertyListener prototype from audio
session services, as shown here:
void MyPropertyListener ( void *inClientData, AudioSessionPropertyID inID,UInt32 inDataSize, const void *inData );
For a route change event, the
system sends the kAudioSessionProperty_AudioRouteChange in the inID
parameter.
The inData parameter sent to your callback contains a CFDictionaryRef
object that describes:
Why the route changed What the previous route was
I did this and it takes the picture when silent switch is mmmm switched:
void applicationAudioRouteDidChange(void *inClientData,
AudioSessionPropertyID inID,
UInt32 inDataSize, const void *inData)
{
if ([[(NSDictionary*)inData objectForKey:#"OutputDeviceDidChange_Reason"] intValue] == 5) {
[((RootViewController*)inClientData).picker takePicture];
}
// Do something like reset the system
}
As mentioned by Tom, the functionality you are trying to implement would be a part of iOS 5.
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.