Detect lock event on iOS7 - iphone

Greetings for the Day Everyone!!
I want iOS devices with iOS7 version to detect the lock. I did research a lot in it, but didn't find any solution for iOS7.
For Example,
Programmatically lock iPhone screen in iOS 7
Lock Unlock events iphone
I have implemented the same feature for iOS6 but it is not working for iOS7.
Checkout the working snipppet for iOS6
// coming back in the foreground
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
{
// if user lock the screen
if ([[UIApplication sharedApplication] applicationState] == 1)
{
// coming back after unlocking screen..
}
}];
I have gone through many sources and found there isn't any solution for this in iOS7. Therefor I post the individual question over here, let me know if any one knows how to detect lock event on iOS7 enabled device.
Any help will be appreciated!

Related

Responding to MPMoviePlayerController notifications during background media playback

I have an app that streams video from the net and plays it using an MPMoviePlayerController object for playback on the device or via AirPlay.
The app supports background operation and has the 'audio' option listed within the required UIBackgroundModes key in its plist file.
When playing over AirPlay, the app can successfully be pushed to the background and the video continues to play properly. So far, so good.
According to the Apple documentation:
Including the audio key tells the system frameworks that they should
continue playing and make the necessary callbacks to the app at
appropriate intervals. If the app does not include this key, any audio
being played by the app stops when the app moves to the background.
However, these callbacks are not being made.
The app uses two types of callback: those associated with notifications MPMoviePlayerController and AVPlayer send during playback together with timer based callbacks that monitor the playback position and performance stats for monitoring purposes.
Looking at Apple's notes, I would certainly expect to receive the first type of callback so that the app can respond to MPMoviePlayerPlaybackStateDidChangeNotification, MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerLoadStateDidChangeNotification, but this doesn't happen.
Does anyone know if it is possible to receive these during background AirPlay playback and, if so, how was this achieved?
**Please note: the app works correctly when running in the foreground and receives the notifications fine. It is only when pushed to the background and playing over AirPlay that the notifications are not received.
Likewise, the video plays over AirPlay in the background properly. It is only the notifications which are not received**
I had this issue and have fixed it though it was a few months back. I could send you my entire class for movie playback if this doesn't work. Note it is using the navigation controller model.
NOTE: This is tested on iPad 2 not on the iPhone.
I show my VC like this:
- (IBAction)playMovie:(id)sender {
MovieVC* movController = [[MovieVC alloc] initWithID:2];
movController.view.backgroundColor = [UIColor blackColor];
AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
[appDel.navigationController pushViewController:movController animated:NO];
[movController release];
}
Then in my MovieVC view controller class i set up the video playback like this:
- (void)initMoviePlayer {
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]];
mMoviePlayer.allowsAirPlay = YES;
mMoviePlayer.view.frame = [self.view bounds];
mMoviePlayer.view.backgroundColor = [UIColor clearColor];
mMoviePlayer.shouldAutoplay = YES;
mMoviePlayer.fullscreen = YES;
mMoviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:mMoviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
This fixed it for me, if it doesn't fix comment and ill edit with the entire class file.
In one of my projects I did what you did, and it worked for me.
Two differences in my project :
I am not streaming via airPlay (device playback only),
I am just playing audio files.
My step-by-step :
Add the audio option to UIBackgroundModes in the plist file,
Register to the NotificationCenter for MPMoviePlayerPlaybackDidFinishNotification and MPMoviePlayerPlaybackStateDidChangeNotification with the following code :
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(playbackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:moviePlayer];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(playbackEnded:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
It works like a charm.
For completeness, I ought to add that at present I believe there is no solution to this problem.
I've discussed it directly with Apple via tech support and there was no practical work-around available.
This functionality was needed to allow the app to record stats about the playback of the stream at regular intervals. While this is fine when the video is played on the device's screen and over AirPlay while the app is in the foreground, it isn't possible to do with the app in the background.
The solution I've gone with instead is to disable the idle timer during all types of playback and to re-enable afterwards using:
[UIApplication sharedApplication].idleTimerDisabled = YES;
and
[UIApplication sharedApplication].idleTimerDisabled = NO;
While this isn't a solution to the original question, it's a workaround for avoiding the issue in the first place.

How to detect if user did a screenshot with home+power in iOS?

I'm trying to detect if user take a screenshot in order to warn them this is a video with copyright. I've tried Darwin Notification, but it didn't work...
NSMetadataQuery might be a solution, but we want the app to run on iOS4 as well. Can somebody help me?
Thanks!:p
It is not possible to detect a screenshot has (or will) be taken on iOS 5.
In iOS 4.0, there was a Darwin notification fired PictureWasTakenNotification, but not anymore.
The documentation does not mention anything, and using NSNotificationCenter to subscribe to all notifications only shows silence after taking a screenshot.
before ios 4.0 you can register for PictureWasTakenNotification .This notification is sent when screenshot is taken.No notifications will be sent before the screenshot was taken.
But after that this has been discontinued and IMHO there is no other way round... :/
you can detect it with adding an observer to UIApplicationUserDidTakeScreenshotNotification .
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note) {
// executes after screenshot
}];

