background

The text in launchscreen. stortboard must be static, and the launchscreen. stortboard must be manually modified every time the version number is changed in pubspec. After packaging it a few times, I decided to automate it.

Copy the code

Train of thought

Following stackOverflow ideas, but unlike pure ios projects, the script is intended to be written in DART. You basically read the version number in the pubspec.yaml file and replace the launchscreen.stortboard file

steps

1. Add code



replace_launch_screen.dart

import 'dart:io';
main() {  
    File launchScreenFile = File(".. /ios/Runner/Base.lproj/LaunchScreen.storyboard");  
    File pubSpecFile = File(".. /pubspec.yaml"); 
    
     1. Read the version number from pubSpec
    String pubSpecFileContent = pubSpecFile.readAsStringSync();  
    RegExp versionReg = new RegExp("[\r\n]\\s*version:\\s*(\\S+)\\+(\\d+)");  
    RegExpMatch versionMatch = versionReg.firstMatch(pubSpecFileContent);  
    print(versionMatch.group(1) + ":" + versionMatch.group(2));  
   
     // 2. Get the replacement string from lauchScreen
    String launchScreenFileContent = launchScreenFile.readAsStringSync();  
    // print(launchScreenFileContent);  
    RegExp versionTextReg = new RegExp('text="\\S+"');  
    RegExpMatch versionTextMatch = versionTextReg.firstMatch(launchScreenFileContent);  
    // print(versionReg.hasMatch(launchScreenFileContent));  
    print(versionTextMatch.group(0));  
    String strToBeReplaced = versionTextMatch.group(0);  
    String replacedLacunchScreen = launchScreenFileContent.replaceFirst(versionTextReg, "text=\"${versionMatch.group(1)}\ "");  
    launchScreenFile.writeAsString(replacedLacunchScreen, mode: FileMode.writeOnly);
}
Copy the code

2. Xcode project add execution





"$FLUTTER_ROOT/bin/cache/dart-sdk/bin/dart" ../scripts/replace_launch_screen.dart Copy the code

Description:

Generally speaking, the code is simple and the steps are not complicated. However, dart is used to replace it, which is consistent with the project code and easy to understand. If you need it, you can refer to it.