-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalStaticMethods.swift
1363 lines (938 loc) · 37 KB
/
GlobalStaticMethods.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// GlobalStaticMethods.swift
// Skeleton
//
// Created by Traffic MacBook Pro on 5/24/16.
// Copyright © 2016 My Macbook Pro. All rights reserved.
//
import UIKit
import Foundation
import SystemConfiguration
class DeviceUtil {
/* ======================================================================================
Enum for Device
========================================================================================= */
enum Device: Int {
case iPhone4 ,
iPhone5,
iPhone6 ,
iPhone6Plus ,
iPadMini,
iPad,
iPadPro,
Unknown
}
/* ======================================================================================
Static variable to get screen size which is orientation independent and it is "let" so only
first time call's geteSize(private method). In this geteSize call's only first time.
value == CGSize
===================================================================================== */
static let size : CGSize = DeviceUtil.getSize()
/* ======================================================================================
Static variable to get device type according to screen height it is "let" so only
first time call's getDeviceType(private method). In this getDeviceType call's only first time.
i.e if current device height is 960 then return iphone 4
value == Device Enum
===================================================================================== */
static let deviceType : Device = DeviceUtil.getDeviceType()
/* ======================================================================================
Private static method to get screen size
Parameter == nil;
Return == CGSize
===================================================================================== */
private static func getSize() -> CGSize {
var size : CGSize = CGSizeMake(UIScreen.mainScreen().nativeBounds.size.width/UIScreen.mainScreen().scale, UIScreen.mainScreen().nativeBounds.size.height/UIScreen.mainScreen().scale)
// For iPhone 6 Plus
if(size.height == 640 && size.width == 360){
size = CGSizeMake(414, 736)
}
return size
}
/* ======================================================================================
Private static method to get Device Type
Parameter == nil;
Return == Device Enum
===================================================================================== */
private static func getDeviceType() -> Device
{
let height = UIScreen.mainScreen().nativeBounds.size.height
switch height {
case 960:
return .iPhone4
case 1136:
return .iPhone5
case 1334:
return .iPhone6
case 2208:
return .iPhone6Plus
case 1024:
return .iPadMini
case 2048:
return .iPad
case 2732:
return .iPadPro
default:
return .Unknown;
}
}
/* ======================================================================================
Static method to get Device Language
Parameter == nil;
Return == NSString i.e "en-US"
===================================================================================== */
static func getDeviceLanguage() -> NSString
{
let pre = NSLocale.preferredLanguages()[0]
return pre;
}
/* ======================================================================================
Static method to get Device Orienttation is in LandscapeLeft or LandscapeRight
Parameter == nil;
Return == BOOL
i.e "Orienttation == .LandscapeLeft"
return true
===================================================================================== */
static func isLandscape() -> Bool
{
let orientation : UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation;
if (orientation == .LandscapeLeft || orientation == .LandscapeRight)
{
return true
}
return false
}
}
class GlobalStaticMethods {
//NSArray *ControllersArray
static func validateTextFields(textFieldType: Int , text: String) -> Bool{
if textFieldType == txtFieldTypeEnum.name.rawValue {
if text.characters.count < 3 || text.characters.count > 20 {
return false
}
return true
}
else if textFieldType == txtFieldTypeEnum.email.rawValue{
return GlobalStaticMethods.isValidEmail(text)
}
else if textFieldType == txtFieldTypeEnum.password.rawValue{
return GlobalStaticMethods.isValidPassword(text)
}
else if textFieldType == txtFieldTypeEnum.phone.rawValue {
return GlobalStaticMethods.isValidNumber(text)
}
else{
return false
}
}
static func getMessageFromStatusCode(statusCodeVal : Int)->String{
if statusCodeVal == statusCode.success.rawValue {
return statusCodeMessages.success.rawValue
}
else if statusCodeVal == statusCode.invalidSession.rawValue {
return statusCodeMessages.invalidSession.rawValue
}
else if statusCodeVal == statusCode.noRecordFound.rawValue {
return statusCodeMessages.noRecordFound.rawValue
}
else if statusCodeVal == statusCode.notAvailableForUserType.rawValue {
return statusCodeMessages.notAvailableForUserType.rawValue
}
else if statusCodeVal == statusCode.updatePassword.rawValue {
return statusCodeMessages.updatePassword.rawValue
}
else if statusCodeVal == statusCode.invalidRequest.rawValue {
return statusCodeMessages.invalidRequest.rawValue
}
else if statusCodeVal == statusCode.passwordEmpty.rawValue {
return statusCodeMessages.passwordEmpty.rawValue
}
else if statusCodeVal == statusCode.uniqueIdentifier.rawValue {
return statusCodeMessages.uniqueIdentifier.rawValue
}
else if statusCodeVal == statusCode.sessionToken.rawValue {
return statusCodeMessages.sessionToken.rawValue
}
else if statusCodeVal == statusCode.addedToFav.rawValue {
return statusCodeMessages.addedToFav.rawValue
}
else if statusCodeVal == statusCode.removedFromFav.rawValue {
return statusCodeMessages.removedFromFav.rawValue
}
else if statusCodeVal == statusCode.alreadyAddedToFav.rawValue {
return statusCodeMessages.alreadyAddedToFav.rawValue
}
else{
return "Try again"
}
}
static func getHeaderFooterHeight() -> CGFloat{
if GlobalStaticMethods.isPhone() {
return 56.0
}
else{
if GlobalStaticMethods.isPadPro(){
return 80.0
}
else{
return 65.0
}
}
}
static func getDeviceTypeStr() -> String {
if GlobalStaticMethods.isPad(){
return "ipad"
}
else{
return "iphone"
}
}
static func getDeviceToken() -> String{
let deviceToken = Singleton.sharedInstance.deviceToken
return deviceToken
}
static func getDeviceId() -> String{
let deviceId = UIDevice.currentDevice().identifierForVendor!.UUIDString
return deviceId
}
// MARK: - trimStr
// +(void)showMsg:(NSString*)msg withTitle:(NSString*)title;
/* static func getCountriesList()->NSArray{
let countriesArr = ["Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Antigua and Barbuda",
"Argentina",
"Armenia",
"Aruba",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas",
"Bahrain",
"Bangladesh",
"Barbados",
"Belarus",
"Belgium",
"Belize",
"Benin",
"Bhutan",
"Bolivia",
"Bosnia and Herzegovina",
"Botswana",
"Brazil",
"Brunei",
"Bulgaria",
"Burkina Faso",
"Burma",
"Burundi",
"Cambodia",
"Cameroon",
"Canada",
"Cape Verde",
"Central African Republic",
"Chad",
"Chile",
"China",
"Colombia",
"Comoros",
"Congo, Democratic Republic of the",
"Congo, Republic of the",
"Costa Rica",
"Cote d'Ivoire",
"Croatia",
"Cuba",
"Curacao",
"Cyprus",
"Czech Republic",
"Denmark",
"Djibouti",
"Dominica",
"Dominican Republic",
"East Timor",
"Ecuador",
"Egypt",
"El Salvador",
"Equatorial Guinea",
"Eritrea",
"Estonia",
"Ethiopia",
"Fiji",
"Finland",
"France",
"Gabon",
"Gambia",
"Georgia",
"Germany",
"Ghana",
"Greece",
"Grenada",
"Guatemala",
"Guinea",
"Guinea-Bissau",
"Guyana",
"Haiti",
"Holy See",
"Honduras",
"Hong Kong",
"Hungary",
"Iceland",
"India",
"Indonesia",
"Iran",
"Iraq",
"Ireland",
"Israel",
"Italy",
"Jamaica",
"Japan",
"Jordan",
"Kazakhstan",
"Kenya",
"Kiribati",
"Korea, North",
"Korea, South",
"Kosovo",
"Kuwait",
"Kyrgyzstan",
"Laos",
"Latvia",
"Lebanon",
"Lesotho",
"Liberia",
"Libya",
"Liechtenstein",
"Lithuania",
"Luxembourg",
"Macau",
"Macedonia",
"Madagascar",
"Malawi",
"Malaysia",
"Maldives",
"Mali",
"Malta",
"Marshall Islands",
"Mauritania",
"Mauritius",
"Mexico",
"Micronesia",
"Moldova",
"Monaco",
"Mongolia",
"Montenegro",
"Morocco",
"Mozambique",
"Namibia",
"Nauru",
"Nepal",
"Netherlands",
"Netherlands Antilles",
"New Zealand",
"Nicaragua",
"Niger",
"Nigeria",
"North Korea",
"Norway,",
"Oman,",
"Pakistan",
"Palau",
"Palestinian Territories",
"Panama",
"Papua New Guinea",
"Paraguay",
"Peru",
"Philippines",
"Poland",
"Portugal",
"Qatar",
"Romania",
"Russia",
"Rwanda",
"Saint Kitts and Nevis",
"Saint Lucia",
"Saint Vincent and the Grenadines",
"Samoa",
"San Marino",
"Sao Tome and Principe",
"Saudi Arabia",
"Senegal",
"Serbia",
"Seychelles",
"Sierra Leone",
"Singapore",
"Sint Maarten",
"Slovakia",
"Slovenia",
"Solomon Islands",
"Somalia",
"South Africa",
"South Korea",
"South Sudan",
"Spain",
"Sri Lanka",
"Sudan",
"Suriname",
"Swaziland",
"Sweden",
"Switzerland",
"Syria",
"Taiwan",
"Tajikistan",
"Tanzania",
"Thailand",
"Timor-Leste",
"Togo",
"Tonga",
"Trinidad and Tobago",
"Tunisia",
"Turkey",
"Turkmenistan",
"Tuvalu",
"Uganda",
"Ukraine",
"United Arab Emirates",
"United Kingdom",
"Uruguay",
"Uzbekistan",
"Vanuatu",
"Venezuela",
"Vietnam",
"Yemen",
"Zambia","Zimbabwe"]
return countriesArr
}*/
static func validateNumber(value: String) -> Bool {
let PHONE_REGEX = "^\\d{3}-\\d{3}-\\d{4}$"
let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
let result = phoneTest.evaluateWithObject(value)
return result
}
static func isValidNumber(number : String) -> Bool{
let badCharacters = NSCharacterSet.decimalDigitCharacterSet().invertedSet
if number.rangeOfCharacterFromSet(badCharacters) == nil {
return true
} else {
return false
}
// if number.characters.count < 6 {
// return false
// }
// let PHONE_REGEX = "^\\d{3}-\\d{3}-\\d{4}$"
// let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
// let result = phoneTest.evaluateWithObject(number)
// return result
}
static func isPhoneNumber(number : String) -> Bool{
let charcter = NSCharacterSet(charactersInString: "+0123456789").invertedSet
var filtered:NSString!
let inputString:NSArray = number.componentsSeparatedByCharactersInSet(charcter)
filtered = inputString.componentsJoinedByString("")
return number == filtered
}
static func isValidPasswordNumber(password : String) -> Bool{
if password.characters.count < 8{
return false
}
let passwordRegEx = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,14}$"
let passwordTest = NSPredicate(format: "SELF contains[c] %@", passwordRegEx)
let result = passwordTest.evaluateWithObject(password)
return result
}
static func isValidPassword(password : String) -> Bool{
if password.characters.count < 6{
return false
}
let passwordRegEx = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,14}$"
let passwordTest = NSPredicate(format: "SELF MATCHES %@", passwordRegEx)
let result = passwordTest.evaluateWithObject(password)
return result
}
static func isValidEmail(email : String) -> Bool{
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(email)
}
static func isLettersOnly(value : String) -> Bool{
for chr in value.characters {
if (!(chr >= "a" && chr <= "z") && !(chr >= "A" && chr <= "Z") ) {
return false
}
}
return true
}
static func isLettersANDSpacesOnly(value : String) -> Bool{
let letterSpaceRegEx = "^[a-zA-Z0-9_ ]*$"
let letterTest = NSPredicate(format: "SELF MATCHES %@", letterSpaceRegEx)
let result = letterTest.evaluateWithObject(value)
return result
}
static func isValidYoutubeUrl(value : String) -> Bool {
let youtubeRegex = "(http(s)?:\\/\\/)?(www\\.|m\\.)?youtu(be\\.com|\\.be)(\\/watch\\?([&=a-z]{0,})(v=[\\d\\w]{1,}).+|\\/[\\d\\w]{1,})"
let letterTest = NSPredicate(format: "SELF MATCHES %@", youtubeRegex)
let result = letterTest.evaluateWithObject(value)
return result
}
static func isPhone()->Bool{
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone{
return true
}
else
{
return false
}
}
static func isPad()->Bool{
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad{
return true
}
else
{
return false
}
}
static func isPadPro()->Bool{
var height : CGFloat = 0.0
if UIApplication.sharedApplication().statusBarOrientation.isLandscape {
height = 1024
}
else{
height = 1366
}
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad && UIScreen.mainScreen().bounds.size.height == height{
return true
}
else
{
return false
}
}
}
class Alert {
static func showAlertMsgWithTitleWithRootView( title : String , msg : String , btnActionTitle : String , viewController : UIViewController?, completionAction: (Void) -> Void ) -> Void{
let alertController = UIAlertController(title: title, message: msg, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: btnActionTitle, style: .Default, handler: { (action) in
completionAction()
})
alertController .addAction(alertAction)
// return alertController
if viewController != nil {
viewController! .presentViewController(alertController, animated: true, completion: nil)
}else{
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
static func showAlertMsgWithTitle( title : String , msg : String , btnActionTitle : String , viewController : UIViewController, completionAction: (Void) -> Void ) -> Void{
let alertController = UIAlertController(title: title, message: msg, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: btnActionTitle, style: .Default, handler: { (action) in
completionAction()
})
alertController .addAction(alertAction)
// return alertController
viewController .presentViewController(alertController, animated: true, completion: nil)
}
static func showAlertMsgWithTitle( title : String , msg : String , otherBtnTitle : String , otherBtnAction: (Void) -> Void , cancelBtnTitle : String , cancelBtnAction: (Void) -> Void, viewController : UIViewController ) -> Void{
let alertController = UIAlertController(title: title, message: msg, preferredStyle: .Alert)
let doneAction = UIAlertAction(title: otherBtnTitle, style: .Default, handler: { (action) in
otherBtnAction()
})
let cancelAction = UIAlertAction(title: cancelBtnTitle, style: .Default, handler: { (action) in
cancelBtnAction()
})
alertController .addAction(doneAction)
alertController .addAction(cancelAction)
// return alertController
viewController .presentViewController(alertController, animated: true, completion: nil)
}
static func showAlertMsgWithTitle( title : String , msg : String , otherBtnTitle : String , otherBtnAction: (Void) -> Void , viewController : UIViewController ) -> Void{
let alertController = UIAlertController(title: title, message: msg, preferredStyle: .Alert)
let doneAction = UIAlertAction(title: otherBtnTitle, style: .Default, handler: { (action) in
otherBtnAction()
})
alertController .addAction(doneAction)
// return alertController
viewController .presentViewController(alertController, animated: true, completion: nil)
}
static func showAlertForDropDowns(title : String , message : String , vc : UIViewController){
Alert.showAlertMsgWithTitle(title, msg: message, otherBtnTitle: "Ok", otherBtnAction: { (Success) in
printLog("")
}, viewController: vc)
}
}
class Singleton {
static let sharedInstance = Singleton()
var sessionToken : String = ""
var deviceToken : String = ""
var projectDataDictionary : NSArray!
// init() {
//
//// projectDataDictionary = plist .loadDataFromPlist("RequestVCType")
//
// uncomment this and add your plist file name
// }
}
class imageClass {
static func getImageFromURL(urlStr : String)->UIImage?{
let imageURL = NSURL(string: urlStr)
let image = UIImage(data: NSData(contentsOfURL: imageURL!)!)
return image
}
static func convertImageToBase64(image : UIImage) -> String {
let imageData:NSData = UIImagePNGRepresentation(image)!
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
return strBase64
}
static func convertBase64ToImage(str : String)->UIImage{
let dataDecoded:NSData = NSData(base64EncodedString: str, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
return decodedimage
}
static func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / image.size.width
let heightRatio = targetSize.height / image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
} else {
newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
func screenShotViewWithSaveOption(view : UIView!) {
//Create the UIImage
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
func screenShotViewImage(view : UIView!) -> UIImage{
//Create the UIImage
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Save it to the camera roll
return image
}
}
class stringsClass {
static func trimStr(str : String) ->String{
return str .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
}
static func getAttributedStringForHTML(var htmlStr : String , textSize : Int )->NSAttributedString?
{
do {
if htmlStr .isEmpty{
htmlStr = "<p></p>"
}
let str = "<div style=\"color:#5A5A5A; font-size: \(textSize)px\"><font face=\"MarselisPro\">\(htmlStr)</font></div>"
let data : NSData = str .dataUsingEncoding(NSUnicodeStringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedStr = try NSAttributedString.init(data: data, options: attributedOptions, documentAttributes: nil)
return attributedStr
}
catch {
return nil
}
}
//loool
static func getAttributedStringForHTMLWithFont( var htmlStr : String , textSize : Int , fontName : String )->NSAttributedString?
{
do {
if htmlStr .isEmpty{
htmlStr = "<p></p>"
}
let str = "<div style=\"color:#5A5A5A; font-size: \(textSize)px\"><font face=\"\(fontName)\">\(htmlStr)</font></div>"
let data : NSData = str .dataUsingEncoding(NSUnicodeStringEncoding)!
let attributedOptions : [String: AnyObject] = [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
]
let attributedStr = try NSAttributedString.init(data: data, options: attributedOptions, documentAttributes: nil)
return attributedStr
}
catch {
return nil
}
}
static func sanitizeStr (str : String) -> String{
let notAllowedCharacters = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+ ").invertedSet
let resultSTR = str.componentsSeparatedByCharactersInSet(notAllowedCharacters).joinWithSeparator("")
printLog(resultSTR)
return resultSTR
}
static func containSpecialChars(str : String)->Bool{
let notAllowedCharacters = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ").invertedSet
let resultSTR = str.componentsSeparatedByCharactersInSet(notAllowedCharacters).joinWithSeparator("")
printLog(resultSTR)
if resultSTR.characters.count == str.characters.count
{
return false;
}
else
{
return true;
}
}
static func containOnlyNumbers(str : String)->Bool{
let notAllowedCharacters = NSCharacterSet(charactersInString: "01234567890").invertedSet
let resultSTR = str.componentsSeparatedByCharactersInSet(notAllowedCharacters).joinWithSeparator("")
printLog(resultSTR)
if resultSTR.characters.count == str.characters.count
{
return false;
}
else
{
return true;
}
}
static func formatNumberAsCurrency(number : Int64)-> String{
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
let str = numberFormatter .stringFromNumber(NSNumber(longLong: number))
return str!
}
static func isEmptyString(str : String) ->String{
return str .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
static func stringWithoutWhitespaces(str : String)->String{
let words : NSArray = str.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
let nospacestring = words .componentsJoinedByString("")
return nospacestring
}
}
class reachability{
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))