ios静态库和代码同名_使用一个代码库开始制作NativeScript iOS和Android应用程序

陈昊昊
2023-12-01

ios静态库和代码同名

Users can choose whatever operating system they prefer, but every operating system use different languages. In the mobile world, Android natively supports the Java and Kotlin languages. If you want to make an iOS app, guess what? Your Java or Kotlin cannot be compiled for that platform. That means you have to start from scratch, rewriting your app in Swift or worse… Objective-C!

用户可以选择他们喜欢的任何操作系统,但是每种操作系统使用不同的语言。 在移动世界中,Android原生支持Java和Kotlin语言。 如果您想制作一个iOS应用,您猜怎么着? 您的Java或Kotlin无法针对该平台进行编译。 这意味着您必须从头开始,用Swift或更糟糕的方式重写应用程序……Objective-C!

The promise of cross-platform app development is that you can write code once and serve users wherever they are.

跨平台应用程序开发的承诺是,您只需编写一次代码即可为用户提供服务,无论他们身在何处。

This capability is perhaps most famously provided by React Native, the framework provided by Facebook. However React Native lacks the Model-View-Controller (MVC) architecture many developers have come to expect. It is only really a starting point for user interfaces, but requires other third-party libraries to handle data.

该功能可能最著名的是由Facebook提供的框架React Native提供的。 然而,React Native缺乏许多开发人员期望的Model-View-Controller(MVC)体系结构。 它实际上只是用户界面的起点,但需要其他第三方库来处理数据。

Xamarin allows you to use C# and much of the capabilities of the Microsoft .NET platform. However, getting a truly native look and feel on Xamarin requires separate user interfaces. The Xamarin.Android interface is built using XML, and so is the Xamarin.iOS one… in a way. Xamarin.iOS relies on Storyboards, which Apple’s Xcode IDE provides as a drag and drop experience that is ultimately saved as a complex and practically unreadable XML file.

Xamarin允许您使用C#和Microsoft .NET平台的许多功能。 但是,要在Xamarin上获得真正的本机外观,需要单独的用户界面。 Xamarin.Android接口是使用XML构建的,Xamarin.iOS也是如此。 Xamarin.iOS依赖于Storyboards,Apple的Xcode IDE提供了Storyboards的拖放体验,最终将其保存为复杂且几乎不可读的XML文件。

It should be pointed out that Xamarin.Forms exists, a platform that allows you to use Microsoft’s Extensible Application Markup Language (XAML) to create one interface that is shared by both platforms. One advantage of NativeScript is the web-based sandbox NativeScript Playground, which allows you to experiment with code and check out the many code samples provided by the community.

应该指出的是,存在Xamarin.Forms,该平台允许您使用Microsoft的可扩展应用程序标记语言(XAML)创建一个由两个平台共享的接口。 NativeScript的优势之一是基于Web的沙箱NativeScript Playground,它使您可以尝试代码并检查社区提供的许多代码示例

When starting with a Playground you’re confronted with a huge QR code. Scanning the QR code with NativeScript’s app allows you to try the app on your phone, all without needing to compile the code yourself. While Xamarin may have a similar community of developers sharing plugins, it lacks this easy sandbox that anyone can use without installing any software on their computer. NativeScript also has the advantage of supporting web frameworks like Angular, Vue.js, React and Svelte. This highlights perhaps the most important distinction between the two platforms.

从Playground开始时,您会遇到巨大的QR码。 使用NativeScript的应用程序扫描QR码,使您可以在手机上尝试该应用程序,而无需自己编译代码。 尽管Xamarin可能有类似的开发人员共享插件社区,但它缺少这样一个简单的沙箱,任何人都可以在无需在计算机上安装任何软件的情况下使用它。 NativeScript还具有支持Angular,Vue.js,React和Svelte等Web框架的优势。 这也许突出了这两个平台之间最重要的区别。

Since NativeScript employs Just In Time (JIT) compilation, it is possible to use Live Sync. This means that code changes you make will be instantly present on the emulator or device you’re debugging on, making it easier to debug without having to recompile the entire app ahead of time as you would with Xamarin or either of the native platforms themselves.

由于NativeScript使用即时(JIT)编译,因此可以使用实时同步。 这意味着您所做的代码更改将立即显示在您要调试的仿真器或设备上,从而使调试变得更容易,而不必像使用Xamarin或任何本机平台本身那样提前重新编译整个应用程序。

NativeScript makes it easier for people with web skills to make native apps, whilst still being able to use the components and CSS they’re used to.

NativeScript使具有Web技能的人们更容易制作本机应用程序,同时仍然能够使用它们惯常使用的组件和CSS。

Xamarin.Forms allows people more familar with Microsoft’s .NET platform, the plugins provided by the NuGet gallery, and the C# language.

Xamarin.Forms使人们对Microsoft的.NET平台,NuGet画廊提供的插件和C#语言更加熟悉。

Here is a more detailed comparison of Xamarin vs NativeScript from their website.

这是他们网站上Xamarin与NativeScript的更详细比较

This tutorial covers the process for installing NativeScript on your computer so that you can actually compile your projects and run them on a device or emulator. This is so that you can emulate an Android device even if you don’t own one, or an iOS device if you have a Mac. NativeScript has a good tutorial on building an app that would help you to get used to the requirements for an app, but I thought I’d focus on the process of setting your computer up for development.

本教程介绍了在计算机上安装NativeScript的过程,以便您可以实际编译项目并在设备或仿真器上运行它们。 这样一来,即使您没有设备,您也可以模拟Android设备;如果您有Mac,则您可以模拟iOS设备。 NativeScript在构建应用程序方面提供了很好的教程 ,可以帮助您适应应用程序的需求,但是我认为我将专注于为开发设置计算机的过程。

There are numerous problems you can face when installing NativeScript, so I’m hoping that my instructions help people who get stuck during the process.

安装NativeScript时可能会遇到许多问题,所以我希望我的说明能帮助那些在此过程中陷入困境的人。

My advice is based on the struggle I had to install NativeScript on Windows and Mac, including the command line arguments some of which are hard to find online.

我的建议是基于我必须在Windows和Mac上安装NativeScript所进行的努力,其中包括命令行参数,其中一些很难在网上找到。

下载并安装SideKick (Downloading and installing SideKick)

SideKick is a Graphical User Interface (GUI) for NativeScript’s Command Line Interface (CLI).

SideKick是用于NativeScript命令行界面(CLI)的图形用户界面(GUI)。

Although it is possible to do everything we’ll be doing using the CLI alone, it is easier for a beginner to get started with an interface that clearly displays the available options.

尽管可以单独使用CLI来完成我们将要做的所有事情,但是对于初学者来说,使用界面清晰地显示可用选项更加容易。

The installer will notify you if you are missing the tools that SideKick requires. Other than the CLI, you will need a version of Node.js and a version of the Android SDK no earlier than 26 but no later than 29. The SideKick installer will attempt to install these for you, but if it fails for some reason, you can use the command line yourself to install the missing tools. SideKick should display the command line commands that you’ll need to install the missing tools, but otherwise these commands are available online.

如果您缺少SideKick所需的工具,安装程序将通知您。 除了CLI之外,您还需要不早于26但不晚于29的Node.js版本和Android SDK版本。SideKick安装程序将尝试为您安装这些文件,但是如果由于某种原因失败,您可以自己使用命令行来安装缺少的工具。 SideKick应该显示安装缺少的工具所需的命令行命令,否则这些命令可在线获得。

The main thing you need to know is how to access the command line on your platform. On Windows 10, open the Start Menu, type cmd, right-click on Command Prompt and choose run as administrator. If you aren’t an administrator on your PC, you may not be able to complete some of the installation steps. In my case, trying to run the SideKick installer without being an administrator caused the installer to think the CLI wasn’t installed, and telling the installer to fix that resulted in silent failure.

您需要了解的主要内容是如何访问平台上的命令行。 在Windows 10上,打开“开始”菜单,键入cmd,右键单击“命令提示符”,然后选择“以管理员身份运行”。 如果您不是PC的管理员,则可能无法完成某些安装步骤。 就我而言,尝试在没有管理员的情况下运行SideKick安装程序会导致安装程序认为未安装CLI,并告诉安装程序进行修复,从而导致静默失败。

Running the installer as an administrator allowed the installer to confirm the existence of the CLI and open the GUI.

以管理员身份运行安装程序可让安装程序确认CLI的存在并打开GUI。

When SideKick has installed correctly, you’ll want to make sure you didn’t miss anything. Run the command prompt as an administrator again and type tns doctor . This is a diagnostic that will search for any remaining tools that your device does not yet have installed. If you’re lucky, you’ll get green ticks next to all of the required tools, with a message saying ‘No issues were detected’. If you want to be able to debug your app on Android, it’s worth checking if you have any emulators. Use the avdmanager list avd terminal command, to see if you have any emulators. If you’re don’t and you’re on a Mac, try the command xcrun simctl list to see if you have available iOS Simulators.

