From the course: .NET 9 Maui: Enhanced Features for Cross-Platform Development

iOS multitarget bindings

- [Instructor] Normally, when an iOS library is compiled, it is compiled for a particular instance of the iOS SDK. In the past, this has been whatever version of the SDK is installed on the machine that is doing the compilation. This normally is fine because Apple has been very insistent on convincing users to upgrade their machines to the latest version of iOS and Mac OS. However, there are some cases where we want our library to be built against more than one version of iOS due to differences in the requirements of native libraries we are binding to. For example, we may have some older applications that rely on being compiled against an older version of iOS, as well as a newer version because there's older iOS machines in the environment. With MAUI and .NET 9, this process has been streamlined substantially. As long as the older iOS versions are installed on the build machine, multiple iOS targets can be specified as part of the build process. However, one thing that Microsoft states is this should only be done with projects like binding libraries, not with the main MAUI app project. While it seems like it's possible to do, a warning will be generated, and Microsoft also warns specifically against it in their documentation. So let's take a look at how this works. In the start code for this video, we have our normal QRTracker project, and there's also a new empty iOS binding project in it. And let's take a look at the project file. So we'll go in here and we'll open up the project file. If we look at the target framework line, we can see a single entry for net 9.0 iOS. This is a default and will compile against whatever is the latest iOS version installed. This is normally what we want, but what if additionally, we wanted to compile against another specific iOS version? How would we do that? And we can do that in the target frameworks line, and we can add in another version. So we're going to change this from target framework to target frameworks, just like that. And instead of 9.0 iOS, what we're going to compile to is net 9.0 iOS, we'll make it 18.4 maybe, and maybe 18.2. Just like that. Here we go. Actually let's make it 18.0 and 18.2. And now we'll save the file. And this gives us the ability to build against iOS 18 as well as 18.2. And I removed the default .NET 9 iOS setting so we can control exactly what versions we are going to build against. So let's take another look at that. So we're saying exactly on 18.0 and 18.2. We can have multiple different versions if we want to compile against them. One thing to be careful of is that if we leave the default net 9.0 iOS entry in place, the latest version of iOS installed on the build machine cannot be any of the specific versions we specify. If it is, and error will occur compile time as it tries to compile the same version twice. So let's actually try compiling this. So we're going to do the build, I'm going to go up a directory into the main QRTracker directory, and then we'll go into our iOS bindings project. So we'll go cd testiosbinding, and now we'll compile. So we'll go dotnet and build, and we'll go C and release. And F, we'll go net9.0-ios18.0. And that will do version 18.0. And if we wanted to do 18.2, we could have specified that instead. If we want to build both at once, we could simply leave the -f switch off and all frameworks specified in the project will be built. So what we do instead is just like that. And this will specify both frameworks. Remember, we can apparently use the same technique to build the main MAUI app project. A warning will be produced, and Microsoft specifically warns against doing so, but it will still compile against both versions of iOS. If you have some reason you need to do this, it isn't officially supported by Microsoft with all that entails.

Contents