2

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.).

7
  • explain you question more. Secondly you can iterate through app delegate.window.rootController.view's subviews. The subview at 0 index will be the top one. Sep 25, 2014 at 8:00
  • I'm trying to get the top most view in current device, that means, not application top most view, the device itself top most view.
    – Idan Moshe
    Sep 25, 2014 at 8:53
  • In my opinion this question isn't very clear. Also to help make it clear it's always good to share some code of the issue you are having.
    – Popeye
    Oct 13, 2014 at 12:27
  • 2
    That's not how this works. You need to make some sort of attempt at doing it yourself then share the specific piece of code you are having trouble with. We will not do your work for you.
    – Popeye
    Oct 13, 2014 at 12:44
  • 1
    With the way applications are sandboxed, I'm not sure you can do this without injecting your code into the springboard process on a jailbroken device. Oct 18, 2014 at 7:25

4 Answers 4

7
+200

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.

  1. Inject your code into third-party applications
  2. Inject your code into SpringBoard
  3. 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.

Example 2 screenshots

0
0

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];
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.