Language - Swift iOS Xcode: Difference between revisions

From Phidgets Support
No edit summary
(Redirected page to Language - Swift)
 
Line 1: Line 1:
[[Category:Language]]{{NoTitle}}
#REDIRECT [[Language - Swift]]
{|
|style="vertical-align:middle; width: 60%;"|
<font size=6>'''Language - Swift'''
 
'''iOS with Swift'''</font>
 
Welcome to using Phidgets with Swift! By using Swift, you will have access to the complete Phidget22 API, including events.
 
Xcode is an integrated development environment for macOS. It is commonly used as a tool for developing software for macOS and iOS applications.
|{{TOC limit|2}}
|}
{{Language_-_Swift_Intro|macOS|Xcode}}
 
==Use Our Examples==
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples for iOS you will need to download [https://developer.apple.com/xcode/ Xcode] from the Mac App Store.
 
 
You will also need to install [https://cocoapods.org/ CocoaPods] in order to access the Phidget libraries for Swift. You can do this by opening the terminal and entering the following command:
<syntaxhighlight lang='bash'>
sudo gem install cocoapods
</syntaxhighlight>
[[Image:Swift_cocoapods_install.png ‎|link=|center]]
 
 
Now that you have Xcode and CocoaPods installed, download a Swift example that will work with your Phidget:
*{{SampleCode|Swift|Swift Examples}}
 
 
After opening the example, you will notice that there is a file called ''Podfile''
[[Image:Swift_example_folder.png|link=|center]]
 
 
If you open this file, you can see that there is a reference to the ''Phidget22Swift'' pod. Note that no version number is included, so the newest available version of the Phidget22Swift pod will be installed:
[[Image:Swift_podfile.png|link=|center]]
 
 
To install the Phidget libraries, open a terminal at the example location and enter the following command:
<syntaxhighlight lang='bash'>
pod install
</syntaxhighlight>
[[Image:Swift_pod_install.png|link=|center]]
 
 
After the libraries are installed, open the generated ''.xcworkspace'' file:
[[Image:Swift_open_workspace.png|link=|center]]
 
 
Next simply select the type of device you would like the application to run on and press play:
[[Image:Swift_select_target.png|link=|center]]
 
 
Here is an example output:
[[Image:Swift_phone_example.png|link=|center]]
 
{{Edit_the_Examples}}
 
==Write Code==
{{Language_-_Swift_Write_Code}}
 
==Setting up a New Project==
Whether you are  building a project from scratch, or adding Phidget functionality to an existing project, you will need to configure your development environment to properly link the Phidget libraries. To begin:
 
Create a new Xcode project:
[[Image:Cocoa_CreateProject.png |link=|center]]
 
 
Next, select an iOS application. For this tutorial's purposes, we will use a Single View Application:
[[Image:iOS_SingleView.png |link=|center]]
 
 
Name the project, select Swift as the language, and choose which devices will be supported:
[[Image:iOS_NameProject_Swift.png|link=|center]]
 
 
Now that your project is created, you need to add the Phidget libraries. In order to do this, you must have CocoaPods installed on your computer (this is covered in detail above in the [[#Use Our Examples |use our examples]] section). Open a terminal at the example location and enter the following command:
<syntaxhighlight lang='bash'>
pod init
</syntaxhighlight>
[[Image:Swift_pod_init.png|link=|center]]
 
 
This will create a new Podfile. Open the Podfile in your favorite text editor and add a reference to the ''Phidget22Swift'' pod:
[[Image:Swift_folder_podfile.png|link=|center]]
 
 
[[Image:Swift_podfile_edit.png|link=|center]]
 
 
Save your edit to the Podfile, and then enter the following command in the terminal which was opened at the example location:
<syntaxhighlight lang='bash'>
pod install
</syntaxhighlight>
[[Image:Swift_pod_install_example.png|link=|center]]
 
 
After running the command, open the ''xcworkspace'' file and access the Phidget libraries by adding the following line to the top of your files:
<syntaxhighlight lang="swift">
import Phidget22Swift
</syntaxhighlight>
 
 
Success! The project now has access to Phidgets and we are ready to begin coding.
 
==Further Reading==
{{Language_-_Swift_Further_Reading}}

Latest revision as of 16:46, 15 March 2021

Redirect to: