Skip to content

Commit e9f1909

Browse files
Fix broken AppDelegate after injecting the sources
It's possible that methods implemented by this plugin are already implemented in AppDelegate.m file. Using the script with its current state leads to a compile-time error: "Duplicate implementation for a method: xxx" We have to check first if the method is implemented, and if it is then we inject the new code to the existing implementation. If it's not then we just put in the whole method. Like we did before.
1 parent 108b9f5 commit e9f1909

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

scripts/ios/custom-widget.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ if (rootdir) {
1919
return path.join(projectRoot, "platforms", platform, cfg.name(), relPath);
2020
};
2121

22+
var appendTo = function(path, methodHeader, implementation) {
23+
var data = fs.readFileSync(path, "utf8");
24+
var indexOfMethodHeader = data.indexOf(methodHeader);
25+
if (indexOfMethodHeader == -1) {
26+
return false;
27+
}
28+
var result = data.replace(methodHeader, methodHeader + '\n' + implementation);
29+
fs.writeFileSync(path, result, "utf8");
30+
return true;
31+
};
32+
2233
var replace = function(path, to_replace, replace_with) {
2334
var data = fs.readFileSync(path, "utf8");
2435
var result = data.replace(to_replace, replace_with);
@@ -31,7 +42,14 @@ if (rootdir) {
3142
var finishLaunchingReplace = "/* HOOK: applicationDidFinishLaunching */";
3243
replace(appDelegate, importReplace, "#import \"ESFBMessaging.h\"\n" + importReplace);
3344
replace(appDelegate, finishLaunchingReplace, "[ESFBMessaging setLaunchData:launchOptions];\n\t" + finishLaunchingReplace);
34-
replace(appDelegate, "@end", "- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {" + "\n\t" + "[ESFBMessaging notificationReceived:userInfo];" + "\n\t" + "completionHandler(UIBackgroundFetchResultNewData);\n}" + "\n\n" + "@end")
45+
46+
47+
var header = "- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {";
48+
var implementation = "\t[ESFBMessaging notificationReceived:userInfo];" + "\n\t" + "completionHandler(UIBackgroundFetchResultNewData);\n";
49+
var method = header + "\n" + "\t[ESFBMessaging notificationReceived:userInfo];\n" + '}';
50+
if (!appendTo(appDelegate, header, implementation)) {
51+
replace(appDelegate, "@end", method + "\n" + "@end");
52+
}
3553
};
3654

3755
updateIOSAppDelegate();

0 commit comments

Comments
 (0)