Finding TopMostWindow/FrontMostWindow in iOS 8 - ios

In iOS 7 and earlier, if we want to find topMostWindow in application, we were commonly using following line of code
[[[UIApplication sharedApplication] windows] lastObject]
This will commonly return UITextEffectsWindow if there is keyboard available on screen else it returns UIWindow.(This is common scenario, not always true)
But, In iOS 8, when i use above command it always gives me UITextEffectsWindow. Also, [[UIApplication sharedApplication] windows] always contains 2 objects(UIWindow and UITextEffectWindow). What should i do to get FrontMostWindow in case of iOS 8.

I think the (internal) UITextEffectsWindow is in fact the frontmost window. So I guess the question is not how to get the frontmost window, but what window you want to get.
Maybe you are looking for the key window? (From the docs: "The key window is the one that is designated to receive keyboard and other non-touch related events.")
So this would be
[[UIApplication sharedApplication] keyWindow]
I think this is the front most window from an end user's perspective (in all cases I have seen so far).

Related

Disable remote controls iOS

By default, remote control is activated on my streaming application but under a certain circumstance I want to disable all controllers and show nothing when the screen is locked.
First I tried to set now playing info to nil like this [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo: nil]; and all text, image and progress disappear but the control buttons and volume bar was still showing.
After that I tried this [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; but it gives me nothing, it shows the same thing on the screen as the previous method call.
For the record, I'm calling [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; on AppDelegate before I tried to call any of the previous methods.
So, I have not found anything related to this that could help me with my problem and I would appreciate any help :)

Getting top UIWIndow/UIView on device using public/non-public/private API

I'm trying to find out how to find the top most view using non-public/private api, is there any way doing it (iOS 7.1.2)?
Edit:
Maybe I wasn't clear,
I want to get the top most view no matter what app is active now (facebook, whatsapp, games, lockscreen, calendar, etc.).
Options
If I understand correctly, you're wanting to show a UIView on top of another application's window. In the app sandbox, you are only allowed access to your own application's window. Jailbreaking, however, will allow you to do this, but it's not exactly simple.
In order to show a view on top of applications, you have three choices. Which option you choose depends on what you're trying to do.
Inject your code into third-party applications
Inject your code into SpringBoard
Use a combination of methods 1 and 2.
Option 1
If you choose to go with this route, your code will be running inside each third-party application. This allows you to modify the view hierarchy of the application to show your own window or even modify existing ones. Since you're code is running in the application, you can obtain the key window by simply calling:
[[UIApplication sharedApplication] keyWindow]
Option 2
If you choose this option, your code will be running regardless of what application is open. By obtaining the main window, you can show UI over anything, including system alerts. I've posted an example of this to GitHub at rpendleton/tutorials/so-26032688. The example shows an alert anytime the user takes their phone out of silent. The code to retrieve SpringBoard's window is:
[[NSClassFromString(#"SBUIController") sharedInstance] window]
Option 3
The third option, and the one you'll likely end up using, is a combination of both options 1 and 2. In this scenario, you'll inject your code into third-party applications, and then communicate with SpringBoard to decide what needs to be done. Typically, your application would communicate with SpringBoard, then SpringBoard can relay that information to the desired application. You may be able to communicate directly between your application and a third party application, but I haven't tried that.
Communication
In order to communicate between your application and SpringBoard / other third-party applications, you'll need to use a messaging system. An easy way to do this communication is via a open source library called RocketBootstrap.
Screenshots
The screenshot on the left is from the example I posted on GitHub, and the screenshot on the right is from one of my tweaks AppScan. Although I'm presenting an alert in both, you can show whatever view you desire.
try getting window of AppDelegate like this
UIWindow *window = ((AppDelegate*)[UIApplication sharedApplication].delegate).window;
And then access window's last object like this
[[window subviews] objectAtIndex:0];
or like this
[[window subviews]lastObject];
If you want to addsubview to it
[[[window subviews] objectAtIndex:0] addSubview:view];
Reading the question and the comments this is what you're looking for...I think, not the most detailed question I saw:
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = self;
window.windowLevel = [[UIApplication sharedApplication] keyWindow].windowLevel + 1;
[window makeKeyAndVisible];
You want to use this as part of the view controller that you want on top.
You question is not totally clear, can you explain why you need to do that?
By the way most probably what you need is the key window first, as stated in the doc:
The key window is the one that is designated to receive keyboard and
other non-touch related events. Only one window at a time may be the
key window.
What you can do is ask for the application current key window:
NSArray * array = [[UIApplication sharedApplication] windows];
Iterate over that array to find the key window and then check for its subviews:
UIView * topView = [[window subviews]lastObject]
If you are looking to a way for emulate an alert view, You should create your own UIWindow and later ad your view controller that handles the "alert" as a rootViewController and make it" key and visible".
Something like that :
UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
alertWindow.rootViewController = [[AlertViewController alloc] initWithXibNamed:#"AlertXib"];
alertWindow.windowLevel = UIWindowLevelAlert;
[alertWindow makeKeyAndVisible];
And when you want to dismiss just set the rootViewController to nil and make the window hidden.
Using the root view controller approach helps into avoid messing with rotation corrections. Even if in iOS8 the rotates.
I've found a good reference here.

Directly launch phoneNumber dialog on iPad

If I use the UIDataDetectorTypePhoneNumber on a UITextView, and click a phone number on a device that has no phone (e.g. iPad), I get a Send Message / Add to Contacts / Copy popover. Is there some way to bring up that dialog directly in code or would I have to reimplement?
I did try [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:5555555"]]; but that does not bring up the dialog.
There are some subtle differences between how detected links and buttons behave and my client wants a clickable phone number that behaves like a button.
I did some method swizzling to see where this popover comes from, and it looks like it is generated before the openURL: method in UIApplication.
Digging further, it looks like the popover comes from some private objects in UITextView.
I think you'll have to reimplement.
Did you try it with some forward slashes?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://5555555"]];
Possibly related to this.
Use telprompt://5555555 instead of tel:5555555

How do I turn on iphone screen programmatically when it's dimmed [duplicate]

This question already has answers here:
Is there a way of adjusting the screen brightness programmatically?
(3 answers)
Closed 9 years ago.
Before iphone lock the screen, it will dim the screen first for a while. I wonder how to lighten the screen in my app at that time without touching the screen. Can anyone give a help?
To be more specific: I don't want to prevent the system sleep, nor adjust the screen brightness. What I want is to lighten the screen when I got a certain event to notice the user while the screen is dimmed.
Just solve this problem in a tricky way..
- (void)lightenTheScreen
{
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
That is default behavior of the iPhone. To prevent that nature you can use following code.
[[UIApplication sharedApplication] setIdleTimerDisabled: YES]; //Add this at the end in your applicationDidFinishLaunching application
From APPLE DOCUMENTATION
"The default value of this property is NO. When most applications have
no touches as user input for a short period, the system puts the
device into a "sleep” state where the screen dims. This is done for
the purposes of conserving power. However, applications that don't
have user input except for the accelerometer—games, for instance—can,
by setting this property to YES, disable the “idle timer” to avert
system sleep. "

iPhone display screen lights not turning off automatically

I have observed that my I run my iPhone application (native) the display lights are not turning off as they usually do in other applications, I have waited for couple of minutes but it wont turn off until I exit my application.
Is there anything which I had messed up with that is not causing the automatic dimming?
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
Are the two lines to enable/disable the dimming of the screen in an iOS app. Double check and see if your app has any of these.
Put this code into application didFinishLaunchingWithOptions: method.
Have you used this in your app?
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
If so, remove it or set it to NO.

Resources