您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

创建 iOS 应用Create an iOS app

备注

Visual Studio App Center 投入新和集成服务移动应用开发的核心。Visual Studio App Center is investing in new and integrated services central to mobile app development. 开发人员可以使用构建测试分发服务来设置持续集成和交付管道。Developers can use Build, Test and Distribute services to set up Continuous Integration and Delivery pipeline. 应用程序部署后,开发人员可以监视状态和其应用程序使用的使用情况Analytics诊断服务,并与用户使用推送服务。Once the app is deployed, developers can monitor the status and usage of their app using the Analytics and Diagnostics services, and engage with users using the Push service. 开发人员还可以利用身份验证其用户进行身份验证并数据服务以持久保存并在云中的应用程序数据同步。Developers can also leverage Auth to authenticate their users and Data service to persist and sync app data in the cloud. 请查看App Center今天。Check out App Center today.

概述Overview

本教程说明如何将云后端服务 Azure 应用服务移动应用添加到 iOS 应用。This tutorial shows how to add Azure App Service Mobile Apps, a cloud backend service, to an iOS app. 第一步是在 Azure 上创建一个新的移动后端。The first step is to create a new mobile backend on Azure. 然后,下载一个简单的“待办事项列表” iOS 示例应用以在 Azure 中存储数据。Then, download a simple Todo list iOS sample app that stores data in Azure.

若要完成本教程,需要一台 Mac 和 一个 Azure 帐户To complete this tutorial, you need a Mac and an Azure account

创建新的 Azure 移动应用后端Create a new Azure mobile app backend

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 单击“创建资源” 。Click Create a resource.

  3. 在搜索框中,键入Web 应用In the search box, type Web App.

  4. 在结果列表中,选择Web 应用从 Marketplace。In the results list, select Web App from the Marketplace.

  5. 选择你订阅资源组(选择现有资源组_或_创建新组 (作为您的应用程序使用相同的名称))。Select your Subscription and Resource Group (select an existing resource group or create a new one (using the same name as your app)).

  6. 选择一个唯一名称的 web 应用。Choose a unique Name of your web app.

  7. 选择默认发布选项作为代码Choose the default Publish option as Code.

  8. 在中运行时堆栈,你需要选择下的一个版本ASP.NET节点In the Runtime stack, you need to select a version under ASP.NET or Node. 如果要构建一个.NET 后端,选择在 ASP.NET 下的一个版本。If you are building a .NET backend, select a version under ASP.NET. 否则如果你面向的基于节点应用程序,从选择一个版本的节点。Otherwise if you are targeting a Node based application, select one of the version from Node.

  9. 选择正确操作系统,Linux 或 Windows。Select the right Operating System, either Linux or Windows.

  10. 选择区域你想要部署此应用。Select the Region where you would like this app to be deployed.

  11. 选择适当应用服务计划并点击查看和创建Select the appropriate App Service Plan and hit Review and create.

  12. 在“资源组”下 ,选择现有资源组,_或_创建新组(使用与应用相同的名称)。Under Resource Group, select an existing resource group or create a new one (using the same name as your app).

  13. 单击创建Click Create. 稍等几分钟,等服务部署成功后再继续。Wait a few minutes for the service to be deployed successfully before proceeding. 查看门户标题中的通知(铃铛)图标以获取状态更新。Watch the Notifications (bell) icon in the portal header for status updates.

  14. 完成部署后,单击部署详细信息部分,然后单击资源的类型上microsoft.web/sitesOnce the deployment is completed, click on the Deployment details section and then click on the Resource of Type Microsoft.Web/sites. 它会将您导航到刚刚创建应用服务 Web 应用。It will navigate you to the App Service Web App that you just created.

  15. 单击配置边栏选项卡下的设置并在应用程序设置,单击新建应用程序设置按钮。Click on the Configuration blade under Settings and in the Application settings, click on the New application setting button.

  16. 在中添加/编辑应用程序设置页上,输入名称作为MobileAppsManagement_EXTENSION_VERSION和值作为最新和点击确定。In the Add/Edit application setting page, enter Name as MobileAppsManagement_EXTENSION_VERSION and Value as latest and hit OK.

您将使用这个新创建的应用服务 Web 应用为移动应用。You are all set to use this newly created App Service Web app as a Mobile app.

