Search code examples
objective-cscreenfullscreenunlock

Unlock second screen Objective c


i have an app which lock screen and now i try it with multiple screens. I don't know to unlock the second screen. here is how i unlock the second screen :

if([[NSScreen screens] count] > 1){
    // Draw a new window to fill the screen
    NSScreen *screen;
    NSRect screenRect = CGRectMake(0, 0, screen.frame.size.width , screen.frame.size.height);

    NSWindow *secondaryMonitorWindow = [[NSWindow alloc] initWithContentRect:screenRect styleMask:NSBorderlessWindowMask  backing:NSBackingStoreBuffered  defer:NO  screen:screen];


    [secondaryMonitorWindow.contentView exitFullScreenModeWithOptions:nil];


}

i successfully unlock the first screen but not the second, if someone can help me...


Solution

  • if someone needs i fixed it with the following code :

    .m

    [windowArray insertObject:self.window atIndex:0];
    
            //if we have many screens
            NSRect screenRect;
            NSArray *screenArray = [NSScreen screens];
            for (NSInteger index = 1; index < [screenArray count]; index++)
    
            {
    
                NSScreen *screen = [screenArray objectAtIndex: index];
    
                screenRect = CGRectMake(0, 0, screen.frame.size.width , screen.frame.size.height);
                NSWindow *window = [[NSWindow alloc] initWithContentRect:screenRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO screen:screen];
    
                [window.contentView setWantsLayer:YES];
                window.contentView.layer.backgroundColor = [NSColor blackColor].CGColor;
                [window.contentView enterFullScreenMode:[[NSScreen screens] objectAtIndex:index] withOptions:nil];
    
    
                [windowArray addObject:window];
    
            }
    

    Don't forget to add in .h

    NSMutableArray *windowArray;
    

    And to exitFullScreen :

    for(NSInteger index = 1; index < [windowArray count]; index ++){
    
                if([[windowArray objectAtIndex:index]contentView].inFullScreenMode){
    
                    [[[windowArray objectAtIndex:index]contentView] exitFullScreenModeWithOptions:nil];
    
                } 
            }