Add iOS example

This commit is contained in:
Dan Shevlyuk 2016-11-16 15:55:29 +03:00
parent bb73e9a877
commit 752c9d8d5d
7 changed files with 429 additions and 1 deletions

View File

@ -16,6 +16,11 @@
3723139A1DCF84EC0042DA87 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 372313981DCF84EC0042DA87 /* Result.swift */; };
3723139B1DCF84EC0042DA87 /* Socket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 372313991DCF84EC0042DA87 /* Socket.swift */; };
3723139C1DCF85270042DA87 /* SwiftSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 3723138D1DCF84CA0042DA87 /* SwiftSocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
375C48301DDC4C56008C701D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375C482F1DDC4C56008C701D /* AppDelegate.swift */; };
375C48321DDC4C56008C701D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 375C48311DDC4C56008C701D /* ViewController.swift */; };
375C48351DDC4C56008C701D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 375C48331DDC4C56008C701D /* Main.storyboard */; };
375C48371DDC4C56008C701D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 375C48361DDC4C56008C701D /* Assets.xcassets */; };
375C483A1DDC4C56008C701D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 375C48381DDC4C56008C701D /* LaunchScreen.storyboard */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -27,11 +32,25 @@
372313951DCF84E30042DA87 /* ytcpsocket.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = ytcpsocket.c; path = Sources/ytcpsocket.c; sourceTree = SOURCE_ROOT; };
372313981DCF84EC0042DA87 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Result.swift; path = Sources/Result.swift; sourceTree = SOURCE_ROOT; };
372313991DCF84EC0042DA87 /* Socket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Socket.swift; path = Sources/Socket.swift; sourceTree = SOURCE_ROOT; };
375C482D1DDC4C56008C701D /* iOS Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "iOS Example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
375C482F1DDC4C56008C701D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
375C48311DDC4C56008C701D /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
375C48341DDC4C56008C701D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
375C48361DDC4C56008C701D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
375C48391DDC4C56008C701D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
375C483B1DDC4C56008C701D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
377DAA691DCDE40200009697 /* SwiftSocket.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftSocket.framework; sourceTree = BUILT_PRODUCTS_DIR; };
377DAA761DCDE45D00009697 /* SwiftSocket.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftSocket.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
375C482A1DDC4C56008C701D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
377DAA651DCDE40200009697 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
@ -67,6 +86,19 @@
name = UDPSocket;
sourceTree = "<group>";
};
375C482E1DDC4C56008C701D /* iOS Example */ = {
isa = PBXGroup;
children = (
375C482F1DDC4C56008C701D /* AppDelegate.swift */,
375C48311DDC4C56008C701D /* ViewController.swift */,
375C48331DDC4C56008C701D /* Main.storyboard */,
375C48361DDC4C56008C701D /* Assets.xcassets */,
375C48381DDC4C56008C701D /* LaunchScreen.storyboard */,
375C483B1DDC4C56008C701D /* Info.plist */,
);
path = "iOS Example";
sourceTree = "<group>";
};
377DAA6A1DCDE40200009697 /* Supporting Files */ = {
isa = PBXGroup;
children = (
@ -81,6 +113,7 @@
isa = PBXGroup;
children = (
5518C83119A329100049DC22 /* Source */,
375C482E1DDC4C56008C701D /* iOS Example */,
5518C83019A329100049DC22 /* Products */,
);
sourceTree = "<group>";
@ -90,6 +123,7 @@
children = (
377DAA691DCDE40200009697 /* SwiftSocket.framework */,
377DAA761DCDE45D00009697 /* SwiftSocket.framework */,
375C482D1DDC4C56008C701D /* iOS Example.app */,
);
name = Products;
sourceTree = "<group>";
@ -129,6 +163,23 @@
/* End PBXHeadersBuildPhase section */
/* Begin PBXNativeTarget section */
375C482C1DDC4C56008C701D /* iOS Example */ = {
isa = PBXNativeTarget;
buildConfigurationList = 375C483E1DDC4C56008C701D /* Build configuration list for PBXNativeTarget "iOS Example" */;
buildPhases = (
375C48291DDC4C56008C701D /* Sources */,
375C482A1DDC4C56008C701D /* Frameworks */,
375C482B1DDC4C56008C701D /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = "iOS Example";
productName = "iOS Example";
productReference = 375C482D1DDC4C56008C701D /* iOS Example.app */;
productType = "com.apple.product-type.application";
};
377DAA681DCDE40200009697 /* SwiftSocket iOS */ = {
isa = PBXNativeTarget;
buildConfigurationList = 377DAA6E1DCDE40200009697 /* Build configuration list for PBXNativeTarget "SwiftSocket iOS" */;
@ -172,10 +223,14 @@
isa = PBXProject;
attributes = {
LastSwiftMigration = 0710;
LastSwiftUpdateCheck = 0710;
LastSwiftUpdateCheck = 0810;
LastUpgradeCheck = 0710;
ORGANIZATIONNAME = swift;
TargetAttributes = {
375C482C1DDC4C56008C701D = {
CreatedOnToolsVersion = 8.1;
ProvisioningStyle = Automatic;
};
377DAA681DCDE40200009697 = {
CreatedOnToolsVersion = 8.1;
LastSwiftMigration = 0810;
@ -193,6 +248,7 @@
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 5518C82619A3290F0049DC22;
productRefGroup = 5518C83019A329100049DC22 /* Products */;
@ -201,11 +257,22 @@
targets = (
377DAA681DCDE40200009697 /* SwiftSocket iOS */,
377DAA751DCDE45D00009697 /* SwiftSocket macOS */,
375C482C1DDC4C56008C701D /* iOS Example */,
);
};
/* End PBXProject section */
/* Begin PBXResourcesBuildPhase section */
375C482B1DDC4C56008C701D /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
375C483A1DDC4C56008C701D /* LaunchScreen.storyboard in Resources */,
375C48371DDC4C56008C701D /* Assets.xcassets in Resources */,
375C48351DDC4C56008C701D /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
377DAA671DCDE40200009697 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
@ -224,6 +291,15 @@
/* End PBXResourcesBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
375C48291DDC4C56008C701D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
375C48321DDC4C56008C701D /* ViewController.swift in Sources */,
375C48301DDC4C56008C701D /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
377DAA641DCDE40200009697 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
@ -246,7 +322,73 @@
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXVariantGroup section */
375C48331DDC4C56008C701D /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
375C48341DDC4C56008C701D /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
};
375C48381DDC4C56008C701D /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
375C48391DDC4C56008C701D /* Base */,
);
name = LaunchScreen.storyboard;
sourceTree = "<group>";
};
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */
375C483C1DDC4C56008C701D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEBUG_INFORMATION_FORMAT = dwarf;
GCC_NO_COMMON_BLOCKS = YES;
INFOPLIST_FILE = "iOS Example/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 10.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.danshevluk.iOS-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
375C483D1DDC4C56008C701D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ANALYZER_NONNULL = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
GCC_NO_COMMON_BLOCKS = YES;
INFOPLIST_FILE = "iOS Example/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 10.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.danshevluk.iOS-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
name = Release;
};
377DAA6F1DCDE40200009697 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
@ -465,6 +607,14 @@
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
375C483E1DDC4C56008C701D /* Build configuration list for PBXNativeTarget "iOS Example" */ = {
isa = XCConfigurationList;
buildConfigurations = (
375C483C1DDC4C56008C701D /* Debug */,
375C483D1DDC4C56008C701D /* Release */,
);
defaultConfigurationIsVisible = 0;
};
377DAA6E1DCDE40200009697 /* Build configuration list for PBXNativeTarget "SwiftSocket iOS" */ = {
isa = XCConfigurationList;
buildConfigurations = (

View File

@ -0,0 +1,22 @@
//
// AppDelegate.swift
// iOS Example
//
// Created by Dan Shevlyuk on 11/16/16.
// Copyright © 2016 swift. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}
}

View File

@ -0,0 +1,68 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2657" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="iOS_Example" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="0aA-sn-7UO">
<rect key="frame" x="32" y="36" width="311" height="615"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="JFV-o8-msm">
<rect key="frame" x="0.0" y="0.0" width="311" height="527"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="1Zb-7f-RAm">
<rect key="frame" x="0.0" y="527" width="311" height="88"/>
<state key="normal" title="Send data"/>
<connections>
<action selector="sendButtonAction" destination="BYZ-38-t0r" eventType="touchUpInside" id="dre-Ph-16U"/>
</connections>
</button>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="0aA-sn-7UO" secondAttribute="bottom" constant="16" id="OXz-iC-fBJ"/>
<constraint firstItem="0aA-sn-7UO" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="16" id="VPh-vK-byV"/>
<constraint firstItem="0aA-sn-7UO" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="16" id="XEK-kb-coH"/>
<constraint firstAttribute="trailingMargin" secondItem="0aA-sn-7UO" secondAttribute="trailing" constant="16" id="Xn5-5H-Q0z"/>
</constraints>
</view>
<connections>
<outlet property="textView" destination="JFV-o8-msm" id="3lP-Nj-elk"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="136.80000000000001" y="132.68365817091455"/>
</scene>
</scenes>
</document>

45
iOS Example/Info.plist Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>

View File

@ -0,0 +1,55 @@
import UIKit
import SwiftSocket
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
let host = "apple.com"
let port = 80
var client: TCPClient?
override func viewDidLoad() {
super.viewDidLoad()
client = TCPClient(address: host, port: Int32(port))
}
@IBAction func sendButtonAction() {
guard let client = client else { return }
switch client.connect(timeout: 10) {
case .success:
appendToTextField(string: "Connected to host \(client.address)")
if let response = sendRequest(string: "GET / HTTP/1.0\n\n", using: client) {
appendToTextField(string: "Response: \(response)")
}
case .failure(let error):
appendToTextField(string: String(describing: error))
}
}
private func sendRequest(string: String, using client: TCPClient) -> String? {
appendToTextField(string: "Sending data ... ")
switch client.send(string: string) {
case .success:
return readResponse(from: client)
case .failure(let error):
appendToTextField(string: String(describing: error))
return nil
}
}
private func readResponse(from client: TCPClient) -> String? {
guard let response = client.read(1024*10) else { return nil }
return String(bytes: response, encoding: .utf8)
}
private func appendToTextField(string: String) {
print(string)
textView.text = textView.text.appending("\n\(string)")
}
}