-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappDataRequester.swift
67 lines (53 loc) · 2.1 KB
/
appDataRequester.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// appDataRequester.swift
// Conference App
//
// Created by George Gruse on 4/15/16.
// Copyright © 2016 Chrystech Systems. All rights reserved.
//
import Foundation
class appDataRequester {
var path: NSString
var mainFile: String
//Make sure that this url is the main json request
var dataPath: NSURL = NSURL(string: "http://djmobilesoftware.com/jsondata.json")!
init()
{
let dir:NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first!
mainFile = "jsonData.dat"
path = dir.stringByAppendingPathComponent(mainFile);
}
func initData()
{
let data:NSData = getDataFromURL(dataPath)!
data.writeToFile(path.stringByAppendingPathExtension(mainFile)!, atomically: true)
}
func getDataFromFile()-> NSDictionary
{
return NSDictionary.init(contentsOfFile:(path.stringByAppendingPathExtension(mainFile)!))!
}
func getDataFromURL(requestURL: NSURL) -> NSData?{
var locked = true // Flag to make sure the
var returnData:NSData?
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
if let httpResponse = response as? NSHTTPURLResponse {
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
returnData = data!
}else{
print("Error while retriving data!")
}
locked = false
}
}
task.resume()
while(locked){ // Runs untill the response is received
}
return returnData
}
//Credit to http://stackoverflow.com/questions/24097826/read-and-write-data-from-text-file by user Adam on the stack
//overflow site
}