How to access keyWindow in share extension in ios?

I need to access keyWindow in a share extension for my application so that - adding an indicator indicator
- Accessing the width and height of the window

I wrote the following line in the share extension classes:

UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];

      

But he says "sharedApplication" is not available.
How can I access the keyWindow in the share extension?

You need your valuable suggestions. thanks in advance

+3


source to share


3 answers


Shared app is not available for extensions. You cannot access the key window from the sharing extension. You can subclass the ShareExtension View controller from UINavigationController and you can present the modal view controller and then you can also navigate. An activity indicator can be added in this modular controller.



0


source


UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
UIWindow *keyWindow = application.keyWindow;

      



however keyWindow is zero ...

0


source


UIApplication is not available for extensions. To add an indicator view, you will need to do it through your UIViewController stack, but you can get a screen size that might be sufficient depending on your specific use case (swift):

UIScreen.mainScreen().bounds.size

      

Read more here about the differences between [UIScreen mainScreen] .bounds vs [UIApplcation sharedApplication] .keyWindow.bounds?

Update

An alternative to using UIScreen to get the size is to navigate through the view hierarchy. This works anywhere in the UIView or UIViewController where the view is laid out. You can also add an indicator to the topmost view.

// Find the top most view
var view: UIView = self;
while let higherView = view.superview {
    view = higherView;
}

// Size you need
var size: CGSize = view.frame.size;

// Add the indicator
let indicator: UIView = UIView(frame: CGRectMake(100, 100, 100, 100));
indicator.backgroundColor = UIColor.orangeColor();
view.addSubview(indicator);
view.bringSubviewToFront(indicator);

      

-1


source







All Articles