How to display the current project version of my App to the user?

后端 未结 6 1391
遥遥无期
遥遥无期 2020-12-01 13:01

I would like to add the current version into the \"about\" section of my app. As seen in this attached screenshot Apple offers versioning.

How do you display these s

相关标签:
6条回答
  • 2020-12-01 13:03

    NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"];

    0 讨论(0)
  • 2020-12-01 13:09

    After further searching and testing, I found the solution myself.

    NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSLog(@"%i Keys:  %@", [infoDictionary count],
                 [[infoDictionary allKeys] componentsJoinedByString: @" ,"]);
    

    This snipplet gave me the following output:

    20 Keys : NSBundleResolvedPath ,CFBundleVersion ,NSBundleInitialPath ,CFBundleIdentifier ,NSMainNibFile ,CFBundleIconFile ,CFBundleInfoPlistURL ,CFBundleExecutable ,DTSDKName ,UIStatusBarStyle ,CFBundleDevelopmentRegion ,DTPlatformName ,CFBundleInfoDictionaryVersion ,CFBundleSupportedPlatforms ,CFBundleExecutablePath ,CFBundleDisplayName ,LSRequiresIPhoneOS ,CFBundlePackageType ,CFBundleSignature ,CFBundleName

    So the solution is as simple as:

    NSString *version =[[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"];
    

    However, this is not the Current Project Version as seen in the screenshot but the Bundle Version of the plist file.

    0 讨论(0)
  • 2020-12-01 13:10

    For the lazy here is the swift version :

    NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString")!
    
    0 讨论(0)
  • 2020-12-01 13:11

    Those items in the Build Info are not available to your built app. They are placeholders that you could possibly pull into your app. What is in your app is anything that you place in, say, the Resources folder of your app, like any text files, or plists, or a nice picture of your versioning engineer.

    Now, you could pull some of the items in the Build Info window into a info.plist, using special identifiers, such as ${VERSION_INFO_PREFIX} or other token. The tokens are available if you click on any of the items on the left hand side in the window you have included above. For example, click on the word "Current Project Version" and copy the token that you see at the bottom, "CURRENT_PROJECT_VERSION". Then go to your plist file, and add an entry. Give it any name you want or "Current Project Version". Paste in ${CURRENT_PROJECT_VERSION} on the right hand side. Now that value is available to you from your app, programmatically. Of course, someone now has to enter that value into the appropriate place either in the Build Info window or elsewhere. It might just be easier just to manage this and fields like this in the info.plist file. It's up to you how you'd like to handle these things.

    Here is how I get version info out of my info.plist:

    + (NSString *) getAppVersionNumber;
    {
        NSString    *myVersion,
                    *buildNum,
                    *versText;
    
        myVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        buildNum = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
        if (myVersion) {
            if (buildNum)
                versText = [NSString stringWithFormat:@"Version: %@ (%@)", myVersion, buildNum];
            else
                versText = [NSString stringWithFormat:@"Version: %@", myVersion];
        }
        else if (buildNum)
            versText = [NSString stringWithFormat:@"Version: %@", buildNum];
        NSLog(versText);
        return versText;
    }
    
    0 讨论(0)
  • 2020-12-01 13:16
      NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"];
    

    returns me always the same value(initial version) even if i change the version from the project settings but.

      NSString * appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    

    does the trick.

    0 讨论(0)
  • 2020-12-01 13:26

    Look into your Info.plist file which should have keys like CFBundleVersion and CFBundleShortVersionString

    0 讨论(0)
提交回复
热议问题