How to use iOS 5+ AirPlay for a second screen

I'm toying with AirPlay using an iPhone 4S and I'm having some trouble. I've updated my Apple TV 2 to the latest (4.4.3 I think? I forget, it's in the other room and I'm too lazy to check) and I have iOS 5.0 on my 4S while I use Xcode 4.2 Build 4D199. I wrote a simple UIView app that attempts to draw on a second screen when available. Here's what I have in my viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:[[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]];
NSLog(#"Registering for screen connect events...");
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(screenDidConnect:)
name:UIScreenDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(screenDidDisconnect:)
name:UIScreenDidDisconnectNotification
object:nil]; NSLog(#"There are %i connected screens.", ScreenCount);
if (ScreenCount > 1) {
self.secondWindow = [self myScreenInit:[[UIScreen screens] objectAtIndex:1]];
[self.secondWindow addSubview:[self createFullscreenViewForText:#"Second window" withSize:CGSizeMake(640, 480)]];
}
}
This code works in the simulator when I enable TV Out although it always crashes if I enable TV Out while in debug. I have to relaunch the app after enabling TV out in the hardware menu in the simulator. On relaunch I see the second screen UILabel on the second screen. On the iPhone 4S I can only make this work if I enable AirPlay using the AirPlay control in the running tasks bar all the way to the left. You'll note that I have added an MPVolumeView component to the view on launch which I attempt to use to connect to my Apple TV to no avail.
If I use the in app MPVolumeView I can successfully disconnect from the Apple TV but when I use it again and select the apple TV to reconnect I do not get the UIScreenDidConnectNotification. Again the only time I can see the 2nd screen is when I enable mirroring. If I connect with either AirPlay control I only count 1 screen and never get the notification. Also, each time I enable mirroring I see this in the console logs:
Could not find mapped image ColorOnGrayShadow_AirPlay.png
This seems to be broken and I would like to know what others experience has been in this area. I'll continue to toy around and update this post as I learn new things.
MPVolumeView's route menu can only control an audio stream going to the AppleTV.
Here's a working example of what you are trying to do:
https://github.com/quellish/AirplayDemo/
The "ColorOnGrayShadow_AirPlay.png" log message is normal for iOS 5. Using an Airplay connected AppleTV as an external display unfortunately requires mirroring, and you can't activate that from inside your app. The user has to follow these instructions:
http://support.apple.com/kb/TS4085

Prevent iPhone from locking when connected to charger and app is running

I'm writing an iPhone app. When the app is running and the iPhone is charging, there is no need to lock the iPhone. Is it possible to prevent the locking of the iPhone when the device is charging and my app is running?
You can subscribe to UIDeviceBatteryStateDidChangeNotification notification to get the moment when your iphone begins/stops to charge. Then in case iphone is charging you can set idleTimerDisabled property in UIApplication object to YES to prevent device to go to sleep:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateBatteryState:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
- (void) updateBatteryState:(NSNotification*)notification{
[UIApplication sharedApplication].idleTimerDisabled =
([UIDevice currentDevice].batteryState == UIDeviceBatteryStateCharging);
}
P.S. If user decides to put device to sleep with sleep/wake button there's no way to prevent him of doing so
This is not possible with current SDK.
EDIT: hmm, haven't got the question correctly from the first read - look on other replies for correct answer; my guess was, you were asking about if it possible to prevent appearance of the sync/charge screen when connecting device via usb or to the wall outlet

UIDeviceOrientationDidChangeNotification doesn't work while device is on table?

Hi there i have a problem,
In my AppDelegate i have used method beginGeneratingDeviceOrientationNotifications to start notify me when device starts rotating.
It works fine if i hand-held my ipad but when it is kept on table it doesn't work as expected.
it fires UIDeviceOrientationUnknown notification.
Also this notification gets started after UI launches not on splash screen.
following is my code:
if([[[PulseUIFactory Factory] GetUICreator] IsIPad])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
current device stars giving proper values.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
then some where i launches my UI as
[self Launch_UI];
but notification starts responding after [self Launch_UI]; call even if notification is registered before its call...
Please any help is appreciable!!!
When you place your device on a table, [[UIDevice currentDevice] orientation] will return UIDeviceOrientationFaceUp. Then if your device remains sitting on the table face up, it doesn't matter how you rotate it on the table, the current device orientation will still be UIDeviceOrientationFaceUp.
If the device has issues determining the orientation, you may get UIDeviceOrientationUnknown. See this tutorial on how to handle device rotation using UIDeviceOrientationDidChangeNotification.
Regarding your notification only firing after the UI is loaded, the UIDeviceOrientationDidChangeNotification will only fire when the device is rotated. So if you are not rotating your device until after the UI loads, you wont get a notification. If this is not the cause of the issue, I'd have to see more of your code to have a better idea of what is going on.