From accb01a94062a1b78598889a1bb2c4b8583c5278 Mon Sep 17 00:00:00 2001 From: Wessel Stoop Date: Thu, 30 Apr 2015 13:56:25 +0200 Subject: [PATCH 1/2] Created an abstraction for the server --- .../Vowel Space Travel iOS/VSTServer.swift | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/VSTServer.swift diff --git a/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/VSTServer.swift b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/VSTServer.swift new file mode 100644 index 0000000..80ca046 --- /dev/null +++ b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/VSTServer.swift @@ -0,0 +1,126 @@ +// +// VSTServer.swift +// Vowel Space Travel iOS +// +// Created by Wessel Stoop on 23/04/15. +// Copyright (c) 2015 Radboud University. All rights reserved. +// + +import Foundation + +enum LoginResult +{ + case Successful + case UserDoesNotExist + case IncorrectPassword +} + +class VSTServer : NSObject +{ + var url : String + + var userName : String? + var userLoggedInSuccesfully : Bool = false + + var latestResponse : NSArray? + + init(url: String) + { + self.url = url + } + + func HTTPGetToJSON(urlExtension: String, completionHandler: ((NSDictionary?, NSError?) -> Void)) + { + var jsonData : NSDictionary? + + var request : NSURLRequest = NSURLRequest(URL: NSURL(string: self.url+urlExtension)!) + + println(request.URL.standardizedURL) + println(request.HTTPBody) + + NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, responseData: NSData!,error:NSError!) -> Void in + + if error == nil + { + jsonData = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers, error:nil) as NSDictionary? + + completionHandler(jsonData,error); + + } + else + { + println("Error: \(error.localizedDescription)") + } + + }) + + } + + func HTTPPostToJSON(urlExtension: String, completionHandler: ((NSDictionary?, NSError?) -> Void)) + { + var jsonData : NSDictionary? + + var request : NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: self.url+urlExtension)!) + + println(self.url+urlExtension) + + var jsonString : String = "{ \"firstName\" : \"Wessel\", \"lastName\" : \"Stoop\" }" + request.HTTPBody = jsonString.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion:true) + request.HTTPMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + + NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, responseData: NSData!,error:NSError!) -> Void in + + if error == nil + { + jsonData = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers, error:nil) as NSDictionary? + + completionHandler(jsonData,error); + + } + else + { + println("Error: \(error.localizedDescription)") + } + + }) + + } + + func tryLoggingIn(userName : String, password: String) -> LoginResult + { + self.userName = userName + self.userLoggedInSuccesfully = true + + return LoginResult.Successful + } + + func createNewUser(userName : String) + { + self.HTTPPostToJSON("players") + { + (jsonData,err) -> Void in + } + } + + func getAllVowels() + { + assert(self.userLoggedInSuccesfully, "You have to be logged in to do this") + + var urlExtensionToGetVowels : String = "vowels?page=0&size=30" + self.HTTPGetToJSON(urlExtensionToGetVowels) + { + (jsonData,err) -> Void in + + var unpackagedJsonData : NSDictionary = jsonData!["_embedded"] as NSDictionary + + for vowel in unpackagedJsonData["vowels"] as NSArray + { + + println(vowel["ipa"]!) + } + + } + } + +} \ No newline at end of file From 79ef989ace299e445c7ed1307dbf32c6fabf2cf3 Mon Sep 17 00:00:00 2001 From: Wessel Stoop Date: Thu, 30 Apr 2015 13:56:46 +0200 Subject: [PATCH 2/2] Tested the abstraction --- .../Vowel Space Travel iOS.xcodeproj/project.pbxproj | 12 ++++++++++++ .../Vowel Space Travel iOS/Constants.swift | 4 ++-- .../Vowel Space Travel iOS/LoginViewController.swift | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS.xcodeproj/project.pbxproj b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS.xcodeproj/project.pbxproj index f19f87c..ab34950 100644 --- a/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS.xcodeproj/project.pbxproj +++ b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 495359281AE91A88009E870A /* VSTServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495359271AE91A88009E870A /* VSTServer.swift */; }; 4996A49A1AD7B4D0000437CF /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4996A4991AD7B4D0000437CF /* LoginViewController.swift */; }; 4996A49C1AD7BA43000437CF /* ProgressViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4996A49B1AD7BA43000437CF /* ProgressViewController.swift */; }; 4996A49E1AD7C9CE000437CF /* TempObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4996A49D1AD7C9CE000437CF /* TempObjects.swift */; }; @@ -38,6 +39,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 495359271AE91A88009E870A /* VSTServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VSTServer.swift; sourceTree = ""; }; 4996A4991AD7B4D0000437CF /* LoginViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = ""; }; 4996A49B1AD7BA43000437CF /* ProgressViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProgressViewController.swift; sourceTree = ""; }; 4996A49D1AD7C9CE000437CF /* TempObjects.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TempObjects.swift; sourceTree = ""; }; @@ -79,6 +81,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 495359291AE91AA1009E870A /* Models */ = { + isa = PBXGroup; + children = ( + 495359271AE91A88009E870A /* VSTServer.swift */, + ); + name = Models; + sourceTree = ""; + }; 4996A4A11AD7E4B1000437CF /* Resources */ = { isa = PBXGroup; children = ( @@ -110,6 +120,7 @@ children = ( 4996A49F1AD7E480000437CF /* Constants.swift */, 49C653DF1ACEA49E00CA2803 /* ViewControllers */, + 495359291AE91AA1009E870A /* Models */, 4996A49D1AD7C9CE000437CF /* TempObjects.swift */, 4996A4A21AD7F829000437CF /* PassControlBackAndForth.swift */, 4996A4A11AD7E4B1000437CF /* Resources */, @@ -274,6 +285,7 @@ 49CD8F0D1ACED40500DD3A5F /* DownloadViewController.swift in Sources */, 49C653B91ACE9CB400CA2803 /* AppDelegate.swift in Sources */, 49AEA1401ADFC291001A82A1 /* InfoViewController.swift in Sources */, + 495359281AE91A88009E870A /* VSTServer.swift in Sources */, 4996A4A01AD7E480000437CF /* Constants.swift in Sources */, 49F1F5C61AD8097000457411 /* ResultViewController.swift in Sources */, 49AEA13E1ADFBEF5001A82A1 /* SettingsViewController.swift in Sources */, diff --git a/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/Constants.swift b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/Constants.swift index 34dbcac..430f78c 100644 --- a/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/Constants.swift +++ b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/Constants.swift @@ -9,6 +9,6 @@ import Foundation let kDevelopmentMode = true -let kSpeedUpDownload = kDevelopmentMode ? false : false +let kSpeedUpDownload = kDevelopmentMode ? true : false -let kWebserviceURL = kDevelopmentMode ? "http://localhost:8080" : "" \ No newline at end of file +let kWebserviceURL = kDevelopmentMode ? "http://applejack.science.ru.nl:8080/" : "" \ No newline at end of file diff --git a/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/LoginViewController.swift b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/LoginViewController.swift index 501ff5f..13f3d3b 100644 --- a/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/LoginViewController.swift +++ b/mobile/Vowel Space Travel iOS/Vowel Space Travel iOS/LoginViewController.swift @@ -62,6 +62,14 @@ class LoginViewController: UIViewController { createAccountLink.text = "Create account" self.view.addSubview(createAccountLink) + //Create the VSTServer object + var vstServer : VSTServer = VSTServer(url: kWebserviceURL) + + vstServer.tryLoggingIn("wessel", password: "hunter2") + + vstServer.getAllVowels() + vstServer.createNewUser("Wessel") + } override func didReceiveMemoryWarning()