简体   繁体   English

使用 Swift 在启动屏幕中显示应用程序版本

[英]Show version of app in launch screen with Swift

Scenario设想

I want to show the version of my iOS 9 app made with Swift.我想展示我用 Swift 制作的 iOS 9 应用程序的版本。

What I did我做了什么

I know how to get the version ( let version: String = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String )我知道如何获取版本( let version: String = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String

I also have a custom label on my home screen.我的主屏幕上也有一个自定义标签。

My problem我的问题

My problem is now, that it is not allowed to use your own UIViewController for the splash / launch screen.我现在的问题是,不允许将您自己的 UIViewController 用于启动画面/启动屏幕。

Many apps show their version right on that screen.许多应用程序直接在该屏幕上显示其版本。 That's why I think that there must be a way how to do it.这就是为什么我认为必须有一种方法来做到这一点。

You can create a script in Build Phases of your project.您可以在项目的构建阶段中创建脚本。 But make sure you do a couple of things first.但请确保您先做几件事。

Go to your LaunchScreen.storyboard ViewController and create your version Label.转到您的 LaunchScreen.storyboard ViewController 并创建您的版本标签。 Make sure to name your label to "APP_VERSION" in Identity Inspector pane -> Document -> Label.确保在身份检查器窗格 -> 文档 -> 标签中将标签命名为“APP_VERSION”。

Then go to your project's build phases and create your script by clicking the "+" button in the top left corner, then select "New Run Script Phase" from the pulldown menu and then drag it before "Copy Bundle Resources" phase.然后转到项目的构建阶段并通过单击左上角的“+”按钮创建脚本,然后从下拉菜单中选择“新建运行脚本阶段”,然后将其拖到“复制捆绑资源”阶段之前。

UPDATE: My older answer didn't work in the newest Xcode environment.更新:我的旧答案在最新的 Xcode 环境中不起作用。 I've fixed the current issues and refactored the script.我已经修复了当前的问题并重构了脚本。 I'll update when I have the incrementer working.当增量器工作时,我会更新。 FYI, make sure you reinstall the app for the updated LaunchScreen.storyboard to show (due to system managing caching of the launch screen in latest versions of iOS).仅供参考,请确保您重新安装应用程序以显示更新的 LaunchScreen.storyboard(由于系统管理在最新版本的 iOS 中启动屏幕的缓存)。

And here's the final working script with shell: /bin/sh in XCode 11 (Swift 5) :这是带有 shell 的最终工作脚本:/bin/sh in XCode 11 (Swift 5)

#   ON/OFF Script Toggle (script ON with #, script OFF without #)
#exit 0

#   App vesion / Build version constants
sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/Base.lproj/LaunchScreen.storyboard"
versionNumber="$MARKETING_VERSION"
buildNumber="$CURRENT_PROJECT_VERSION"

#   Output version & build numbers into a label on LaunchScreen.storyboard
sed -i .bak -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"$versionNumber($buildNumber)\"/" "$sourceFilePath"

Updated @Repose script更新了@Repose 脚本

#   ON/OFF Script Toggle (script ON with #, script OFF without #)
#exit 0

#   Increment Build Number Bool (Increment ON with true, increment OFF with false)
shouldIncrement=true

#   App vesion / Build version constants
versionNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")

#   Increment build number
if [ "$shouldIncrement" = true ]; then
    buildNumber=$(($buildNumber + 1))
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
fi

#   Output version & build numbers into a label on LaunchScreen.storyboard
sed -i bak -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"Version: $versionNumber Build: $buildNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/Base.lproj/LaunchScreen.storyboard"

Its not allowed to use any UIViewController in LaunchScreen thus what other applications does is place a UILabel in LaunchScreen xib/Storyboard and write their version number as text.它不允许在 LaunchScreen 中使用任何UIViewController因此其他应用程序所做的是在 LaunchScreen xib/Storyboard 中放置一个UILabel并将它们的版本号写为文本。

There might be some work around's, Its still not preferred or allowed by Apple, don't waste your time searching you will get the following error if you tried to set custom class :可能有一些解决方法,Apple 仍然不喜欢或不允许它,不要浪费时间搜索,如果您尝试设置自定义类,您将收到以下错误:

error: Illegal Configuration: Launch screens may not set custom classnames

NOTE: What usually apps do is create another LaunchScreen UIViewConttoller directly after the default launch screen with same design and presented without animation, for example to receive some data.注意:通常应用程序所做的是在默认启动屏幕之后直接创建另一个 LaunchScreen UIViewConttoller具有相同的设计并且没有动画呈现,例如接收一些数据。

Making Your Version Number Super-Visible in iOS:使您的版本号在 iOS 中超级可见:

Start by adding a label to your LaunchScreen.storyboard :首先向LaunchScreen.storyboard添加标签:

  1. Format the label however you like.随意格式化标签。 I like to use something subtle that you can read when you need to but that isn't very noticeable otherwise.我喜欢使用一些微妙的东西,您可以在需要时阅读,但否则就不会很明显。
  2. Set Document/Label to APP_VERSION .Document/Label设置为APP_VERSION You will use this value in the Build Phase script.您将在构建阶段脚本中使用此值。

Continue by adding the custom Build Phase:继续添加自定义构建阶段:

  1. Select your project in the upper-left-hand corner of Xcode.在 Xcode 的左上角选择您的项目。
  2. Select the app target towards the middle of the screen.选择屏幕中间的应用程序目标。

You should see a strip of tabs that includes General , Capabilities , […], Build Settings , and then Build Phases .您应该会看到一排选项卡,其中包括GeneralCapabilities 、 [...]、 Build SettingsBuild Phases

  1. Click on the + above the list of build phases and choose单击构建阶段列表上方的+并选择

    New Run Script Phase新的运行脚本阶段

  2. Rename the phase to something like “Show Version on LaunchScreen”.将阶段重命名为“在 LaunchScreen 上显示版本”之类的名称。 Xcode can be finicky about this part; Xcode 可能对这部分很挑剔; if you can't rename the phase, don't worry too much about it.如果你不能重命名阶段,不要太担心。

  3. Move the phase somewhere above Copy Bundle Resources .将阶段移到Copy Bundle Resources上方的某个位置。

  4. Paste the following into the script area:将以下内容粘贴到脚本区域:

    # Output version & build number to the APP_VERSION label on LaunchScreen.storyboard
    versionNumber="$MARKETING_VERSION"
    buildNumber=$CURRENT_PROJECT_VERSION"
    
    sed -i "" -e "/userLabel=\"APP_VERSION\"/s/text=\"[^\"]*\"/text=\"Version: $versionNumber ($buildNumber)\"/" "$PROJECT_DIR/$PROJECT_NAME/Base.lproj/LaunchScreen.storyboard"

source: https://thoughtbot.com/blog/making-your-version-number-super-visible来源: https : //thoughtbot.com/blog/making-your-version-number-super-visible

You can try to do it with LaunchScreen.storyboard .您可以尝试使用LaunchScreen.storyboard来实现。 Try to add new LaunchScreen to your project with New File -> User Interface -> Launch Screen It automatically creates a storyboard type of LaunchScreen .尝试使用New File -> User Interface -> Launch Screen将新LaunchScreen添加到您的项目中。它会自动创建LaunchScreen类型的storyboard Then you can change the initial Launch Screen on project settings -> General in this section:然后您可以在project settings -> General中更改初始启动屏幕:

在此处输入图片说明

And work with this storyboard with your custom class并使用您的自定义类使用此storyboard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM