-
Notifications
You must be signed in to change notification settings - Fork 1
/
MDBSwiftParseUtils.swift
executable file
·160 lines (139 loc) · 5.74 KB
/
MDBSwiftParseUtils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// MDBSwiftParseUtils.swift
//
// Created by Akkshay Khoslaa on 4/25/16.
// Copyright © 2016 Mobile Developers of Berkeley. All rights reserved.
//
import Foundation
import UIKit
import Parse
import CoreLocation
public class MDBSwiftParseUtils {
/**
Sets the image of an imageview asynchronously.
- parameters:
- file: PFFile containing image
- imageView: UIImageView that you want to set the image of
*/
static func setImageViewImageFromFile(file: PFFile, imageView: UIImageView) {
file.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
imageView.image = image
}
}
}
}
/**
Sets the image of a UIButton asynchronously.
- parameters:
- file: PFFile containing image
- button: UIButton that you want to set the image of
*/
static func setButtonImageFromFile(file: PFFile, button: UIButton) {
file.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
button.setImage(image, forState: .Normal)
}
}
}
}
/**
Sets the image of an imageview asynchronously using a pointer that is passed in.
- parameters:
- pointer: PFObject that needs to be fetched that contains the PFFile
- imageView: UIImageView that you want to set the image of
*/
static func setImageViewImageFromPointer(pointer: PFObject, imageView: UIImageView) {
pointer.fetchIfNeededInBackgroundWithBlock {
(imageObject: PFObject?, error: NSError?) -> Void in
let headerImageFile = imageObject!["img"] as! PFFile
headerImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
imageView.image = image
}
}
}
}
}
/**
Sets the image of an imageview asynchronously using a pointer that is passed in.
- parameters:
- pointer: PFObject that needs to be fetched that contains the PFFile
- imageView: UIImageView that you want to set the image of
- imageFieldName: Name of the field containing the PFFile
*/
static func setImageViewImageFromPointer(pointer: PFObject, imageFieldName: String, imageView: UIImageView) {
pointer.fetchIfNeededInBackgroundWithBlock {
(imageObject: PFObject?, error: NSError?) -> Void in
let headerImageFile = imageObject!["img"] as! PFFile
headerImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data: imageData)
imageView.image = image
}
}
}
}
}
/**
Sets the image of an imageview asynchronously using a pointer that is passed in.
- parameters:
- pointer: PFObject that needs to be fetched that contains the PFFile
- imageView: UIImageView that you want to set the image of
- imageFieldName: Name of the field containing the PFFile
*/
static func setButtonImageFromPointer(pointer: PFObject, imageFieldName: String, button: UIButton) {
pointer.fetchIfNeededInBackgroundWithBlock {
(imageObject: PFObject?, error: NSError?) -> Void in
let headerImageFile = imageObject!["img"] as! PFFile
headerImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data: imageData)
button.setImage(image, forState: .Normal)
}
}
}
}
}
/**
Returns the distance between 2 locations as a formatted string.
- returns:
distance between 2 locations as a String
- parameters:
- firstLocation: first location geopoint
- secondLocation: second location geopoint
*/
static func getDistanceString(firstLocation: PFGeoPoint, secondLocation: PFGeoPoint) -> String {
return "\(Double(round(10*firstLocation.distanceInMilesTo(secondLocation))/10)) mi"
}
/**
Check if location services are authorized, and if they are get the current location.
Make sure you call MDBSwiftUtils.startLocationServices() first.
- returns:
the current location as PFGeoPoint if location services are authorized; otherwise returns nil
- parameters:
- locationManager: CLLocationManager instance being used in your VC
*/
static func getCurrentLocationGeoPoint(locationManager: CLLocationManager) -> PFGeoPoint? {
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse
|| CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways) {
let currentLocation = locationManager.location
return PFGeoPoint(location: currentLocation)
} else {
return nil
}
}
}