创建数据库连接并配置客户端和服务器项目Create a database connection and configure the client and server project

  1. 下载以下平台的客户端 SDK 快速入门:Download the client SDK quickstarts for the following platforms:

    iOS (Objective-C)iOS (Objective-C)
    iOS (Swift)iOS (Swift)
    Android (Java)Android (Java)
    Xamarin.iOSXamarin.iOS
    Xamarin.AndroidXamarin.Android
    Xamarin.FormsXamarin.Forms
    CordovaCordova
    Windows (C#)Windows (C#)

    备注

    如果使用 iOS 项目,则需从最新 GitHub 版本中下载“azuresdk-iOS-*.zip”。If you use the iOS project you need to download "azuresdk-iOS-*.zip" from latest GitHub release. MicrosoftAzureMobile.framework 文件解压缩并添加到项目的根目录中。Unzip and add the MicrosoftAzureMobile.framework file to the project's root.

  2. 需要添加数据库连接,或者连接到现有的连接。You will have to add a database connection or connect to an existing connection. 首先,确定是要创建数据存储,还是使用现有的数据存储。First, determine whether you’ll create a data store or use an existing one.

    • 创建新的数据存储:若要创建数据存储,请使用以下快速入门:Create a new data store: If you’re going to create a data store, use the following quickstart:

      快速入门:开始使用 Azure SQL 数据库中的单一数据库Quickstart: Getting started with single databases in Azure SQL Database

    • 现有数据源:若要使用现有的数据库连接,请按以下说明操作Existing data source: Follow the instructions below if you want to use an existing database connection

      1. SQL 数据库连接字符串格式 - Data Source=tcp:{your_SQLServer},{port};Initial Catalog={your_catalogue};User ID={your_username};Password={your_password}SQL Database Connection String format - Data Source=tcp:{your_SQLServer},{port};Initial Catalog={your_catalogue};User ID={your_username};Password={your_password}

        {your_SQLServer} :服务器的名称,此项可以在数据库的概览页中找到,通常采用“server_name.database.windows.net”格式。{your_SQLServer} Name of the server, this can be found in the overview page for your database and is usually in the form of “server_name.database.windows.net”. {port} :通常为 1433。{port} usually 1433. {your_catalogue} :数据库的名称。{your_catalogue} Name of the database. {your_username} :用于访问数据库的用户名。{your_username} User name to access your database. {your_password} :用于访问数据库的密码。{your_password} Password to access your database.

        详细了解 SQL 连接字符串格式Learn more about SQL Connection String format

      2. 移动应用添加连接字符串。可以在应用服务中管理应用程序的连接字符串,方法是使用菜单中的“配置”选项。 Add the connection string to your mobile app In App Service, you can manage connection strings for your application by using the Configuration option in the menu.

        若要添加连接字符串,请执行以下操作:To add a connection string:

        1. 单击“应用程序设置” 选项卡。Click on the Application settings tab.

        2. 单击“[+] 新建连接字符串”。 Click on [+] New connection string.

        3. 需为连接字符串提供“名称”、“值”和“类型”。 You will need to provide Name, Value and Type for your connection string.

        4. 键入 MS_TableConnectionString 作为“名称” Type Name as MS_TableConnectionString

        5. “值”应该是在前面的步骤中生成的连接字符串。Value should be the connecting string you formed in the step before.

        6. 如果向 SQL Azure 数据库添加连接字符串,请在“类型”下选择“SQLAzure”。 If you are adding a connection string to a SQL Azure database choose SQLAzure under type.

  3. Azure 移动应用有适用于 .NET 和 Node.js 后端的 SDK。Azure Mobile Apps has SDKs for .NET and Node.js backends.

    • Node.js 后端Node.js backend

      若要使用 Node.js 快速入门应用,请按以下说明操作。If you’re going to use Node.js quickstart app, follow the instructions below.

      1. 在 Azure 门户中转到“简易表”即可看到以下屏幕。 In the Azure portal, go to Easy Tables, you will see this screen.

        Node 简易表

      2. 确保已在“配置”选项卡中添加 SQL 连接字符串。 然后勾选“我已了解此操作会覆盖所有站点内容”框,并单击“创建 TodoItem 表”按钮 。Make sure the SQL connection string is already added in the Configuration tab. Then check the box of I acknowledge that this will overwrite all site contents and click the Create TodoItem table button.

        Node 简易表的“配置”

      3. 在“简易表”中单击“+ 添加”按钮。 In Easy Tables, click the + Add button.

        Node 简易表的“添加”按钮

      4. 创建允许匿名访问的 TodoItem 表。Create a TodoItem table with anonymous access.

        Node 简易表的“添加表”

    • .NET 后端.NET backend

      若要使用 .NET 快速入门应用,请按以下说明操作。If you’re going to use .NET quickstart app, follow the instructions below.

      1. azure-mobile-apps-quickstarts 存储库下载 Azure 移动应用 .NET 服务器项目。Download the Azure Mobile Apps .NET server project from the azure-mobile-apps-quickstarts repository.

      2. 在 Visual Studio 中以本地方式生成 .NET 服务器项目。Build the .NET server project locally in Visual Studio.

      3. 在 Visual Studio 中打开解决方案资源管理器,右键单击 ZUMOAPPNAMEService 项目,单击“发布”, 此时会出现“Publish to App Service”窗口。In Visual Studio, open Solution Explorer, right-click on ZUMOAPPNAMEService project, click Publish, you will see a Publish to App Service window. 如果在 Mac 上工作,请单击此处了解部署应用的其他方式。If you are working on Mac, check out other ways to deploy the app here.

        Visual Studio 发布

      4. 选择“应用服务” 作为发布目标,接着单击“选择现有”,然后单击窗口底部的“发布”按钮。 Select App Service as publish target, then click Select Existing, then click the Publish button at the bottom of the window.

      5. 首先需通过 Azure 订阅登录 Visual Studio。You will need to log into Visual Studio with your Azure subscription first. 选择 SubscriptionResource Group,然后选择应用的名称。Select the Subscription, Resource Group, and then select the name of your app. 准备就绪后,单击“确定”,这样就会将本地的 .NET 服务器项目部署到应用服务后端。 When you are ready, click OK, this will deploy the .NET server project that you have locally into the App Service backend. 部署完成后,系统会在浏览器中将你重定向到 http://{zumoappname}.azurewebsites.net/When deployment finishes, you will be redirected to http://{zumoappname}.azurewebsites.net/ in the browser.

运行 iOS 应用Run the iOS app

  1. 使用 Xcode 打开下载的客户端项目。Open the downloaded client project using Xcode.

  2. 转到 Azure 门户,并导航到已创建的移动应用。Go to the Azure portal and navigate to the mobile app that you created. Overview 边栏选项卡上,查找作为移动应用公共终结点的 URL。On the Overview blade, look for the URL which is the public endpoint for your mobile app. 示例 - 我的应用名称“test123”的站点名将为 https://test123.azurewebsites.netExample - the sitename for my app name "test123" will be https://test123.azurewebsites.net.

  3. 在 Swift 项目中,打开此文件夹中的文件 ToDoTableViewController.swift - ZUMOAPPNAME/ZUMOAPPNAME/ToDoTableViewController.swift。For Swift project, open the file ToDoTableViewController.swift in this folder - ZUMOAPPNAME/ZUMOAPPNAME/ToDoTableViewController.swift. 应用程序名称为 ZUMOAPPNAMEThe application name is ZUMOAPPNAME.

  4. viewDidLoad() 方法中,将 ZUMOAPPURL 参数替换为上面的公共终结点。In viewDidLoad() method, replace ZUMOAPPURL parameter with public endpoint above.

    let client = MSClient(applicationURLString: "ZUMOAPPURL")

    变为becomes

    let client = MSClient(applicationURLString: "https://test123.azurewebsites.net")

  5. 在 Objective-C 项目中,打开此文件夹(ZUMOAPPNAME/ZUMOAPPNAME)中的文件 QSTodoService.mFor Objective-C project, open the file QSTodoService.m in this folder - ZUMOAPPNAME/ZUMOAPPNAME. 应用程序名称为 ZUMOAPPNAMEThe application name is ZUMOAPPNAME.

  6. init 方法中,将 ZUMOAPPURL 参数替换为上面的公共终结点。In init method, replace ZUMOAPPURL parameter with public endpoint above.

    self.client = [MSClient clientWithApplicationURLString:@"ZUMOAPPURL"];

    变为becomes

    self.client = [MSClient clientWithApplicationURLString:@"https://test123.azurewebsites.net"];

  7. 按“运行” 按钮生成项目,并在 iOS 模拟器中启动应用。Press the Run button to build the project and start the app in the iOS simulator.

  8. 在应用中, 单击加号 (+) 图标,键入有意义的文本(例如 Complete the tutorial) ,然后单击“保存”按钮。In the app, click the plus (+) icon, type meaningful text, such as Complete the tutorial, and then click the save button. 这会将一个 POST 请求发送到之前部署的 Azure 后端。This sends a POST request to the Azure backend you deployed earlier. 后端将请求中的数据插入到 TodoItem SQL 表中,并将有关新存储的项的信息返回给移动应用。The backend inserts data from the request into the TodoItem SQL table, and returns information about the newly stored items back to the mobile app. 移动应用会在列表中显示此数据。The mobile app displays this data in the list.

    在 iOS 上运行的快速入门应用