-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPF.iOS.Common.pas
1657 lines (1481 loc) · 62.3 KB
/
DPF.iOS.Common.pas
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
// ------------------------------------------------------------------------------
// DPF.iOS.Common Tools & Classes
//
// Dadeh Pardazane Faragir ( DPF ) Co.
//
// Web: http://www.dpfaragir.com
//
// Developed By: Babak Yaghoobi
//
// Email #1: [email protected]
// Email #2: [email protected]
// Email #3: [email protected]
//
// ------------------------------------------------------------------------------
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ------------------------------------------------------------------------------
unit DPF.iOS.Common;
interface
{$I DPF.iOS.Defs.inc}
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Math,
System.Classes,
System.NetEncoding,
FMX.Consts,
FMX.Types,
FMX.Types3D,
FMX.Platform,
{$IFDEF DELPHIXE5}
FMX.Graphics,
{$ENDIF}
DateUtils,
{$IFDEF IOS}
{$IFDEF DELPHIXE6}
Macapi.Helpers,
{$ENDIF}
Soap.EncdDecd,
Macapi.ObjCRuntime,
Macapi.ObjectiveC,
Posix.Wchar,
Macapi.CoreFoundation,
IOSapi.CoreLocation,
IOSApi.Foundation,
IOSApi.UIKit,
IOSApi.QuartzCore,
IOSApi.CocoaTypes,
IOSApi.CoreGraphics,
IOSApi.CoreText,
FMX.Platform.iOS,
{$ELSE}
{$IFDEF MACOS}
Macapi.ObjectiveC,
Macapi.Foundation
{$ENDIF MACOS}
{$ENDIF IOS}
FMX.Forms;
const
libAudioToolbox = '/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox';
libKit = '/System/Library/Frameworks/UIKit.framework/UIKit';
libc = '/usr/lib/libc.dylib';
kSystemSoundID_vibrate = $FFF;
MERCATOR_OFFSET = 268435456;
MERCATOR_RADIUS = 85445659.44705395;
{$IFNDEF IOS}
type
{$IFDEF CPU64}
CGFloat = Double;
{$ELSE}
CGFloat = Single;
{$ENDIF}
{$ENDIF}
{$IFDEF IOS}
// ------------------------------------------------------------------------------
function NSSTR( const Str: string ): NSString;
// ------------------------------------------------------------------------------
function sysctl( name: PInteger; namelen: cardinal; oldp: Pointer; oldlen: Psize_t; newp: Pointer; newlen: size_t ): Integer; cdecl; external libc name _PU + 'sysctl';
function sysctlbyname( Name: MarshaledAString; oldp: Pointer; oldlen: Psize_t; newp: Pointer; newlen: size_t ): Integer; cdecl; external libc name _PU + 'sysctlbyname';
function sysctlnametomib( name: MarshaledAString; mibp: PInteger; sizep: Psize_t ): Integer; cdecl; external libc name _PU + 'sysctlnametomib';
// ------------------------------------------------------------------------------
procedure AudioServicesPlaySystemSound( inSystemSoundID: UInt32 ); cdecl; external libAudioToolbox name _PU + 'AudioServicesPlaySystemSound';
procedure UIGraphicsBeginImageContext( size: CGSize ); cdecl; external libUIKit name _PU + 'UIGraphicsBeginImageContext';
procedure UIGraphicsEndImageContext; cdecl; external libUIKit name _PU + 'UIGraphicsEndImageContext';
procedure UIGraphicsPushContext( context: CGContextRef ); cdecl; external libUIKit name _PU + 'UIGraphicsPushContext';
procedure UIGraphicsPopContext; cdecl; external libUIKit name _PU + 'UIGraphicsPopContext';
// function UIEdgeInsetsMake( top: CGFloat; left: CGFloat; bottom: CGFloat; right: CGFloat ): UIEdgeInsets; cdecl; external libUIKit name _PU + 'UIEdgeInsetsMake';
// {$EXTERNALSYM UIEdgeInsetsMake}
function GetSharedApplication: UIApplication;
function ScreenForceRotate( IfOrientationNotIn: TScreenOrientation ): Boolean;
procedure HideKeyBoard;
procedure ShowAlert( AMessage: string; ATitle: string = ''; CloseButton: string = 'Ok' );
function NSStrToStr( const ASource: NSString ): string;
function PNSStr( const AStr: string ): PNSString;
function GetAppVersion: string;
procedure VibrateDevice;
function GetFileSize( const FileName: string ): Int64;
function GetAppFolder: string;
function GetDocumentsFolder: string;
function GetTempDirectory: string;
function GetPreferencesFolder: string;
function TColorToUIColor( const Color: TAlphaColor; Alpha: Single ): UIColor; overload;
function TColorToUIColor( const Color: TAlphaColor ): UIColor; overload;
function TColorToUIColorPtr( const Color: TAlphaColor; Alpha: Single = 1 ): Pointer;
function AddFontFromFile( const FileName: string ): Boolean;
function NSDateToDateTime( const ADateTime: NSDate ): TDateTime;
function DateTimeToNSDate( const ADateTime: TDateTime ): NSDate;
function GetTimeZone: integer;
function GetGMTDateTime( const ADateTime: TDateTime ): TDateTime; overload;
function GetGMTDateTime( const ADateTime: TDateTime; TimeZone: integer ): TDateTime; overload;
function GetLocalDateTime( const ADateTime: TDateTime ): TDateTime; overload;
function longitudeToPixelSpaceX( longitude: double ): double;
function latitudeToPixelSpaceY( latitude: double ): double;
function pixelSpaceXToLongitude( pixelX: double ): double;
function pixelSpaceYToLatitude( pixelY: double ): double;
function GetURLContent( const HttpURL: string ): string;
function BitmapToUIImage( const Bitmap: TBitmap ): UIImage;
function UIImageToBitmap( const AImage: UIImage; ARotate: Single ): TBitmap;
function UIImageToBase64( const AImage: UIImage ): string;
function MemoryStreamToUIImage( const memStream: TMemoryStream ): UIImage;
procedure SaveBitmapInAlbum( const BitMap: TBitmap );
procedure SaveImageInAlbum( const Image: UIImage );
function IsIPad: Boolean;
function GetFileMIMEType( const AFileName: string ): string;
function CocoaNSStringConst( Fwk: string; ConstStr: string ): NSString;
function CocoaNSNumberConst( Fwk: string; ConstStr: string ): NSNumber;
function FNV1aHash( const S: string ): Cardinal;
function GetHashFileName( URL: string; CreateDirectory: Boolean ): string;
function GetHash( const Str: string ): int64;
procedure DPFNSLog( const AMessage: string ); overload;
procedure DPFNSLog( const AMessage: string; Rect: CGRect ); overload;
function PolyDecode( const encoded: string ): TArray<CLLocationCoordinate2D>;
procedure PlaySystemSound( SoundID: UInt32 = $450 );
function IsNumeric( const AString: string ): Boolean; overload;
function IsNumeric( const AChar: Char ): Boolean; inline; overload;
function convertImageToGrayScale( image: UIImage ): UIImage;
procedure SetAppFullScreen;
function GetImageOrientation( const AImage: UIImage ): Single;
procedure RemoveFile( FileName: string ); overload;
procedure RemoveFile( FileURL: NSURL ); overload;
function MakeScreenshot( view: UIView ): UIImage;
function AddLeadingZeroes( const aNumber, Length: integer ): string;
function isURLExists( const URL: string; const timeout: Single = 5.0 ): Integer;
function GetUUID( deleteExtra: Boolean = true ): string;
function GetDeviceID: string;
function GenerateBoundary: string;
function GetPlatform: string;
function GetSysInfoByName( typeSpecifier: string ): string;
function GetNSURL( URL: string ): NSURL;
function RenameFile( oldFileName: string; newFileName: string ): boolean;
{$ENDIF}
procedure DisposeOfAndNil( var Obj ); // SZ added
implementation
{$IFDEF IOS}
uses DPF.iOS.Classes;
var
MIMETableList: TStringList;
// DeviceTimeZone: NSInteger = $FFFF;
// ------------------------------------------------------------------------------
procedure DPFNSLog( const AMessage: string );
begin
{$IFDEF DEBUG}
try
NSLog( ( NSSTR( AMessage ) as ILocalObject ).GetObjectID );
except
end;
{$ENDIF}
end;
procedure DPFNSLog( const AMessage: string; Rect: CGRect );
begin
{$IFDEF DEBUG}
DPFNSLog(AMessage + ' ' + Format('%.1n, %.1n, %.1n, %.1n', [Rect.origin.x, Rect.origin.y, Rect.size.width, rect.size.height]));
{$ENDIF}
end;
// ------------------------------------------------------------------------------
function NSSTR( const Str: string ): NSString;
begin
{$IFDEF DELPHIXE6}
result := Macapi.Helpers.StrToNSStr( Str );
{$ELSE}
result := iOSapi.Foundation.NSSTR( Str );
{$ENDIF}
end;
// ------------------------------------------------------------------------------
function CocoaPointerConst( Fwk: string; ConstStr: string ): Pointer;
var
FwkMod: HMODULE;
begin
Result := nil;
FwkMod := LoadLibrary( PWideChar( Fwk ) );
if FwkMod <> 0 then
begin
Result := GetProcAddress( FwkMod, PWideChar( ConstStr ) );
FreeLibrary( FwkMod );
end;
end;
// ------------------------------------------------------------------------------
// const FoundationFwk: string = '/System/Library/Frameworks/Foundation.framework/Foundation';
//
// Result := CocoaNSStringConst(FoundationFwk, 'NSLocaleLanguageCode');
//
// ------------------------------------------------------------------------------
function CocoaNSStringConst( Fwk: string; ConstStr: string ): NSString;
var
Obj: Pointer;
begin
Obj := Pointer( CocoaPointerConst( Fwk, ConstStr )^ );
if Obj <> nil then
Result := TNSString.Wrap( Obj )
else
Result := nil;
end;
// ------------------------------------------------------------------------------
function CocoaNSNumberConst( Fwk: string; ConstStr: string ): NSNumber;
var
Obj: Pointer;
begin
Obj := Pointer( CocoaPointerConst( Fwk, ConstStr )^ );
if Obj <> nil then
Result := TNSNumber.Wrap( Obj )
else
Result := nil;
end;
// ------------------------------------------------------------------------------
function GetSysInfoByName( typeSpecifier: string ): string;
var
Size : Integer;
AResult: TArray<Byte>;
begin
sysctlbyname( MarshaledAString( TMarshal.AsAnsi( typeSpecifier ) ), nil, @Size, nil, 0 );
SetLength( AResult, Size );
sysctlbyname( MarshaledAString( TMarshal.AsAnsi( typeSpecifier ) ), MarshaledAString( AResult ), @Size, nil, 0 );
Result := TEncoding.UTF8.GetString( AResult );
end;
// ------------------------------------------------------------------------------
(*
iPhone1,1 -> iPhone 1G
iPhone1,2 -> iPhone 3G
iPhone2,1 -> iPhone 3GS
iPhone3,1 -> iPhone 4/AT&T
iPhone3,2 -> iPhone 4/Other
iPhone3,3 -> iPhone 4/Verizon
iPhone4,1 -> (iPhone 4S/GSM)
iPhone4,2 -> (iPhone 4S/CDMA)
iPhone4,3 -> (iPhone 4S)
iPhone5,1 -> iPhone Next Gen
iPhone5,1 -> iPhone Next Gen
iPhone5,1 -> iPhone Next Gen
iPod1,1 -> iPod touch 1G
iPod2,1 -> iPod touch 2G
iPod2,2 -> Unknown
iPod3,1 -> iPod touch 3G
iPod4,1 -> iPod touch 4G
iPad1,1 -> iPad 1G, WiFi and 3G
iPad2,1 -> iPad 2G, WiFi
iPad2,2 -> iPad 2G, GSM 3G
iPad2,3 -> iPad 2G, CDMA 3G
iPad3,1 -> (iPad 3G, WiFi)
iPad3,2 -> (iPad 3G, GSM)
iPad3,3 -> (iPad 3G, CDMA)
iPad4,1 -> (iPad 4G, WiFi)
iPad4,2 -> (iPad 4G, GSM)
iPad4,3 -> (iPad 4G, CDMA)
AppleTV2,1 -> AppleTV 2
AppleTV3,1 -> AppleTV 3
i386, x86_64 -> iPhone Simulator
*)
function GetPlatform: string;
begin
result := GetSysInfoByName( 'hw.machine' );
end;
// ------------------------------------------------------------------------------
function IsIPad: Boolean;
begin
Result := TUIDevice.Wrap( TUIDevice.OCClass.currentDevice ).userInterfaceIdiom = UIUserInterfaceIdiomPad;
end;
// ------------------------------------------------------------------------------
procedure SetAppFullScreen;
begin
GetSharedApplication.setStatusBarHidden( True );
GetSharedApplication.keyWindow.RootViewController.setWantsFullScreenLayout( True );
GetSharedApplication.keyWindow.RootViewController.view.setFrame( TUIScreen.Wrap( TUIScreen.OCClass.mainScreen ).bounds );
end;
// ------------------------------------------------------------------------------
function ScreenForceRotate( IfOrientationNotIn: TScreenOrientation ): boolean;
const
ConvOri: array [TScreenOrientation] of NativeUInt = ( UIDeviceOrientationPortrait, UIDeviceOrientationLandscapeRight, UIDeviceOrientationPortraitUpsideDown, UIDeviceOrientationLandscapeLeft );
var
w: UIWindow;
v: UIView;
// ScreenService : IFMXScreenService;
MainRroot : UIViewController;
Orientations: TFormOrientations;
// CurOrientation: TScreenOrientation;
begin
Orientations := Application.FormFactor.Orientations;
try
(*
if TPlatformServices.Current.SupportsPlatformService( IFMXScreenService, IInterface( ScreenService ) ) then
CurOrientation := ScreenService.GetScreenOrientation
else
CurOrientation := TScreenOrientation.soPortrait;
Result := CurOrientation <> IfOrientationNotIn;
*)
Result := true; // CurOrientation <> IfOrientationNotIn;
if Result then
begin
MainRroot := GetSharedApplication.keyWindow.rootViewController;
Application.FormFactor.Orientations := [TScreenOrientation.Portrait, TScreenOrientation.Landscape, TScreenOrientation.InvertedPortrait, TScreenOrientation.InvertedLandscape];
GetSharedApplication.keyWindow.setRootViewController( nil );
// application.ProcessMessages;
GetSharedApplication.setStatusBarOrientation( ConvOri[IfOrientationNotIn] );
TUIDevice.Wrap( TUIDevice.OCClass.currentDevice ).performSelector( sel_getUid( 'setOrientation:' ), ( TNSNumber.Wrap( TNSNumber.OCClass.numberWithUnsignedInt( ConvOri[IfOrientationNotIn] ) ) as ILocalObject ).GetObjectID, 0 );
w := GetSharedApplication.keyWindow;
if w.subviews.count > 0 then
begin
v := TUIView.Wrap( w.subviews.objectAtIndex( 0 ) );
v.removeFromSuperview;
w.insertSubview( v, 0 );
end;
GetSharedApplication.keyWindow.setRootViewController( MainRroot );
GetSharedApplication.keyWindow.rootViewController.didRotateFromInterfaceOrientation( ConvOri[IfOrientationNotIn] );
end;
finally
Application.FormFactor.Orientations := Orientations;
end;
end;
// ------------------------------------------------------------------------------
function GetURLContent( const HttpURL: string ): string;
var
url : NSURL;
content: NSString;
begin
url := TNSURL.Wrap( TNSURL.OCClass.URLWithString( NSStr( HttpURL ) ) );
content := TNSString.Wrap( TNSString.OCClass.stringWithContentsOfURL( url, NSUTF8StringEncoding, nil ) );
Result := UTF8ToString( content.UTF8String );
end;
// ------------------------------------------------------------------------------
function PNSStr( const AStr: string ): PNSString;
begin
Result := ( NSStr( AStr ) as ILocalObject ).GetObjectID
end;
// ------------------------------------------------------------------------------
function GetSharedApplication: UIApplication;
begin
Result := TUIApplication.Wrap( TUIApplication.OCClass.sharedApplication );
end;
// ------------------------------------------------------------------------------
procedure HideKeyBoard;
begin
GetSharedApplication.keyWindow.endEditing( true );
end;
// ------------------------------------------------------------------------------
function GetAppVersion: string;
var
NSB : NSBundle;
NSDic: NSDictionary;
NS : NSString;
begin
NSB := TNSBundle.Wrap( TNSBundle.OCClass.mainBundle );
NSDic := NSB.InfoDictionary;
NS := TNSString.Wrap( NSDic.valueForKey( NSStr( 'CFBundleVersion' ) ) );
Result := UTF8ToString( NS.UTF8String );
end;
// ------------------------------------------------------------------------------
// Note the following points:
// 1) Vibration work on iPhone and On the iPod touch, does nothing.
// 2) In the iPhone settings->Sounds->Vibrate on Ring set to ON
procedure VibrateDevice;
begin
AudioServicesPlaySystemSound( kSystemSoundID_vibrate );
end;
// ------------------------------------------------------------------------------
function GetFileSize( const FileName: string ): Int64;
var
FileManager : NSFileManager;
fileAttributes: NSDictionary;
url : NSURL;
begin
url := TNSURL.Wrap( TNSURL.OCClass.fileURLWithPath( NSStr( ParamStr( 0 ) ) ) );
FileManager := TNSFileManager.Wrap( TNSFileManager.OCClass.defaultManager );
fileAttributes := FileManager.attributesOfItemAtPath( NSStr( FileName ), nil );
Result := fileAttributes.fileSize;
FileManager.release;
FileManager := nil;
end;
// ------------------------------------------------------------------------------
function GetAppFolder: string;
begin
Result := GetHomePath + PathDelim + Application.Title + '.app' + PathDelim;
end;
// ------------------------------------------------------------------------------
function GetDocumentsFolder: string;
var
searchPaths : NSArray;
documentPath: NSString;
UR : NSURL;
begin
searchPaths := TNSFileManager.Wrap( TNSFileManager.OCClass.defaultManager ).URLsForDirectory( NSDocumentDirectory, NSUserDomainMask );
UR := TNSURL.Wrap( searchPaths.lastObject );
result := UTF8ToString( UR.path.UTF8String );
result := IncludeTrailingBackslash( result );
TNSFileManager.Wrap( TNSFileManager.OCClass.defaultManager ).createDirectoryAtPath( UR.path, true, nil, nil );
exit;
searchPaths := TNSArray.Wrap( NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, true ) );
if assigned( searchPaths ) and ( searchPaths.count > 0 ) then
documentPath := TNSString.Wrap( searchPaths.objectAtIndex( 0 ) );
if assigned( documentPath ) then
result := UTF8ToString( documentPath.UTF8String );
result := IncludeTrailingBackslash( result );
end;
// ------------------------------------------------------------------------------
function GetPreferencesFolder: string;
var
searchPaths: NSArray;
UR : NSURL;
begin
searchPaths := TNSFileManager.Wrap( TNSFileManager.OCClass.defaultManager ).URLsForDirectory( NSLibraryDirectory, NSUserDomainMask );
UR := TNSURL.Wrap( searchPaths.lastObject );
result := UTF8ToString( UR.path.UTF8String );
result := IncludeTrailingBackslash( result ) + 'Preferences/';
TNSFileManager.Wrap( TNSFileManager.OCClass.defaultManager ).createDirectoryAtPath( UR.path, true, nil, nil );
end;
// ------------------------------------------------------------------------------
function GetTempDirectory: string;
begin
Result := IncludeTrailingPathDelimiter( UTF8ToString( TNSString.Wrap( NSTemporaryDirectory( ) ).stringByExpandingTildeInPath.UTF8String ) );
end;
// ------------------------------------------------------------------------------
function TColorToUIColor( const Color: TAlphaColor; Alpha: Single ): UIColor; overload;
begin
if Color = TAlphaColors.Null then
Result := TUIColor.Wrap( TUIColor.OCClass.clearColor )
else
Result := TUIColor.Wrap( TUIColor.OCClass.colorWithRed( TAlphaColorRec( Color ).R / 255, TAlphaColorRec( Color ).G / 255, TAlphaColorRec( Color ).B / 255, Alpha ) );
end;
// ------------------------------------------------------------------------------
function TColorToUIColor( const Color: TAlphaColor ): UIColor; overload;
begin
if Color = TAlphaColors.Null then
Result := TUIColor.Wrap( TUIColor.OCClass.clearColor )
else
Result := TUIColor.Wrap( TUIColor.OCClass.colorWithRed( TAlphaColorRec( Color ).R / 255, TAlphaColorRec( Color ).G / 255, TAlphaColorRec( Color ).B / 255, TAlphaColorRec( Color ).A / 255 ) );
end;
// ------------------------------------------------------------------------------
function TColorToUIColorPtr( const Color: TAlphaColor; Alpha: Single = 1 ): Pointer;
begin
Result := TUIColor.OCClass.colorWithRed( TAlphaColorRec( Color ).R / 255, TAlphaColorRec( Color ).G / 255, TAlphaColorRec( Color ).B / 255, Alpha );
end;
// ------------------------------------------------------------------------------
function AddFontFromFile( const FileName: string ): Boolean;
var
FontData: NSData;
error : CFErrorRef;
provider: CGDataProviderRef;
font : CGFontRef;
begin
FontData := TNSData.Wrap( TNSData.OCClass.dataWithContentsOfFile( NSStr( FileName ) ) );
provider := CGDataProviderCreateWithCFData( CFDataRef( @FontData ) );
font := CGFontCreateWithDataProvider( provider );
Result := CTFontManagerRegisterGraphicsFont( font, @error ) > 0;
CFRelease( font );
CFRelease( provider );
end;
// ------------------------------------------------------------------------------
function GetTimeZone: integer;
begin
result := TNSTimeZone.Wrap( TNSTimeZone.OCClass.defaultTimeZone ).secondsFromGMT;
end;
// ------------------------------------------------------------------------------
// From FMX.Notification.iOS
{ function NSDateToDateTime( const ADateTime: NSDate ): TDateTime;
begin
Result := ( ADateTime.TimeIntervalSince1970 + GetTimeZone ) / SecsPerDay + EncodeDate( 1970, 1, 1 );
end; }
function NSDateToDateTime( const ADateTime: NSDate ): TDateTime;
var
t: double;
begin
t := ADateTime.timeIntervalSince1970;
if t < -EncodeDate( 1970, 1, 1 ) * SecsPerDay then
t := t - SecsPerDay;
Result := ( t + GetTimeZone ) / SecsPerDay + EncodeDate( 1970, 1, 1 );
end;
// ------------------------------------------------------------------------------
function DateTimeToNSDate( const ADateTime: TDateTime ): NSDate;
begin
result := TNSDate.Wrap( TNSDate.OCClass.dateWithTimeIntervalSince1970( DateTimeToUnix( ADateTime ) ) );
end;
// ------------------------------------------------------------------------------
// From FMX.Notification.iOS
function GetGMTDateTime( const ADateTime: TDateTime ): TDateTime; overload;
begin
Result := IncSecond( ADateTime, ( -1 ) * GetTimeZone );
end;
// ------------------------------------------------------------------------------
function GetLocalDateTime( const ADateTime: TDateTime ): TDateTime; overload;
begin
Result := IncSecond( ADateTime, GetTimeZone );
end;
// ------------------------------------------------------------------------------
function GetGMTDateTime( const ADateTime: TDateTime; TimeZone: integer ): TDateTime; overload;
begin
if GetTimeZone > 0 then
Result := ADateTime - EncodeTime( TimeZone, 0, 0, 0 )
else
Result := ADateTime + EncodeTime( Abs( TimeZone ), 0, 0, 0 );
end;
// ------------------------------------------------------------------------------
function NSStrToStr( const ASource: NSString ): string;
begin
if ASource = nil then // SZ: do it the same way as Delphi NSStrToStr (ASource could be nil if a string is empty)
Exit( '' )
else
Result := UTF8ToString( ASource.UTF8String );
end;
// ------------------------------------------------------------------------------
function longitudeToPixelSpaceX( longitude: double ): double;
begin
if longitude > 90.0 then
longitude := 90;
if longitude < -90.0 then
longitude := -90;
Result := MERCATOR_OFFSET;
if not IsNan( longitude ) then
Result := Round( MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * PI / 180.0 );
end;
// ------------------------------------------------------------------------------
function latitudeToPixelSpaceY( latitude: double ): double;
begin
if latitude > 180.0 then
latitude := 180.0;
if latitude < -180.0 then
latitude := -180.0;
Result := MERCATOR_OFFSET;
if not IsNan( latitude ) then
Result := Round( MERCATOR_OFFSET - MERCATOR_RADIUS * ln( ( 1 + sin( latitude * PI / 180.0 ) ) / ( 1 - sin( latitude * PI / 180.0 ) ) ) / 2.0 );
end;
// ------------------------------------------------------------------------------
function pixelSpaceXToLongitude( pixelX: double ): double;
begin
Result := ( ( Round( pixelX ) - MERCATOR_OFFSET ) / MERCATOR_RADIUS ) * 180.0 / PI;
end;
// ------------------------------------------------------------------------------
function pixelSpaceYToLatitude( pixelY: double ): double;
begin
Result := ( PI / 2.0 - 2.0 * tan( exp( ( Round( pixelY ) - MERCATOR_OFFSET ) / MERCATOR_RADIUS ) ) ) * 180.0 / PI;
end;
// ------------------------------------------------------------------------------
function UIImageToBase64( const AImage: UIImage ): string;
var
BMP : TBitMap;
InputS : TMemoryStream;
OutputS: TStringStream;
begin
BMP := UIImageToBitmap( AImage, 0 );
InputS := TMemoryStream.Create;
OutputS := TStringStream.Create;
BMP.SaveToStream( InputS );
BMP.Free;
InputS.Position := 0;
EncodeStream( InputS, OutputS );
Result := OutputS.DataString;
InputS.Free;
OutputS.Free;
end;
// ------------------------------------------------------------------------------
function UIImageToBitmap( const AImage: UIImage; ARotate: Single ): TBitmap;
function ReduceImageSize( const AOriginalSize: TSize ): TSize;
var
ScaleCoef : Single;
LMaxTextureSize: Integer;
begin
{$IFDEF DELPHIXE7}
LMaxTextureSize := TContextManager.DefaultContextClass.MaxTextureSize;
{$ELSE}
LMaxTextureSize := TContext3D.MaxTextureSize;
{$ENDIF}
Result := AOriginalSize;
if Max( AOriginalSize.cx, AOriginalSize.cy ) div LMaxTextureSize > 0 then
begin
ScaleCoef := Max( AOriginalSize.cx, AOriginalSize.cy ) / LMaxTextureSize;
Result := TSize.Create( Round( AOriginalSize.cx / ScaleCoef ), Round( AOriginalSize.cy / ScaleCoef ) );
end;
end;
var
ImageRef : CGImageRef;
Bitmap : TBitmap;
CtxRef : CGContextRef;
ColorSpace: CGColorSpaceRef;
Data : TBitmapData;
BitmapSize: TSize;
begin
ImageRef := AImage.CGImage;
if Assigned( ImageRef ) then
begin
BitmapSize := ReduceImageSize( TSize.Create( CGImageGetWidth( ImageRef ), CGImageGetHeight( ImageRef ) ) );
Bitmap := TBitmap.Create( BitmapSize.cx, BitmapSize.cy );
ColorSpace := CGColorSpaceCreateDeviceRGB;
try
if Bitmap.Map( TMapAccess.Write, Data ) then
begin
CtxRef := CGBitmapContextCreate( Data.Data, Bitmap.Width, Bitmap.Height, 8, 4 * Bitmap.Width, ColorSpace, kCGImageAlphaPremultipliedLast or kCGBitmapByteOrder32Big );
try
CGContextDrawImage( CtxRef, CGRectMake( 0, 0, Bitmap.Width, Bitmap.Height ), ImageRef );
finally
CGContextRelease( CtxRef );
end;
end;
finally
CGColorSpaceRelease( ColorSpace );
end;
Bitmap.Rotate( ARotate );
Result := Bitmap;
end
else
Result := nil;
end;
// ------------------------------------------------------------------------------
function BitmapToUIImage( const Bitmap: TBitmap ): UIImage;
var
ImageRef : CGImageRef;
CtxRef : CGContextRef;
ColorSpace: CGColorSpaceRef;
BitmapData: TBitmapData;
begin
if Bitmap.IsEmpty then
Result := TUIImage.Create
else
begin
ColorSpace := CGColorSpaceCreateDeviceRGB;
try
if Bitmap.Map( TMapAccess.Read, BitmapData ) then
begin
CtxRef := CGBitmapContextCreate( BitmapData.Data, Bitmap.Width, Bitmap.Height, 8, 4 * Bitmap.Width, ColorSpace, kCGImageAlphaPremultipliedLast or kCGBitmapByteOrder32Big );
try
ImageRef := CGBitmapContextCreateImage( CtxRef );
try
Result := TUIImage.Wrap( TUIImage.Alloc.initWithCGImage( ImageRef ) );
finally
CGImageRelease( ImageRef );
end;
finally
CGContextRelease( CtxRef );
end;
end;
finally
CGColorSpaceRelease( ColorSpace );
end;
end;
end;
// ------------------------------------------------------------------------------
function MemoryStreamToUIImage( const memStream: TMemoryStream ): UIImage;
var
data: NSData;
begin
Result := nil;
memStream.Position := 0;
data := TNSData.Wrap( TNSData.alloc.initWithBytesNoCopy( memStream.Memory, memStream.Size, False ) );
try
if data.length > 0 then
Result := TUIImage.Wrap( TUIImage.alloc.initWithData( data ) );
finally
data.release;
end;
end;
// ------------------------------------------------------------------------------
function GetFileMIMEType( const AFileName: string ): string;
var
LExt: string;
begin
LExt := LowerCase( ExtractFileExt( AFileName ) );
Result := MIMETableList.Values[LExt];
if Result = '' then
Result := 'application/octet-stream'
end;
// ------------------------------------------------------------------------------
procedure SaveBitmapInAlbum( const BitMap: TBitmap );
var
Image: UIImage;
begin
if not Assigned( Bitmap ) then
exit;
Image := BitmapToUIImage( BitMap );
UIImageWriteToSavedPhotosAlbum( ( Image as ILocalObject ).GetObjectID, nil, nil, nil );
end;
// ------------------------------------------------------------------------------
procedure SaveImageInAlbum( const Image: UIImage );
begin
if Assigned( image ) and ( image.size.width > 0.99 ) then
UIImageWriteToSavedPhotosAlbum( ( Image as ILocalObject ).GetObjectID, nil, nil, nil );
end;
// ------------------------------------------------------------------------------
// Extracted From Indy.
procedure FillMimeTable;
begin
if not Assigned( MIMETableList ) or ( MIMETableList.Count > 0 ) then
Exit;
// Animation
MIMETableList.Add( '.nml=animation/narrative' );
// Audio
MIMETableList.Add( '.aac=audio/mp4' );
MIMETableList.Add( '.aif=audio/x-aiff' );
MIMETableList.Add( '.aifc=audio/x-aiff' );
MIMETableList.Add( '.aiff=audio/x-aiff' );
MIMETableList.Add( '.au=audio/basic' );
MIMETableList.Add( '.gsm=audio/x-gsm' );
MIMETableList.Add( '.kar=audio/midi' );
MIMETableList.Add( '.m3u=audio/mpegurl' );
MIMETableList.Add( '.m4a=audio/x-mpg' );
MIMETableList.Add( '.mid=audio/midi' );
MIMETableList.Add( '.midi=audio/midi' );
MIMETableList.Add( '.mpega=audio/x-mpg' );
MIMETableList.Add( '.mp2=audio/x-mpg' );
MIMETableList.Add( '.mp3=audio/x-mpg' );
MIMETableList.Add( '.mpga=audio/x-mpg' );
MIMETableList.Add( '.m3u=audio/x-mpegurl' );
MIMETableList.Add( '.pls=audio/x-scpls' );
MIMETableList.Add( '.qcp=audio/vnd.qcelp' );
MIMETableList.Add( '.ra=audio/x-realaudio' );
MIMETableList.Add( '.ram=audio/x-pn-realaudio' );
MIMETableList.Add( '.rm=audio/x-pn-realaudio' );
MIMETableList.Add( '.sd2=audio/x-sd2' );
MIMETableList.Add( '.sid=audio/prs.sid' );
MIMETableList.Add( '.snd=audio/basic' );
MIMETableList.Add( '.wav=audio/x-wav' );
MIMETableList.Add( '.wax=audio/x-ms-wax' );
MIMETableList.Add( '.wma=audio/x-ms-wma' );
MIMETableList.Add( '.mjf=audio/x-vnd.AudioExplosion.MjuiceMediaFile' );
// Image
MIMETableList.Add( '.art=image/x-jg' );
MIMETableList.Add( '.bmp=image/bmp' );
MIMETableList.Add( '.cdr=image/x-coreldraw' );
MIMETableList.Add( '.cdt=image/x-coreldrawtemplate' );
MIMETableList.Add( '.cpt=image/x-corelphotopaint' );
MIMETableList.Add( '.djv=image/vnd.djvu' );
MIMETableList.Add( '.djvu=image/vnd.djvu' );
MIMETableList.Add( '.gif=image/gif' );
MIMETableList.Add( '.ief=image/ief' );
MIMETableList.Add( '.ico=image/x-icon' );
MIMETableList.Add( '.jng=image/x-jng' );
MIMETableList.Add( '.jpg=image/jpeg' );
MIMETableList.Add( '.jpeg=image/jpeg' );
MIMETableList.Add( '.jpe=image/jpeg' );
MIMETableList.Add( '.pat=image/x-coreldrawpattern' );
MIMETableList.Add( '.pcx=image/pcx' );
MIMETableList.Add( '.pbm=image/x-portable-bitmap' );
MIMETableList.Add( '.pgm=image/x-portable-graymap' );
MIMETableList.Add( '.pict=image/x-pict' );
MIMETableList.Add( '.png=image/x-png' );
MIMETableList.Add( '.pnm=image/x-portable-anymap' );
MIMETableList.Add( '.pntg=image/x-macpaint' );
MIMETableList.Add( '.ppm=image/x-portable-pixmap' );
MIMETableList.Add( '.psd=image/x-psd' );
MIMETableList.Add( '.qtif=image/x-quicktime' );
MIMETableList.Add( '.ras=image/x-cmu-raster' );
MIMETableList.Add( '.rf=image/vnd.rn-realflash' );
MIMETableList.Add( '.rgb=image/x-rgb' );
MIMETableList.Add( '.rp=image/vnd.rn-realpix' );
MIMETableList.Add( '.sgi=image/x-sgi' );
MIMETableList.Add( '.svg=image/svg-xml' );
MIMETableList.Add( '.svgz=image/svg-xml' );
MIMETableList.Add( '.targa=image/x-targa' );
MIMETableList.Add( '.tif=image/x-tiff' );
MIMETableList.Add( '.wbmp=image/vnd.wap.wbmp' );
MIMETableList.Add( '.webp=image/webp' );
MIMETableList.Add( '.xbm=image/xbm' );
MIMETableList.Add( '.xbm=image/x-xbitmap' );
MIMETableList.Add( '.xpm=image/x-xpixmap' );
MIMETableList.Add( '.xwd=image/x-xwindowdump' );
// Text
MIMETableList.Add( '.323=text/h323' );
MIMETableList.Add( '.xml=text/xml' );
MIMETableList.Add( '.uls=text/iuls' );
MIMETableList.Add( '.txt=text/plain' );
MIMETableList.Add( '.rtx=text/richtext' );
MIMETableList.Add( '.wsc=text/scriptlet' );
MIMETableList.Add( '.rt=text/vnd.rn-realtext' );
MIMETableList.Add( '.htt=text/webviewhtml' );
MIMETableList.Add( '.htc=text/x-component' );
MIMETableList.Add( '.vcf=text/x-vcard' );
// Video
MIMETableList.Add( '.asf=video/x-ms-asf' );
MIMETableList.Add( '.asx=video/x-ms-asf' );
MIMETableList.Add( '.avi=video/x-msvideo' );
MIMETableList.Add( '.dl=video/dl' );
MIMETableList.Add( '.dv=video/dv' );
MIMETableList.Add( '.flc=video/flc' );
MIMETableList.Add( '.fli=video/fli' );
MIMETableList.Add( '.gl=video/gl' );
MIMETableList.Add( '.lsf=video/x-la-asf' );
MIMETableList.Add( '.lsx=video/x-la-asf' );
MIMETableList.Add( '.mng=video/x-mng' );
MIMETableList.Add( '.mp2=video/mpeg' );
MIMETableList.Add( '.mp3=video/mpeg' );
MIMETableList.Add( '.mp4=video/mp4' );
MIMETableList.Add( '.mpeg=video/x-mpeg2a' );
MIMETableList.Add( '.mpa=video/mpeg' );
MIMETableList.Add( '.mpe=video/mpeg' );
MIMETableList.Add( '.mpg=video/mpeg' );
MIMETableList.Add( '.ogv=video/ogg' );
MIMETableList.Add( '.moov=video/quicktime' );
MIMETableList.Add( '.mov=video/quicktime' );
MIMETableList.Add( '.mxu=video/vnd.mpegurl' );
MIMETableList.Add( '.qt=video/quicktime' );
MIMETableList.Add( '.qtc=video/x-qtc' );
MIMETableList.Add( '.rv=video/vnd.rn-realvideo' );
MIMETableList.Add( '.ivf=video/x-ivf' );
MIMETableList.Add( '.webm=video/webm' );
MIMETableList.Add( '.wm=video/x-ms-wm' );
MIMETableList.Add( '.wmp=video/x-ms-wmp' );
MIMETableList.Add( '.wmv=video/x-ms-wmv' );
MIMETableList.Add( '.wmx=video/x-ms-wmx' );
MIMETableList.Add( '.wvx=video/x-ms-wvx' );
MIMETableList.Add( '.rms=video/vnd.rn-realvideo-secure' );
MIMETableList.Add( '.asx=video/x-ms-asf-plugin' );
MIMETableList.Add( '.movie=video/x-sgi-movie' );
// Application
MIMETableList.Add( '.7z=application/x-7z-compressed' );
MIMETableList.Add( '.a=application/x-archive' );
MIMETableList.Add( '.aab=application/x-authorware-bin' );
MIMETableList.Add( '.aam=application/x-authorware-map' );
MIMETableList.Add( '.aas=application/x-authorware-seg' );
MIMETableList.Add( '.abw=application/x-abiword' );
MIMETableList.Add( '.ace=application/x-ace-compressed' );
MIMETableList.Add( '.ai=application/postscript' );
MIMETableList.Add( '.alz=application/x-alz-compressed' );
MIMETableList.Add( '.ani=application/x-navi-animation' );
MIMETableList.Add( '.arj=application/x-arj' );
MIMETableList.Add( '.asf=application/vnd.ms-asf' );
MIMETableList.Add( '.bat=application/x-msdos-program' );
MIMETableList.Add( '.bcpio=application/x-bcpio' );
MIMETableList.Add( '.boz=application/x-bzip2' );
MIMETableList.Add( '.bz=application/x-bzip' );
MIMETableList.Add( '.bz2=application/x-bzip2' );
MIMETableList.Add( '.cab=application/vnd.ms-cab-compressed' );
MIMETableList.Add( '.cat=application/vnd.ms-pki.seccat' );
MIMETableList.Add( '.ccn=application/x-cnc' );
MIMETableList.Add( '.cco=application/x-cocoa' );
MIMETableList.Add( '.cdf=application/x-cdf' );
MIMETableList.Add( '.cer=application/x-x509-ca-cert' );
MIMETableList.Add( '.chm=application/vnd.ms-htmlhelp' );
MIMETableList.Add( '.chrt=application/vnd.kde.kchart' );
MIMETableList.Add( '.cil=application/vnd.ms-artgalry' );
MIMETableList.Add( '.class=application/java-vm' );
MIMETableList.Add( '.com=application/x-msdos-program' );
MIMETableList.Add( '.clp=application/x-msclip' );
MIMETableList.Add( '.cpio=application/x-cpio' );
MIMETableList.Add( '.cpt=application/mac-compactpro' );
MIMETableList.Add( '.cqk=application/x-calquick' );
MIMETableList.Add( '.crd=application/x-mscardfile' );
MIMETableList.Add( '.crl=application/pkix-crl' );
MIMETableList.Add( '.csh=application/x-csh' );
MIMETableList.Add( '.dar=application/x-dar' );
MIMETableList.Add( '.dbf=application/x-dbase' );
MIMETableList.Add( '.dcr=application/x-director' );
MIMETableList.Add( '.deb=application/x-debian-package' );
MIMETableList.Add( '.dir=application/x-director' );
MIMETableList.Add( '.dist=vnd.apple.installer+xml' );
MIMETableList.Add( '.distz=vnd.apple.installer+xml' );
MIMETableList.Add( '.dll=application/x-msdos-program' );
MIMETableList.Add( '.dmg=application/x-apple-diskimage' );
MIMETableList.Add( '.doc=application/msword' );
MIMETableList.Add( '.dot=application/msword' );
MIMETableList.Add( '.dvi=application/x-dvi' );
MIMETableList.Add( '.dxr=application/x-director' );