SideKick正确安装后,您将要确保您没有错过任何东西。 再次以管理员身份运行命令提示符,然后键入tns doctor 。 这是一种诊断,它将搜索您的设备尚未安装的所有剩余工具。 如果幸运的话,所有必需的工具旁都会出现绿色的勾,并显示一条消息“未检测到问题”。 如果您希望能够在Android上调试应用,则值得检查是否有任何模拟器。 使用avdmanager list avd terminal命令查看是否有任何模拟器。 如果您不是,并且您使用的是Mac,请尝试使用命令xcrun simctl list查看是否有可用的iOS模拟器。

If you have iOS or Android emulators or Simulators, you can skip to the ‘creating a new project in Sidekick’ section.

如果您具有iOS或Android仿真器或模拟器,则可以跳到“在Sidekick中创建新项目”部分。

For the rest of you, here are some tips on fixing those problems.

对于其余的人,这里有一些解决这些问题的技巧。

解决缺少软件的问题 (Fixing problems with missing software)

If you’re on a Mac and want to build to iOS, you’ll need to get Xcode on the App Store. When I was running tns doctor, it said that I didn’t have Xcode installed, even though I did. If you experience this, the terminal command sudo xcode-select --reset fixed the problem for me.

如果您使用的是Mac,并且想构建到iOS,则需要在App Store上获取Xcode 。 当我运行tns doctor ,它说即使我安装了Xcode,也没有安装Xcode。 如果遇到这种情况,终端命令sudo xcode-select --reset为我解决了这个问题。

Here are the setup pages for each platform:

以下是每个平台的设置页面:

After completing those steps I had all the green ticks after running tns doctor. However, I still didn’t have any available Android emulators. I tried to run the suggested command to create an emulator:

完成这些步骤后,运行tns doctor之后,我得到了所有的绿色记号。 但是,我仍然没有任何可用的Android模拟器。 我试图运行建议的命令来创建模拟器:

avdmanager create avd -n test -k "system-images;android-28;google_apis;x86"

This is because my ANDROID_HOME variable was not set correctly.

这是因为我的ANDROID_HOME变量设置不正确。

On Windows, you can do this without the Command Prompt. Open the Start Menu and type ‘environment’. This should give you an option called ‘Edit the system environment variables’. This will open the System Properties, at the bottom of which you’ll find a button that says Environment Variables. In the System variables list, ANDROID_HOME should be set to C:\Android\android-sdk. My JAVA_HOME was set to C:\Program Files\Java\jdk1.8.0_211, though the exact version you have installed there could be different.

在Windows上,无需命令提示符即可执行此操作。 打开“开始”菜单,然后键入“环境”。 这应该为您提供一个名为“编辑系统环境变量”的选项。 这将打开“系统属性”,在该属性的底部,您会看到一个名为“环境变量”的按钮。 在系统变量列表中,ANDROID_HOME应该设置为C:\Android\android-sdk 。 我的JAVA_HOME设置为C:\Program Files\Java\jdk1.8.0_211 ,尽管在那里安装的确切版本可能有所不同。

Assuming you installed JDK 8 on your Mac, you can set this JAVA_HOME variable with export JAVA_HOME=$(/usr/libexec/java_home -v 1.8).

假设您在Mac上安装了JDK 8,则可以使用export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)设置此JAVA_HOME变量。

To see where your ANDROID_HOME is set to on MacOS, use the command echo $ANDROID_HOME. If it returns a path, go there in Finder and see if the SDK is actually there. If it returns nothing, you have set it to the wrong place. Mine was set to the location /Users/$userid/Library/Android/sdk where $userid is the name of my home folder and the username I log into the Mac with. This works if you have installed the SDK with the SDK Manager that comes with Android Studio, but if you have used Homebrew you’ll find the SDK at /usr/local/share/android-sdk.

要查看在MacOS上将ANDROID_HOME设置为的位置,请使用命令echo $ANDROID_HOME 。 如果返回路径,请在Finder中转到那里,然后查看SDK是否确实存在。 如果没有返回任何内容,则说明您将其设置在错误的位置。 我的设置为/Users/$userid/Library/Android/sdk位置,其中$userid是我的主文件夹的名称和我登录Mac时使用的用户名。 如果您已将SDK与Android Studio随附的SDK Manager一起安装,则此方法有效,但是如果您使用Homebrew,则会在/usr/local/share/android-sdk找到该SDK。

To change it, simply paste the location you want after export ANDROID_HOME=, like this:

要更改它,只需在export ANDROID_HOME=之后粘贴所需的位置,如下所示:

export ANDROID_HOME=/usr/local/share/android-sdk

After setting these variables, I was able to run the command for installing the system image needed to create an emulator on MacOS:

设置完这些变量后,我能够运行命令以安装在MacOS上创建模拟器所需的系统映像:

$ANDROID_HOME/tools/bin/sdkmanager "tools" "emulator" "platform-tools" "platforms;android-28" "build-tools;28.0.3" "extras;android;m2repository" "extras;google;m2repository"

