Skip to content

Commit

Permalink
Add log message if App moves to background (#39943)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #39943

When the user attaches a debugger, and the app moves to the background the debugging session persists. This
sends a CDP console.info so the debugging user is aware of the app's state. It is an easy state to get into
when debugging on multiple emulators.

Changelog: [iOS][Added] - Add console.log notification in DevTools if app transitions between back/foreground.

Reviewed By: dmytrorykun

Differential Revision: D49956535

fbshipit-source-id: 29e1aba9c4eaeba072fe04f2b932a3e04c96d081
  • Loading branch information
blakef authored and facebook-github-bot committed Oct 19, 2023
1 parent 0a8639c commit 05967e4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef RCTBundleStatus * (^RCTBundleStatusProvider)(void);
@interface RCTInspectorRemoteConnection : NSObject
- (void)onMessage:(NSString *)message;
- (void)onDisconnect;
- (void)handleBackgroundEvent:(NSNotification *)notification;
@end

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,49 @@ - (instancetype)initWithPackagerConnection:(RCTInspectorPackagerConnection *)own
if (self = [super init]) {
_owningPackagerConnection = owningPackagerConnection;
_pageId = pageId;
[self addObserverFor:UIApplicationDidEnterBackgroundNotification];
[self addObserverFor:UIApplicationWillEnterForegroundNotification];
}
return self;
}

- (void)addObserverFor:(NSString *)notificationName
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleBackgroundEvent:)
name:notificationName
object:nil];
}

- (void)consoleInfo:(NSString *)message format:(NSString *)format
{
if (!message) {
return;
}
NSNumber *now = @([[NSDate date] timeIntervalSince1970] * 1000);
NSDictionary *json = @{
@"method" : @"Runtime.consoleAPICalled",
@"params" : @{@"type" : @"info", @"args" : format == nil ? @[ message ] : @[ message, format ], @"timestamp" : now}
};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
if (error != nil) {
NSLog(@"Unable to serialize a console.warn() message: %@", error);
return;
}
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[self onMessage:str];
}

- (void)handleBackgroundEvent:(NSNotification *)notification
{
if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
[self consoleInfo:@"App has moved into the %cforeground" format:@"font-weight: bold"];
} else if ([notification.name isEqualToString:UIApplicationDidEnterBackgroundNotification]) {
[self consoleInfo:@"App has moved into the %cbackground" format:@"font-weight: bold"];
}
}

- (void)onMessage:(NSString *)message
{
[_owningPackagerConnection sendWrappedEvent:_pageId message:message];
Expand Down

0 comments on commit 05967e4

Please sign in to comment.