When I show an alert with UIAlertController, the alert itself presented in a new window. (for now at least) And when the alert window dismisses, system seems to set a random window key-window.
I am presenting a new "banner" window to render some banners over status-bar (AppStore compatibility is out of topic here), and usually, this "banner" window becomes next key window, and causes many problems on user input and first responder management.
So, I want to prevent this "banner" window to become a key window, but I cannot figure out how. For now, as a workaround, I am just re-setting my main window to be a key window again as soon as that "banner" window becomes key window. But it doesn't feel really good.
How can I prevent a window to become a key window?
As a workaround, we can set main window key again as soon as the "banner" window becomes a key like this.
class BannerWindow: UIWindow {
weak var mainWindow: UIWindow?
override func becomeKeyWindow() {
super.becomeKeyWindow()
mainWindow?.makeKeyWindow()
}
}
Faced with this too. It seems that it's enough to just make:
class BannerWindow: UIWindow {
override func makeKey() {
// Do nothing
}
}
This way you don't need to keep a reference to a previous keyWindow, which is especially cool if it might get changed.
For Objective-C it's:
#implementation BannerWindow
- (void)makeKeyWindow {
// Do nothing
}
#end
I've been trying to solve this problem for years. I finally reported a Radar for it: http://www.openradar.me/30064691
My workaround looks something like this:
// a UIWindow subclass that I use for my overlay windows
#implementation GFStatusLevelWindow
...
#pragma mark - Never become key
// http://www.openradar.me/30064691
// these don't actually help
- (BOOL)canBecomeFirstResponder
{
return NO;
}
- (BOOL)becomeFirstResponder
{
return NO;
}
- (void)becomeKeyWindow
{
LookbackStatusWindowBecameKey(self, #"become key window");
[[self class] findAndSetSuitableKeyWindow];
}
- (void)makeKeyWindow
{
LookbackStatusWindowBecameKey(self, #"make key window");
}
- (void)makeKeyAndVisible
{
LookbackStatusWindowBecameKey(self, #"make key and visible window");
}
#pragma mark - Private API overrides for status bar appearance
// http://www.openradar.me/15573442
- (BOOL)_canAffectStatusBarAppearance
{
return NO;
}
#pragma mark - Finding better key windows
static BOOL IsAllowedKeyWindow(UIWindow *window)
{
NSString *className = [[window class] description];
if([className isEqual:#"_GFRecordingIndicatorWindow"])
return NO;
if([className isEqual:#"UIRemoteKeyboardWindow"])
return NO;
if([window isKindOfClass:[GFStatusLevelWindow class]])
return NO;
return YES;
}
void LookbackStatusWindowBecameKey(GFStatusLevelWindow *self, NSString *where)
{
GFLog(GFError, #"This window should never be key window!! %# when in %#", self, where);
GFLog(GFError, #"To developer of %#: This is likely a bug in UIKit. If you can get a stack trace at this point (by setting a breakpoint at LookbackStatusWindowBecameKey) and sending that stack trace to nevyn#lookback.io or support#lookback.io, I will report it to Apple, and there will be rainbows, unicorns and a happier world for all :) thanks!", [[NSBundle mainBundle] gf_displayName]);
}
+ (UIWindow*)suitableWindowToMakeKeyExcluding:(UIWindow*)notThis
{
NSArray *windows = [UIApplication sharedApplication].windows;
NSInteger index = windows.count-1;
UIWindow *nextWindow = [windows objectAtIndex:index];
while((!IsAllowedKeyWindow(nextWindow) || nextWindow == notThis) && index >= 0) {
nextWindow = windows[--index];
}
return nextWindow;
}
+ (UIWindow*)findAndSetSuitableKeyWindow
{
UIWindow *nextWindow = [[self class] suitableWindowToMakeKeyExcluding:nil];
GFLog(GFError, #"Choosing this as key window instead: %#", nextWindow);
[nextWindow makeKeyWindow];
return nextWindow;
}
Related
I have a Mac Catalyst app that's essentially a one-window app, but I added multi-window support (using scenes) to allow opening a second window for one function, which a small portion of users will use. Now Apple has rejected the app because with multi-window support, the app doesn't quit when a user clicks the red button at the top of the main window. One solution is to provide a menu item to reopen it, but I think it would be more intuitive for users if the app simply quit as it did before.
I found a similar problem on the Apple forums and am trying to implement the provided solution. Using this tutorial that provides more setup instructions, I have added a macOS bundle as a new target, embedded that into the iOS target, and added this class to the bundle:
#import "AppKitBridge.h"
#implementation AppKitBridge
#synthesize application;
#synthesize window;
- (id)init {
NSLog(#"AppKitBridge init");
self = [super init];
self.application = [NSApplication sharedApplication];
self.window = [[self.application windows] firstObject];
if (self.window) {
self.application.delegate = self;
self.window.delegate = self;
} else {
NSLog(#"AppKitBridge error: window is nil");
}
return self;
}
- (void)test {
NSArray *windows = NSApplication.sharedApplication.windows;
for (NSWindow *window in windows) {
NSLog(#"AppKitBridge window: %#", window);
}
}
- (void)applicationDidUpdate:(NSNotification *)notification {
NSLog(#"AppKitBridge applicationDidUpdate");
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
NSLog(#"AppKitBridge applicationShouldTerminateAfterLastWindowClosed");
return TRUE;
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
NSLog(#"AppKitBridge applicationShouldTerminate");
return TRUE;
}
#end
Then in viewDidLoad of the initial view controller of the iOS app, I call this method to load the bundle:
- (void)enableAppKit {
NSString *pluginPath = [[[NSBundle mainBundle] builtInPlugInsPath] stringByAppendingPathComponent:#"AppKit.bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:pluginPath];
[bundle load];
NSObject *appKit = [[[bundle classNamed:#"AppKitBridge"] alloc] init];
[appKit performSelector:#selector(test) withObject:nil afterDelay:0];
}
When I run the app, the console shows the AppKitBridge init, AppKitBridge window and AppKitBridge applicationDidUpdate lines. So it seems like the overall setup is working. But when I click the red window button, it does not show the AppKitBridge applicationShouldTerminateAfterLastWindowClosed or AppKitBridge applicationShouldTerminate lines, and the app does not quit.
Should this do what I'm expecting, and if so, what am I missing in the setup?
The problem is this line:
NSObject *appKit = [[[bundle classNamed:#"AppKitBridge"] alloc] init];
Your appKit object is a local variable so your AppKitBridge instance goes out of existence one line later. You need this object to persist if it is to function as the app/window delegate. Assign it to an instance property of some persistent object.
I've looked through a ton of other StackOverflow posts about this error and all of them provide very reasonable solutions the problem. In other words, they generally pinpoint something in the code that isn't being auto-retained, but should be and then it subsequently causes a crash.
The problem I'm having is that the line of code that Crashlytics is telling me doesn't seem to have anything that could possibly be dealloc'd.. at least that I know of. Hopefully, you'll be able to see something I'm not seeing.
I'm not able to replicate the crash myself, but Crashlytics tells me I've had 146 of these crashes across 28 different users in the last 3 months.
My MainMenuDrawerViewController is a UITableViewController that sits in my left-side drawer (using MMDrawerController).
The crash happens in -tableView:didSelectRowAtIndexPath: on the following line:
[self updateCenterWithViewControllerIdentifiedBy:#"ReportsViewController"];
The only two objects on that line are self and a string literal, so I don't understand what could possibly be dealloc'd and causing the EXC_BAD_ACCESS.
Here is the overall method (with irrelevant code cut out):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
// removed other case statements
case DrawerRowReports: {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self performSegueWithIdentifier:#"ShowReportList" sender:self];
} else {
[self updateCenterWithViewControllerIdentifiedBy:#"ReportsViewController"];
}
break;
}
// removed other case statements
default:
break;
}
}
The -updateCenterWithViewControllerIdentifiedBy: function instantiates a View Controller from the storyboard using the given identifier, then instantiates an MMNavigationController with the first view controller as the root, then updates the mm_drawerController to put that MMNavigationController into the center position.
I'll include that method as well below, BUT the Crashlytics report doesn't say the bad access happens inside the method, it says it happens at the line above.
- (nullable id) updateCenterWithViewControllerIdentifiedBy:(nullable NSString*)storyboardIdentifier {
return [self updateCenterWithViewControllerIdentifiedBy:storyboardIdentifier withCloseAnimation:YES];
}
- (nullable id) updateCenterWithViewControllerIdentifiedBy:(nullable NSString*)storyboardIdentifier withCloseAnimation:(BOOL)withCloseAnimation {
return [self updatePosition:DrawerCenter withViewControllerIdentifiedBy:storyboardIdentifier withValueDictionary:nil withCloseAnimation:withCloseAnimation];
}
- (nullable id) updatePosition:(DrawerPosition)position withViewControllerIdentifiedBy:(nullable NSString*)storyboardIdentifier withValueDictionary:(nullable NSDictionary*)configDictionary withCloseAnimation:(BOOL)withCloseAnimation {
//BaseViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:storyboardIdentifier];
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:storyboardIdentifier];
if (configDictionary != nil) {
for (NSString *fieldname in [configDictionary allKeys]) {
[viewController setValue:[configDictionary objectForKey:fieldname] forKey:fieldname];
}
}
UINavigationController * nav = [[MMNavigationController alloc] initWithRootViewController:viewController];
if (position == DrawerCenter) {
[self.mm_drawerController setCenterViewController:nav
withCloseAnimation:withCloseAnimation
completion:nil];
} else if (position == DrawerRight) {
[self.mm_drawerController setRightDrawerViewController:nav];
} else if (position == DrawerLeft) {
[self.mm_drawerController setLeftDrawerViewController:nav];
} else {
NSLog(#"unknown drawer position: %ld", (long)position);
}
return viewController;
}
I'm trying to add a button to the video player that gets added to the view when a video becomes fullscreen from inside of an embedded web browser.
I saw this: Detect when a webview video becomes fullscreen on ios8. However, I think at that point we won't have a pointer to the video player. Perhaps there's a way to loop through all the subviews of the window's view and grab whatever is an instance of AVPlayer?
Ideally I could do something like this:
NSNotificationCenter.defaultCenter().addObserverForName(
UIWindowDidResignKeyNotification,
object: self.view.window,
queue: nil
) { notification in
let window = whatever the window is now
let player = window.methodThatReturnsVideoPlayer
// do stuff with player
let button = UIButton(...)
window.view.addSubView(button)
}
There is no public API method to get a pointer to the video player in a UIWebView.
There is, however, an unsafe way to access the video player. Using reflection, you can iterate through the subviews of a UIWebView and attempt to find the player:
- (UIView *)getViewInsideView:(UIView *)view withPrefix:(NSString *)classNamePrefix {
UIView *webBrowserView = nil;
for (UIView *subview in view.subviews) {
if ([NSStringFromClass([subview class]) hasPrefix:classNamePrefix]) {
return subview;
} else {
if((webBrowserView = [self getViewInsideView:subview withPrefix:classNamePrefix])) {
break;
}
}
}
return webBrowserView;
}
- (UIViewController *)movieControllerFromWebView:(UIWebView *)webview {
UIViewController *movieController = nil;
#try {
UIView *movieView = [self getViewInsideView:webview withPrefix:[NSString stringWithFormat:#"MP%#oView", #"Vide"]];
SEL mpavControllerSel = NSSelectorFromString([NSString stringWithFormat:#"mp%#roller", #"avCont"]);
if ([movieView respondsToSelector:mpavControllerSel]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
movieController = (UIViewController *)[movieView performSelector:mpavControllerSel];
#pragma clang diagnostic pop
}
}
#catch (NSException *exception) {
NSLog(#"Failed to get movieController: %#", exception);
}
return movieController;
}
This code has been adopted from YouTubeHacks.m.
Your milage may vary, as Apple tends to change the name of their player objects with the different SDKs (MPMoviePlayerController, UIMovieView, MPVideoView, UIMoviePlayerController, etc.). I would look at the MediaPlayer.framework private headers for reference.
I've written a category on UIView that allows me to walk the view hierarchy:
UIView+Capture.h
typedef void(^MSViewInspectionBlock)(UIView *view, BOOL *stop);
#interface UIView (Capture)
- (void)inspectViewHeirarchy:(MSViewInspectionBlock)block;
#end
UIView+Capture.m
#implementation UIView (Capture)
- (void)inspectViewHeirarchy:(MSViewInspectionBlock)block
{
BOOL stop = NO;
[self inspectViewHeirarchy:block stop:stop];
}
#pragma - Private
- (void)inspectViewHeirarchy:(MSViewInspectionBlock)block stop:(BOOL)stop
{
if (!block || stop) {
return;
}
block(self, &stop);
for (UIView *view in self.subviews) {
[view inspectViewHeirarchy:block stop:stop];
if (stop) {
break;
}
}
}
#end
Which you can use like so:
[[[UIApplication sharedApplication] keyWindow] inspectViewHeirarchy:^(UIView *view, BOOL *stop) {
if ([view isMemberOfClass:[UIScrollView class]]) {
NSLog(#"Found scroll view!");
*stop = YES;
}
}];
Everything works fine, except setting stop to YES. This appears to have absolutely no effect whatsoever. Ideally, I'd like this to halt the recursion, so when I've found the view I want to take some action on I don't have to continue to traverse the rest of the view hierarchy.
I'm pretty dense when it comes to using blocks, so it may be something completely obvious. Any help will be greatly appreciated.
The way you're using a block is exactly the same as using a C function. So there's nothing special you really need to know about blocks. Your code should work but note the difference between passing stop as a BOOL * to your block and to create a new local when you recurse.
It looks like you're expecting calls down to inspectViewHierarchy:stop: to affect the outer stop variable. That won't happen unless you pass it as a reference. So I think what you want is:
- (void)inspectViewHeirarchy:(MSViewInspectionBlock)block stop:(BOOL *)stop
...and appropriate other changes.
I assume you want to return all the way out from the top-level inspectViewHierarchy when the user sets stop to YES.
(Incidentally, you spelled “hierarchy” wrong and you should use a prefix on methods you add to standard classes.)
#implementation UIView (Capture)
- (void)micpringle_visitSubviewsRecursivelyWithBlock:(MSViewInspectionBlock)block
{
BOOL stop = NO;
[self inspectViewHierarchy:block stop:&stop];
}
#pragma - Private
- (void)micpringle_visitSubviewsRecursivelyWithBlock:(MSViewInspectionBlock)block stop:(BOOL *)stop
{
block(self, stop);
if (*stop)
return;
for (UIView *view in self.subviews) {
[view micpringle_visitSubviewsRecursivelyWithBlock:block stop:stop];
if (*stop)
break;
}
}
#end
- (BOOL) inspectViewHeirarchy:(MSViewInspectionBlock)block
{
BOOL stop = NO;
block(self, &stop);
if (stop)
return YES;
for (UIView *view in self.subviews) {
if ([view inspectViewHeirarchy:block])
return YES;
}
return NO;
}
Try this:
- (void)inspectViewHeirarchy:(MSViewInspectionBlock)block
{
__block BOOL stop = NO;
[self inspectViewHeirarchy:block stop:stop];
}
Blocks, by nature, copy the variables and context in which they are declared.
Even though you are passing the boolean as a reference, it's possible that it's using a copy of the context and not the true stop.
This is just a wild guess but, inside inspectViewHierarchy:stop: do something like:
- (void)inspectViewHeirarchy:(MSViewInspectionBlock)block stop:(BOOL)stop
{
if (!block || stop) {
return;
}
// Add these changes
__block BOOL blockStop = stop;
block(self, &blockStop);
for (UIView *view in self.subviews) {
[view inspectViewHeirarchy:block stop:stop];
if (stop) {
break;
}
}
}
This may be a long shot and I'm not 100% sure it will work without having your project, but it's worth a shot.
Also, refactor your method so "heirarchy" is actually spelled "hierarchy" :] It's good for reusability and for keeping a good code base ;)
wouldn't you want to check the status of 'stop' directly after you invoke the block? It doesn't help to invoke it after you call inspectViewHierarchy:stop: because you are passing a copy of 'stop' to that method instead of the reference.
I am new to Objective c Native coding in IOS. I am having two buttons. One to lock the app to portrait and another lock the landscape orientation. What should I do to achieve this. Locking in the sense it will fix to the locked side and will not turn.
Yes i have used a method to implement the same using ios plugin which i created in worklight(For hybrid apps).
The function for setting the flag is
#import "HelloworlPlugin.h"
#implementation HelloworlPlugin
int count=0;
-(void)sayhello:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)option
{
[arguments pop];
NSString *responseString=[NSString stringWithFormat:#"%#",[arguments objectAtIndex:0]];
if([responseString isEqualToString:#"1"])
{
count=1;
}
else
{
count=0;
}
NSLog(#"Zero %#",responseString);
}
-(int)count1
{
NSLog(#"count= %d",count);
if(count<1)
{
Cv=0;
}
else
{
Cv=1;
}
NSLog(#"%d",Cv);
return Cv;
}
#end
Where in say hello function i get the arguments from the hybrid app and based on that i set the flag variable Cv to 1 or 0.
Below is the default method which i have changed little bit to do my implementation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
HelloworlPlugin *object=[[HelloworlPlugin alloc] init];
int fValue=[object count1];
NSLog(#"%d",fValue);
if(fValue==1)
{
return (interfaceOrientation ==UIInterfaceOrientationPortrait);
}
else
{
return true;
}
}
shouldAutorotateToInterfaceOrientation
is a thing that is default function based on which the orientation is set returning true set all the orientation else by giving the approriate orientation we can set the answer.So here based on the cv value i stored in fValue i have locked the orientation.
For example this way:
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];