I'm using React Native to create a mobile application.When I click on a button in my app, I need to programmatically take a screenshot of the current page of my application together with the iPhone status bar that shows the time, cellular provider, and battery level. However, my app page is being captured without having the statusbar.
My 'screenshot taken' function is written in Objective-C.
Is this happening because of any privacy-related concerns?
Would you kindly assist me with this?
Attaching the screenshot code,
#import <UIKit/UIKit.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
@interface ScreenshotModule : NSObject
@end
@implementation ScreenshotModule
RCT_EXPORT_MODULE();
RCT_REMAP_METHOD(takeStatusBarScreenshot, resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
dispatch_async(dispatch_get_main_queue(), ^{
@try {
// Get the status bar window
UIWindow *statusBarWindow = [UIApplication sharedApplication].windows.firstObject;
UIScene *scene = [UIApplication sharedApplication].connectedScenes.allObjects.firstObject;
if ([scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
BOOL statusBarHidden = windowScene.statusBarManager.isStatusBarHidden;
if (statusBarHidden) {
NSLog(@"Status bar is hidden, app is in full-screen mode.");
} else {
NSLog(@"Status bar is visible.");
}
} else {
NSLog(@"The scene is not a UIWindowScene.");
}
// Check if the statusBarWindow is valid
if (!statusBarWindow) {
reject(@"screenshot_failed", @"Status bar window not found", nil);
return;
}
// Get the window scene and status bar frame
UIWindowScene *windowScene = statusBarWindow.windowScene;
CGRect statusBarFrame = windowScene.statusBarManager.statusBarFrame;
// Log the status bar frame for debugging
RCTLogInfo(@"Status Bar Frame: %@", NSStringFromCGRect(statusBarFrame));
// Check if the status bar frame is valid
if (CGRectIsEmpty(statusBarFrame)) {
reject(@"screenshot_failed", @"Status bar frame is empty", nil);
return;
}
// Start capturing the status bar
UIGraphicsBeginImageContextWithOptions(statusBarFrame.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
// Render the status bar layer
[statusBarWindow.layer renderInContext:context];
// Create an image from the current context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (!image) {
reject(@"screenshot_failed", @"Failed to capture screenshot", nil);
return;
}
// Convert the image to PNG format and then to a base64 string
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData == nil) {
reject(@"screenshot_failed", @"Image data is nil", nil);
return;
}
NSString *base64String = [imageData base64EncodedStringWithOptions:0];
// Log base64 string length for debugging
RCTLogInfo(@"Base64 Image Length: %lu", (unsigned long)[base64String length]);
// Optionally, save the image to a file (for debugging purposes)
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"statusbar_screenshot.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
RCTLogInfo(@"Status bar screenshot saved to: %@", path);
// Resolve with the base64 image
resolve(base64String);
}
@catch (NSException *exception) {
reject(@"screenshot_error", @"Error while capturing status bar screenshot", nil);
}
});
}
@end