Skip to content

Commit e7b9551

Browse files
chore: fix native iOS automatic capturing
1 parent 19c1c98 commit e7b9551

File tree

10 files changed

+168
-113
lines changed

10 files changed

+168
-113
lines changed

examples/default/ios/InstabugExample.xcodeproj/project.pbxproj

Lines changed: 74 additions & 74 deletions
Large diffs are not rendered by default.

examples/default/ios/Podfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ target 'InstabugExample' do
1818
# Flags change depending on the env values.
1919
flags = get_default_flags()
2020

21-
# pod 'Instabug', :podspec => 'https://ios-releases.instabug.com/custom/feature-support_cp_network_filtering_obfuscation-base/13.4.2/Instabug.podspec'
22-
pod 'Instabug', :podspec => 'https://ios-releases.instabug.com/custom/feature-support_cp_network_filtering_obfuscation-release/13.4.2/Instabug.podspec'
21+
pod 'Instabug', :podspec => 'https://ios-releases.instabug.com/custom/release-support_cp_network_filtering_obfuscation-add_reset_network_state/13.4.2/Instabug.podspec'
2322
use_react_native!(
2423
:path => config[:reactNativePath],
2524
# Hermes is now enabled by default. Disable by setting this flag to false.

examples/default/ios/Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ DEPENDENCIES:
524524
- FBReactNativeSpec (from `../node_modules/react-native/React/FBReactNativeSpec`)
525525
- glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`)
526526
- hermes-engine (from `../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`)
527-
- Instabug (from `https://ios-releases.instabug.com/custom/feature-support_cp_network_filtering_obfuscation-release/13.4.2/Instabug.podspec`)
527+
- Instabug (from `https://ios-releases.instabug.com/custom/release-support_cp_network_filtering_obfuscation-add_reset_network_state/13.4.2/Instabug.podspec`)
528528
- instabug-reactnative-ndk (from `../node_modules/instabug-reactnative-ndk`)
529529
- libevent (~> 2.1.12)
530530
- OCMock
@@ -600,7 +600,7 @@ EXTERNAL SOURCES:
600600
:podspec: "../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec"
601601
:tag: hermes-2023-03-20-RNv0.72.0-49794cfc7c81fb8f69fd60c3bbf85a7480cc5a77
602602
Instabug:
603-
:podspec: https://ios-releases.instabug.com/custom/feature-support_cp_network_filtering_obfuscation-release/13.4.2/Instabug.podspec
603+
:podspec: https://ios-releases.instabug.com/custom/release-support_cp_network_filtering_obfuscation-add_reset_network_state/13.4.2/Instabug.podspec
604604
instabug-reactnative-ndk:
605605
:path: "../node_modules/instabug-reactnative-ndk"
606606
RCT-Folly:
@@ -706,7 +706,7 @@ SPEC CHECKSUMS:
706706
Google-Maps-iOS-Utils: f77eab4c4326d7e6a277f8e23a0232402731913a
707707
GoogleMaps: 032f676450ba0779bd8ce16840690915f84e57ac
708708
hermes-engine: 10fbd3f62405c41ea07e71973ea61e1878d07322
709-
Instabug: 2a314dc5c7a6d5d07f7cb20f21c98dab72ec2387
709+
Instabug: eee21d3c3aaf8f2576bfa840b170a96c3898a437
710710
instabug-reactnative-ndk: 960119a69380cf4cbe47ccd007c453f757927d17
711711
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
712712
OCMock: 589f2c84dacb1f5aaf6e4cec1f292551fe748e74
@@ -758,6 +758,6 @@ SPEC CHECKSUMS:
758758
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
759759
Yoga: 8796b55dba14d7004f980b54bcc9833ee45b28ce
760760

761-
PODFILE CHECKSUM: 406d29e19cb1ac67c149111d9ea23ee7bbf9db83
761+
PODFILE CHECKSUM: 627a2a90e282a9fc9378b8a2ab29d94557724393
762762

763763
COCOAPODS: 1.15.2

examples/default/src/screens/apm/NetworkScreen.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Screen } from '../../components/Screen';
55
import { ClipboardTextInput } from '../../components/ClipboardTextInput';
66
import { useQuery } from 'react-query';
77
import { HStack, VStack } from 'native-base';
8-
import { gql, request } from 'graphql-request';
8+
import { gql, GraphQLClient } from 'graphql-request';
99
import { CustomButton } from '../../components/CustomButton';
1010
import axios from 'axios';
1111
import type { HomeStackParamList } from '../../navigation/HomeStack';
@@ -101,6 +101,12 @@ export const NetworkScreen: React.FC<
101101
}
102102

