-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOSUtils.swift
83 lines (63 loc) · 2.18 KB
/
IOSUtils.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
//
// IOSUtils.swift
//
//
// Created by Diogo on 04/05/2016.
// Copyright © 2016 Merino. All rights reserved.
//
import Foundation
class IOSUtils {
func getIp() -> String? {
let host = CFHostCreateWithName(nil,"www.google.com").takeRetainedValue()
CFHostStartInfoResolution(host, .Addresses, nil)
var success: DarwinBoolean = false
if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray?,
let theAddress = addresses.firstObject as? NSData {
var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
if getnameinfo(UnsafePointer(theAddress.bytes), socklen_t(theAddress.length),
&hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
if let numAddress = String.fromCString(hostname) {
return numAddress
}
}
return ""
}else{
return ""
}
}
/**
App version.
- Returns: "1.0"
*/
func appVersion() -> String {
let dictionary = Bundle.main.infoDictionary!
let version = dictionary["CFBundleShortVersionString"] as! String
_ = dictionary["CFBundleVersion"] as! String
return version
}
/**
Get formated current date.
- Returns: "EEE, dd MMM yyyy HH:mm"
*/
func formatCurrentDate() -> String {
let dayTimePeriodFormatter = DateFormatter()
dayTimePeriodFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm"
let dateString = dayTimePeriodFormatter.string(from: Date())
return dateString
}
/**
Simple time ago.
- Parameter dateLong: date time.
- Returns: "Today", "Yesterday" or "x days ago"
*/
func formatDateTimeAgo(dateLong: Double) -> String {
let days = (Constants.Time.currentTimeNow() - dateLong) / 86400000;
if days == 0 {
return "Today";
}else if days == 1 {
return "Yesterday"
}else {
return "\(Int(days)) days ago";
}
}
}