How to get version string from info.plist

dswdsw Member ✭✭

I've searched all sorts of variations on how to read a property from the info.plist file programmatically. All posts say to use something along the lines of:

var versionString = NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleVersion")].ToString();

However, when I try to use this, Visual Studio gives the red underline on NSBundle and NSString. None of the posts show what the using statement for NSBundle is (if there is one), or give any guidance on usage...

I'm a total Xamarin and c# n00b - please, can someone tell me how to use NSBundle in a .cs file to read the identity.version number from the info.plist file.

Xamarin Forms 4.1.0.581479
Visual Studio Community 2019 for Mac

Tagged:

Best Answer

Answers

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    The using statement should be

    using Foundation;

  • ColeXColeX Member, Xamarin Team Xamurai
    edited July 22

    Can't you use the vs tool to show potential fixes ?

    And when i was typing NSBundle Xamarin on google , the first result is related to it .

  • dswdsw Member ✭✭

    Thanks guys, yes I did see those results, but when I add using Foundation; to the c# file, I also get a red underline under Foundation - does this need to be installed? I couldn't see it on Nuget...?

  • dswdsw Member ✭✭

    @ColeX said:
    Can't you use the vs tool to show potential fixes ?

    And when i was typing NSBundle Xamarin on google , the first result is related to it .

    The editor doesn't give me the option to add a using for Foundation in the quick-fixes popup

  • ColeXColeX Member, Xamarin Team Xamurai
    edited July 22

    It is weird , are you coding in iOS project ?

    Check the references in your project , is Xamarin.iOS in it ?

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭
    edited July 22

    Just to be sure, you are adding your code in the ios project of your forms app, right?

    Also see this thread in the forms forum: https://forums.xamarin.com/discussion/104922/how-to-set-get-the-app-version-number-and-build-for-cross-platform

  • dswdsw Member ✭✭

    @ColeX - there is an item for Xamarin.iOS in the References folder of the .iOS project yes

    @DirkWilhelm - well, I want to add the code to one of my view models. The view models are in the main project of the solution, rather than the iOS-specific project? Is this right? If not, where would I put this code in the iOS project, and how would I get it to my view model?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited July 22

    Take a look into Dependency Service .

    Implement it in specific project , call it in Forms project.

  • dswdsw Member ✭✭

    @ColeX @DirkWilhelm - perfect, thanks for your help guys.

    We are using Xamarin Essentials already, so all I had to do in the end was add using Xamarin.Essentials; and public string versionNumber { get; } = AppInfo.VersionString; to the view model :smile:

Sign In or Register to comment.