103103
const fetchGraphQlData = async () => {
104+
const client = new GraphQLClient('https://countries.trevorblades.com/graphql', {
105+
headers: {
106+
'ibg-graphql-header': 'AndrewQL', // change Query Name here
107+
},
108+
});
109+
104110
const document = gql`
105111
query {
106112
country(code: "EG") {
@@ -110,10 +116,7 @@ export const NetworkScreen: React.FC<
110116
}
111117
`;
112118

113-
return request<{ country: { emoji: string; name: string } }>(
114-
'https://countries.trevorblades.com/graphql',
115-
document,
116-
);
119+
return client.request<{ country: { emoji: string; name: string } }>(document);
117120
};
118121

119122
const { data, isError, isSuccess, isLoading, refetch } = useQuery('helloQuery', fetchGraphQlData);
@@ -127,7 +130,6 @@ export const NetworkScreen: React.FC<
127130
}
128131

129132
async function makeSequentialApiCalls(urls: string[]): Promise<any[]> {
130-
// const fetchPromises = urls.map((url) => fetch(url).then((response) => response.json()));
131133
const results: any[] = [];
132134

133135
try {
@@ -182,7 +184,7 @@ export const NetworkScreen: React.FC<
182184
title="Send Sequantail Requests"
183185
/>
184186

185-
<CustomButton onPress={() => refetch} title="Reload GraphQL" />
187+
<CustomButton onPress={() => refetch()} title="Reload GraphQL" />
186188
<View>
187189
{isLoading && <Text>Loading...</Text>}
188190
{isSuccess && <Text>GraphQL Data: {data.country.emoji}</Text>}

ios/RNInstabug/InstabugNetworkLoggerBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
*/
1212

1313
- (void)isNativeInterceptionEnabled:(RCTPromiseResolveBlock)resolve :(RCTPromiseRejectBlock)reject;
14+
- (void)resetNetworkLogToDefaultStateIOS;
1415
@end

ios/RNInstabug/InstabugNetworkLoggerBridge.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ -(void)stopObserving {
4444
resolve(@(IBGNetworkLogger.isNativeNetworkInterceptionFeatureEnabled));
4545
}
4646

47+
RCT_EXPORT_METHOD(resetNetworkLogToDefaultStateIOS) {
48+
[IBGNetworkLogger resetNetworkLogToDefaultState];
49+
}
50+
4751
@end

ios/RNInstabug/Util/IBGNetworkLogger+CP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
3030
timestamp:(NSNumber * _Nullable)timestamp
3131
generatedW3CTraceparent:(NSString * _Nullable)generatedW3CTraceparent
3232
caughtedW3CTraceparent:(NSString * _Nullable)caughtedW3CTraceparent;
33+
+ (void)resetNetworkLogToDefaultState;
3334

3435
@end
3536

src/modules/Instabug.ts

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ function reportCurrentViewForAndroid(screenName: string | null) {
7373
}
7474

7575
function _logFlags() {
76-
console.log(
77-
`Andrew: init -> {
76+
if (Platform.OS === 'android') {
77+
console.log(
78+
`Andrew: APM Flags -> {
7879
isNativeInterceptionFeatureEnabled: ${isNativeInterceptionFeatureEnabled},
7980
hasAPMNetworkPlugin: ${hasAPMNetworkPlugin},
8081
shouldEnableNativeInterception: ${shouldEnableNativeInterception}
8182
}`,
82-
);
83+
);
84+
} else {
85+
console.log(
86+
`Andrew: APM Flags -> {
87+
isNativeInterceptionFeatureEnabled: ${isNativeInterceptionFeatureEnabled},
88+
shouldEnableNativeInterception: ${shouldEnableNativeInterception}
89+
}`,
90+
);
91+
}
8392
}
8493

8594
/**
@@ -101,19 +110,19 @@ export const init = async (config: InstabugConfig) => {
101110
// Add app state listener to handle background/foreground transitions
102111
addAppStateListener(async (nextAppState) => handleAppStateChange(nextAppState, config));
103112

113+
// Perform platform-specific checks and update interception mode
114+
handleNetworkInterceptionMode(config);
115+
116+
// Log the current APM network flags and initialize Instabug
117+
_logFlags();
118+
104119
//Set APM networking flags for the first time
105120
setApmNetworkFlagsIfChanged({
106121
isNativeInterceptionFeatureEnabled: isNativeInterceptionFeatureEnabled,
107122
hasAPMNetworkPlugin: hasAPMNetworkPlugin,
108123
shouldEnableNativeInterception: shouldEnableNativeInterception,
109124
});
110125

111-
// Perform platform-specific checks and update interception mode
112-
handleNetworkInterceptionMode(config);
113-
114-
// Log the current APM network flags and initialize Instabug
115-
_logFlags();
116-
117126
// call Instabug native init method
118127
initializeNativeInstabug(config);
119128

@@ -140,16 +149,20 @@ const handleAppStateChange = async (nextAppState: AppStateStatus, config: Instab
140149
// Checks if the app has come to the foreground
141150
if (['inactive', 'background'].includes(_currentAppState) && nextAppState === 'active') {
142151
// Update the APM network flags
143-
const updatedFlags = await fetchApmNetworkFlags();
144-
const isUpdated = setApmNetworkFlagsIfChanged(updatedFlags);
152+
const isUpdated = await fetchApmNetworkFlags();
145153

146154
if (isUpdated) {
155+
console.log('Andrew: App has come to the foreground!');
147156
console.log('Andrew: APM network flags updated.');
148157
handleNetworkInterceptionMode(config);
149-
initializeNativeInstabug(config);
158+
handleIOSNativeInterception(config);
159+
_logFlags();
160+
setApmNetworkFlagsIfChanged({
161+
isNativeInterceptionFeatureEnabled,
162+
hasAPMNetworkPlugin,
163+
shouldEnableNativeInterception,
164+
});
150165
}
151-
_logFlags();
152-
console.log('Andrew: App has come to the foreground!');
153166
}
154167

155168
_currentAppState = nextAppState;
@@ -160,16 +173,25 @@ const handleAppStateChange = async (nextAppState: AppStateStatus, config: Instab
160173
* Fetches the current APM network flags.
161174
*/
162175
const fetchApmNetworkFlags = async () => {
163-
isNativeInterceptionFeatureEnabled = await NativeNetworkLogger.isNativeInterceptionEnabled();
176+
let isUpdated = false;
177+
const newNativeInterceptionFeatureEnabled =
178+
await NativeNetworkLogger.isNativeInterceptionEnabled();
179+
if (isNativeInterceptionFeatureEnabled !== newNativeInterceptionFeatureEnabled) {
180+
isNativeInterceptionFeatureEnabled = newNativeInterceptionFeatureEnabled;
181+
isUpdated = true;
182+
}
164183
if (Platform.OS === 'android') {
165-
hasAPMNetworkPlugin = await NativeNetworkLogger.hasAPMNetworkPlugin();
184+
const newHasAPMNetworkPlugin = await NativeNetworkLogger.hasAPMNetworkPlugin();
185+
if (hasAPMNetworkPlugin !== newHasAPMNetworkPlugin) {
186+
hasAPMNetworkPlugin = newHasAPMNetworkPlugin;
187+
isUpdated = true;
188+
}
166189
}
167190

168-
return {
169-
isNativeInterceptionFeatureEnabled,
170-
hasAPMNetworkPlugin,
171-
shouldEnableNativeInterception,
172-
};
191+
console.log(
192+
`Andrew: fetchApmNetworkFlags {isNativeInterceptionFeatureEnabled: ${isNativeInterceptionFeatureEnabled}, hasAPMNetworkPlugin: ${hasAPMNetworkPlugin}}`,
193+
);
194+
return isUpdated;
173195
};
174196

175197
/**
@@ -194,7 +216,8 @@ const handleNetworkInterceptionMode = (config: InstabugConfig) => {
194216
};
195217

196218
/**
197-
* Handles the JS interception logic for Android.
219+
* Handles the network interception logic for Android if the user set
220+
* network interception mode with [NetworkInterceptionMode.javascript].
198221
*/
199222
function handleAndroidJSInterception() {
200223
if (isNativeInterceptionFeatureEnabled && hasAPMNetworkPlugin) {
@@ -206,7 +229,8 @@ function handleAndroidJSInterception() {
206229
}
207230

208231
/**
209-
* Handles the native interception logic for Android.
232+
* Handles the network interception logic for Android if the user set
233+
* network interception mode with [NetworkInterceptionMode.native].
210234
*/
211235
function handleAndroidNativeInterception() {
212236
if (isNativeInterceptionFeatureEnabled) {
@@ -230,7 +254,30 @@ function handleAndroidNativeInterception() {
230254
}
231255

232256
/**
233-
* Handles the interception mode logic for Android.
257+
* Control either to enable or disable the native interception logic for iOS.
258+
*/
259+
function handleIOSNativeInterception(config: InstabugConfig) {
260+
if (Platform.OS === 'ios') {
261+
console.log(
262+
`Andrew: handleIOSNativeInterception(${
263+
shouldEnableNativeInterception &&
264+
config.networkInterceptionMode === NetworkInterceptionMode.native
265+
})`,
266+
);
267+
268+
if (
269+
shouldEnableNativeInterception &&
270+
config.networkInterceptionMode === NetworkInterceptionMode.native
271+
) {
272+
NativeInstabug.setNetworkLoggingEnabled(true); // Enable native iOS automatic network logging.
273+
} else {
274+
NativeNetworkLogger.resetNetworkLogToDefaultStateIOS(); // Disable native iOS automatic network logging.
275+
}
276+
}
277+
}
278+
279+
/**
280+
* Handles the network interception mode logic for Android.
234281
* By deciding which interception mode should be enabled (Native or JavaScript).
235282
*/
236283
const handleInterceptionModeForAndroid = (config: InstabugConfig) => {

src/native/NativeNetworkLogger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface NetworkLoggerNativeModule extends NativeModule {
55
// Network logging Flags //
66
isNativeInterceptionEnabled(): Promise<boolean>;
77
hasAPMNetworkPlugin(): Promise<boolean>;
8+
resetNetworkLogToDefaultStateIOS(): void; // iOS only
89
}
910

1011
export const NativeNetworkLogger = NativeModules.IBGNetworkLogger;

src/utils/InstabugUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export const reportNetworkLog = (network: NetworkData) => {
197197
network.requestBody,
198198
network.requestBodySize,
199199
network.method,
200-
network.url,
200+
network.url + '/js',
201201
network.requestContentType,
202202
responseHeaders,
203203
network.responseBody,
@@ -217,7 +217,7 @@ export const reportNetworkLog = (network: NetworkData) => {
217217
console.log('Andrew: ' + 'NetworkLogger -> NativeInstabug.networkLogIOS');
218218

219219
NativeInstabug.networkLogIOS(
220-
network.url,
220+
network.url + '/js',
221221
network.method,
222222
network.requestBody,
223223
network.requestBodySize,

0 commit comments

Comments
 (0)