On Windows this command is the same, except using %ANDROID_HOME% instead of $ANDROID_HOME.

在Windows上,此命令是相同的,除了使用%ANDROID_HOME%代替$ANDROID_HOME

On MacOS, this may result in a warning that a file called repositories.cfg could not be loaded. If this is true, simply type cd and copy and paste the directory of the missing file, without the filename at the end. Once you’re in that directory, simply use the touch repositories.cfg to create the file. Now run the command to download the system image again, and hopefully it’ll work this time.

在MacOS上,这可能会导致警告,导致无法加载名为repositories.cfg的文件。 如果是这样,只需键入cd并复制并粘贴丢失文件的目录,文件名末尾即可。 进入该目录后,只需使用touch repositories.cfg创建文件。 现在,运行命令以再次下载系统映像,希望这次可以正常工作。

If you still can’t create an emulator, you may need to run this:

如果仍然无法创建仿真器,则可能需要运行以下命令:

android update sdk -u --filter platform-tools,android-28

After this you should be able to create the emulator using the command at the start of this section. Here it is again:

之后,您应该能够使用本节开头的命令创建仿真器。 这里又是:

avdmanager create avd -n test -k "system-images;android-28;google_apis;x86"

在SideKick中创建一个新项目 (Creating a new project in SideKick)

SideKick comes with a few templates that should help you to get started with your project. You can get started with an existing navigation structure like a tab bar, or start an empty project. You can use plain JavaScript or TypeScript, or any of the web frameworks that NativeScript supports.

SideKick附带了一些模板,这些模板应该可以帮助您开始进行项目。 您可以开始使用现有的导航结构(如标签栏),也可以启动一个空项目。 您可以使用纯JavaScript或TypeScript或NativeScript支持的任何Web框架。

Once your project is created, it’s probably a good idea to check you can build to virtual devices. Selecting the Devices tab, click the Virtual Device Launcher button in the centre of the screen. You can see what Android or iOS emulators are on your system, and there’ll be a yellow warning icon next to any that have problems. Clicking this warning icon will give you more information about why the device might not work, which will help you in fixing problems with your installation.

创建项目后,最好检查一下是否可以构建到虚拟设备。 选择设备选项卡,单击屏幕中心的虚拟设备启动器按钮。 您可以看到系统上有哪些Android或iOS模拟器,并且在有问题的旁边都会有一个黄色的警告图标。 单击此警告图标将为您提供有关设备可能无法运行的原因的更多信息,这将帮助您解决安装问题。

If you want to use Android emulators and none are available, this is sometimes fixed by logging out and logging back in.

如果要使用Android仿真器,但没有可用的仿真器,则有时可以通过注销并重新登录来解决。

Otherwise some of the steps in the previous section may help.

否则,上一节中的某些步骤可能会有所帮助。

Click on ‘Open in Editor’ at the top, and you’ll find that there is no text editor set for NativeScript. This will give you the option to browse and set the code editor that you prefer. The developers of NativeScript sometimes suggests Sublime Text as a possibility, but they usually suggest Visual Studio Code because NativeScript has an extension for this editor that allows you to build and run projects.

单击顶部的“在编辑器中打开”,您会发现没有为NativeScript设置文本编辑器。 这将为您提供浏览和设置所需代码编辑器的选项。 NativeScript的开发人员有时会建议使用Sublime Text ,但他们通常会建议使用Visual Studio Code,因为NativeScript对此编辑器进行了扩展,使您可以构建和运行项目。

See the instructions for installing the VS Code extension, as I won’t go into that process here.

请参阅有关安装VS Code扩展的说明 ,因为我在这里不再进行该过程。

下一步 (Next steps)

As I said at the beginning of my post, there are a lot of ways to learn NativeScript out there. Perhaps the best way to see complete apps and dive into their code is the code samples page on their website. Here you’ll find apps, many of which are released on the Google Play and the iOS App Store, along with GitHub links to their code. The app templates are useful too.

正如我在文章开头所说的那样,有很多方法可以学习NativeScript。 查看完整应用程序并深入研究其代码的最佳方法也许是其网站上的代码示例页面 。 在这里,您会找到许多应用程序,其中许多是在Google Play和iOS App Store上发布的,还有指向其代码的GitHub链接。 应用程序模板也很有用。

I hope this tutorial has made it easier to get started with this exciting platform.

我希望本教程使您能够更轻松地开始使用这个令人兴奋的平台。

翻译自: https://medium.com/swlh/get-started-making-nativescript-ios-android-apps-with-one-code-base-a4718b3d0895

ios静态库和代码同名

 类似资料: