1

I have been banging my head against the wall with this issue, but since I updated to iOS 10.1, any view added to UIApplication.shared.keyWindow does not seem to render. It is simply invisible.

I happen to have a test phone with iOS 9.3.2 running in it and the added views DO appear visible.

NB: I have been using this for a long time to show "in app notifications" and it has been working like a charm.

It is a VERY simple piece of code and does not throw any errors whatsoever.

let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))

view.backgroundColor = UIColor.red

let window = UIApplication.shared.keyWindow

window?.addSubview(view)

I have checked the following:

the window is not nil and has a perfectly valid frame. Same goes for the UIView I add, and it does appear in the view hierarchy. I have also tried moving the code around, putting it in ViewDidAppear, ViewDidLayoutSubViews etc… but to no avail.

Any ideas? Anybody else facing the same issue?

EDIT: Seeing some of the comments below, I believe I need to clarify: this piece of code should work (if I take reference on Apple documentation). Also, the issue is iOS 10 specific. In versions < iOS 10 it works perfectly.

So I am not looking for where this called should be called but rather for what may have changed in iOS 10 that would bring this behaviour.

Thanks

Benjamin
  • 8,128
  • 3
  • 34
  • 45

1 Answers1

0

Totally by chance I found the solution, and it is very strange. In our project, we are using HOKO Links for deeplinking and we encountered a bug with iOS 10: HOKO was "stealing" all events in func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool (Facebook, Google Sign in…).

We filed a bug to the guys from HOKO and managed to track down the culprit by scanning their code for "version greater than iOS9" and came accross the following method:

- (void)requestDeferredDeeplink:(void (^)(NSString *))handler {

    BOOL isFirstRun = ![[HOKUtils     objectForKey:HOKDeferredDeeplinkingNotFirstRun] boolValue];

    if (isFirstRun) {
        self.handler = handler;

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
    if (HOKSystemVersionGreaterThanOrEqualTo(@"9.0")) {

        NSString *fingerprintURL = [NSString stringWithFormat:@"%@?uid=%@", [HOKNetworkOperation urlFromPath:HOKFingerprintMatchingPath], [HOKDevice device].uid];
        self.safariViewController = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:fingerprintURL]];
        self.safariViewController.delegate = self;

        UIViewController *rootViewController = [[UIViewController alloc] init];

        UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectZero];
        window.rootViewController = rootViewController;
        [window makeKeyAndVisible];
        window.alpha = 0;

        [rootViewController presentViewController:self.safariViewController animated:NO completion:nil];
    } else {
        [self requestDeferredDeeplink];
    }
#else
    [self requestDeferredDeeplink];
#endif
  }
}

The solution was to remove all the code between #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000 and #endif and replace with [self requestDeferredDeeplink];

We also noticed that HOKO was setting the window alpha to 0… And THAT is what was preventing UIViews added to the keyWindow being visible. However, strangely enough, it wasn't effecting the rest of the App.

I doubt anyone else is likely to encounter this issue, but I am leaving this here for posterity.

Benjamin
  • 8,128
  • 3
  • 34
  • 45