Call iOS Swift Code

8 minutes
Share the link to this page
Copied
  Completed
Learn how to call Swift code from Dart.

Transcript

Hello, in this video, I am going to show you how to call swift code from your Dart flutter based project. So we this is actually not too difficult. I've got another video that covers how to send parameters as well. So feel free to take a look at that. That's an extension of this video, make sure you watch the video then that one video covering how to do for Java for Android for kotlin for Android, and for Objective C for iOS, so I've got it all covered. And first of all in your Dart file, you want to do a new import.

This import is going to be package colon, floater first last services dot dot, I need a semicolon and the error disappeared. Now if we scroll down to our class, you know where our main application is. We need to create a basically a platform This is going to be the connection the channel that the dots I will use to connect to the swift side we're gonna have a static cons, platform equals cons. Method channel. And in here, you can put a string, you can really put wherever you want. But the format that I like to use is a package store name.

So comm dot, dot epic, for example, like to do a forward slash, you can pull everyone to receive your company name, your product name wherever it is. And so the way I like to structure it is like this. So you have some sort of package name that can be, you know, almost like the parent, you know, platform connection, and then you can add sub channels sub connections, as well. But again, you can put whatever you want. And now if we scroll down, we're going to implement another method it's going to be avoid going to printing. This simply is going to get some result from Swift.

So something's going to be you know, return, it's gonna be a simple string, and that is going to print it out. So we just put a thing with asynchronous. And before I forget, I've got this pattern. The moment it doesn't do anything, but when it's clicked, it's gonna call this method. And in here, we're gonna create a string. And this string will store the result from our swift code, we just need to try Troy, learn you equals away.

Platform, dot invoke method. And here, we put the unique method that we are going to call on that particular channel. I'm going to call it property lists. And this have no relation with each other. So we can call this whatever we want. This has been same as what we put in the swift side, which I'm going to show you in a second.

Give me an error. I haven't implemented the catch block. And now we'll print out the error if there is any shouldn't be, but just in case, we can debug it, and I'm just going to print out the moment So that's it. That's all of the dot sign done. Now what you want to do is go to this iOS folder, open that up, go to run app, go to app delegate dot Swift, you can edit it and add the code in here. But as you can see, there's like no syntax highlighting, there'll be a no IntelliSense, none of that.

But I recommend clicking open iOS module in Xcode, Oregon to basically open up the iOS project for your flutter project in Xcode, and make sure you open up app delegate dot Swift. And there you go, you have all of the code in we've IntelliSense with, you know, syntax highlighting, highlighting all of that good stuff in here. Within this method, what you want to do is basically implement a channel and a controller to be able to detect the method being called, first of all, you'd be next. flutter View Controller equals window, question mark dot root view controller view controller. Now we're going to create a channel for next channel flutter method channel. And first of all we need to specify a name and this name is going to be the exact same this channel so I would recommend just copying it to make sure no mistakes occur otherwise you will not work even if one character is wrong on the book binary messenger and if you don't believe me, okay, so now what we want to do is essentially, you know, create a method that gets triggered when something is trying to communicate with this channel.

So you do channel dot set method call handler. And what we want to do, we're going to get rid you're going to put braces on our self. method, quarterly, these two parameters are going to be passed in. When this is triggered method call which will have stuff like which method is being called the actual arguments as well, that I'm covering in the next video and result which is what you are returning an essay in And here, we need to just put So if method call, don't get method call dot nepad is equal to 20. So this needs to match what you put in the invoke method. If it's identical to that, we also result.

And when we put in here it can be a string could be in the middle really matter, that's going to get returned for the result is why you can turn on say fine. With now we're ready to run. Just something to bear in mind. When you make changes on your iOS side for Swift, or Objective C is the same with Android, Java on the kotlin side, need to shut down the application if it's already open by pressing that button. Then we're running it or we debugging it. That's because if you just turn to a hot lead, hot reload or a hot restart Not only the dot, you know, changes will get pushed, or your West will not get rebuilt, therefore, you will not get the new changes.

So just by coming out, click that we get Yeah, I mean, we get hi from Swift, and that code is right here. And that's it. If you had multiple methods that were on this particular channel, you could just do an elsif method called on method equals this other particular method. And you could have multiple channels as well. Here, you could create multiple swift files, you could import your libraries, you could have classes, do whatever you want, really, once you've got to this point. And and if you know Swiss, the you know, the sky's the limit, you can literally do whatever you want.

So that's it. If you have any questions, feel free to pop me a message. In the next video, we are going to come up passing in parameters into swift so we can actually get a bit more customization. So I look forward to seeing you in that video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.