Auto Launch a React Native Mac App With Swift Dependencies

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Building React Native Apps for Mac course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Building React Native Apps for Mac, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Building React Native Apps for Mac

Since we are developing a status bar application, you would expect to have an option for the up to auto start whenever you log in into your computer or whenever the computer starts. This is one of the main differences between a mobile device and a desktop device that you can have multiple apps running, right? So in order to do this, there is a manual process which is very complicated and is very time consuming. And there is one framework that can actually do this for you. It's called Launch at Logging. It's developed by Sindre, which is a developer that commits a lot of open source repositories. Now the problem with this framework is that it doesn't support GoCoapods. And the first time that I implemented this functionality, I didn't want to mess with even more package managers because they all have their quirks. But after doing this process myself, I have to admit, it's very time consuming. So if you can have something that does the heavy work for you, then that's better. So we're just going to go ahead and do this via the Swift dependencies. So here you can see the instructions on the give up repo. I'm just going to copy the address of the library. And on Xcode, I'm going to go into File, Swift Packages, Add Package Dependency . I'm going to select my main target. And I'm going to paste the URL in here. Now on some versions of Xcode, this is going to require for you to log in into GitHub. And if you have two factor authentication, that's also not going to work, right ? Because Xcode does not have access to your two factor key. So you might want to try to use the SSH address for this package. In my work, in my not, on this version of my version of macOS and Xcode, I don 't need to do it. I can just use the regular HTTP address. So give it a try. So I'm going to leave things as they are right now. And here at the next window, I'm going to change the target because I don't want to launch a login at iOS. That brings me nothing. What I want to do is add it to the macOS target. And then what the Xcode is going to do is going to fetch the package and it's going to add it to our project as a dependency. So now that we have the framework integrated, I need to do one more thing. If you go back into the GitHub repository, you will see that you need to add a script phase. So I'm going to go back into first. I'm going to take this value. I can just copy it. I'm going to go back into Xcode. I'm going to go into my macOS target and build phases. And after the copy bundle resources phase, this is where I need to insert my new file. So I'm going to click on this plus icon, run new script phase. I'm going to change the name to the launch at login script. And I'm just going to drag it to make sure that it's under the copy bundle resources. Here I am just going to take what I got from the repository. And I don't even need to save it. Everything is taken care of. So let's just try to compile the app and see what happens. So after compiling a wrap, you see that we got an error. Here I got a Swift get a big type conformance. It's not found. This really, if you're not a native developer, it's really hard to get a feeling for what might be wrong here. But it has to do something with Swift, right? I'm kind of showing you this in order for you to get a rough feeling of how you can get clues of what might be wrong, especially if you're not a native developer, right? You have to gather information from whatever you have. So this program is because we are embedding two versions of the Swift libraries . I found a solution by Googling, but we're just going to take care of it right now. So I'm going to go back into my explorer. I'm going to open the root object. And I'm going to make sure that I am on the project and select the value here from my application. Then on build settings, I'm going to search for the library search paths. And this is the key that I'm interested in. If I double click this, you should see in here that we here, like I said, we're integrating two types of the Swift libraries. So I'm just going to delete this key. And now I'm going to try to compile the app one more time. And there you go. So our application has compiled. Now we have the framework fully integrated into our library. Now it's time to connect it to our UI. So once again, I'm going to use a very known trick by now, which is I'm going to expose native functionality to our JavaScript side of things. And I'm going to call this get start at login status, right? This is going to be a function that is going to extract the current value of the flag that we're going to set in order to start the app. So we can display it on the UI. This one is going to take the RCT promise resolve block, which I'm going to call the resolve function. And the rejector is going to be the RCT promise reject block, which is just a reject. Great. So this is going to allow us to read the value. We still need to add one more to set the value. So set start at login status. And this is going to take Boolean, which is the status. I'm going to go back to my Swift object. This time I'm just going to take the code from the lesson. And there you go. There is one more thing that I need to do, which is I need to import the library. Launch add login. Great. So now, let's go. It's not throwing any errors. Let me compile the app. Great. So the application starts. And now I just have to show it to the user. Once again, on my native class, I'm going to get start at login status. This one returns a promise with a Boolean inside of it. And the set start at login status. This takes the status as a Boolean. And this is just void. I mean, of course, I could return a promise, but I'm just going to assume that 's going to work at the first try. Set start at login status. And get start at login status. Great. So now I only need to display a UI element for the user to see what is being done. Let me just check it if everything is all right. Great. So on my books container, I am going to add another promise. So I'm going to add a flag, which is going to be my start at login status. And set start at login status. I'm going to use a use state that's going to start as false, right? The application starts. We still don't know if the flag has been set or not. On my use effect hook, I'm going to add going to call my building apps native, get start at login status. That promise is going to resolve and whatever that results to, I'm going to pass it to my set function from my hook. And then down here, I am going to add a text. Let me add first a view inside of it with text. And here I'm going to say this is launch application at login. I'm going to use a switch component, which is part of React Native. The value is going to be my start at login status. And whenever the value changes, I need to call my native function. So I'm going to call the building apps native. Let's start at login status with the negated value of start at login status. I also have to replicate the status at my local flag. And there you go. So now. This is probably because we now have more hooks. Let me just check one more time. Yeah, it doesn't matter. So I'm going to dismiss these errors and then right click and reload the application. Great. And here you can see now we have our launch application at login. If I set this and let me just reload the app to make sure everything is working , then it will be working. Now I cannot show you this in action because then I would have to login and my screen would stop recording. But when you're developing this on your own, you can package your application, archive it in release mode because you need the app to be somewhere stored independently. Like you cannot run it from Xcode, turn on the flag and then when you turn on your computer expected to run, the application needs to be packaged. So just a quick tip. If you don't know how to package the application, you go into product archive and then it's going to produce some options where you can export the application. Then you run it and if you set this value to true and you restart your computer or you can log out and log in one more time. Then your application should be there when the computer starts. [BLANK_AUDIO]