-
Notifications
You must be signed in to change notification settings - Fork 184
/
WTWikitudePlugin.m
992 lines (746 loc) · 41.2 KB
/
WTWikitudePlugin.m
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
//
// WTWikitudePlugin.m
// Wikitude
//
// Copyright (c) 2012 Wikitude. All rights reserved.
//
#import "WTWikitudePlugin.h"
#import <WikitudeSDK/WikitudeSDK.h>
#import "WTARViewController.h"
//------------ Start-up Configuration - begin -------
//
// Note: The keys and values used here have to be in accordance with those specified in WikitudePlungin.js
//
NSString * const kWTWikitudePlugin_ArgumentKeyLicenseKey = @"SDKKey";
NSString * const kWTWikitudePlugin_ArgumentKeyARchitectWorldURL = @"ARchitectWorldURL";
NSString * const kWTWikitudePlugin_ArgumentFeatureConfiguration = @"StartupConfiguration";
NSString * const kWTWikitudePlugin_ArgumentIOSConfiguration = @"iOS";
NSString * const kWTWikitudePlugin_ArgumentRequiredFeatures = @"RequiredFeatures";
NSString * const kWTWikitudePlugin_requiredFeature_Geo = @"geo";
NSString * const kWTWikitudePlugin_requiredFeature_ImageTracking = @"image_tracking";
NSString * const kWTWikitudePlugin_requiredFeature_InstantTracking = @"instant_tracking";
NSString * const kWTWikitudePlugin_requiredFeature_ObjectTracking = @"object_tracking";
NSString * const kWTWikitudePlugin_requiredFeature_PhotoLibraryScreenshotImport = @"photo_library_screenshot_import";
NSString * const kWTWikitudePlugin_ArgumentCaptureDeviceResolution = @"camera_resolution";
NSString * const kWTWikitudePlugin_captureDeviceResolution_SD_640x480 = @"sd_640x480";
NSString * const kWTWikitudePlugin_captureDeviceResolution_HD_1280x720 = @"hd_1280x720";
NSString * const kWTWikitudePlugin_captureDeviceResolution_FULL_HD_1920x1080 = @"full_hd_1920x1080";
NSString * const kWTWikitudePlugin_captureDeviceResolution_AUTO = @"auto";
NSString * const kWTWikitudePlugin_ArgumentCameraPosition = @"camera_position";
NSString * const kWTWikitudePlugin_cameraPosition_Undefined = @"undefined";
NSString * const kWTWikitudePlugin_cameraPosition_Front = @"front";
NSString * const kWTWikitudePlugin_cameraPosition_Back = @"back";
NSString * const kWTWikitudePlugin_ArgumentCameraFocusMode = @"cameraFocusMode";
NSString * const kWTWikitudePlugin_cameraFocusMode_Locked = @"locked";
NSString * const kWTWikitudePlugin_cameraFocusMode_AutoFocus = @"autoFocus";
NSString * const kWTWikitudePlugin_cameraFocusMode_ContinuousAutoFocus = @"continuousAutoFocus";
NSString * const kWTWikitudePlugin_ArgumentCaptureDeviceFocusDistance = @"camera_manual_focus_distance";
NSString * const kWTWikitudePlugin_ArgumentCaptureDeviceFocusRangeRestriction = @"cameraFocusRangeRestriction";
NSString * const kWTWikitudePlugin_cameraFocusRange_None = @"none";
NSString * const kWTWikitudePlugin_cameraFocusRange_Near = @"near";
NSString * const kWTWikitudePlugin_cameraFocusRange_Far = @"far";
NSString * const kWTWikitudePlugin_ArgumentSystemDeviceSensorCalibrationDisplay = @"shouldUseSystemDeviceSensorCalibrationDisplay";
NSString * const kWTWikitudePlugin_useSystemDeviceSensorCalibrationDispaly_NO = @"NO";
NSString * const kWTWikitudePlugin_useSystemDeviceSensorCalibrationDispaly_YES = @"YES";
//------------ Start-up Configuration - end ---------
//------------ plist keys - begin -------
NSString * const kWTWikitudePlugin_localPathPrefix = @"WTCordovaPluginLocalPathPrefix";
//------------ plist keys - end ---------
@interface WTWikitudePlugin () <WTArchitectViewDelegate, WTArchitectViewControllerDelegate>
@property (nonatomic, strong) WTAuthorizationRequestManager *authorizationRequestManager;
@property (nonatomic, strong) WTArchitectViewController *arViewController;
@property (nonatomic, strong) NSString *loadArchitectWorldCallbackId;
@property (nonatomic, strong) NSString *urlInvokedCallbackId;
@property (nonatomic, strong) NSString *receivedJSONObjectCallbackId;
@property (nonatomic, strong) NSString *screenshotCallbackId;
@property (nonatomic, strong) NSString *errorHandlerCallbackId;
@property (nonatomic, strong) NSString *accessRequestCallbackId;
@property (nonatomic, strong) NSString *deviceSensorsNeedCalibrationCallbackId;
@property (nonatomic, strong) NSString *deviceSensorsFinishedCalibrationCallbackId;
@property (nonatomic, strong) NSString *onBackButtonCallbackId;
@property (nonatomic, assign) BOOL isUsingInjectedLocation;
@property (nonatomic, assign) BOOL isDeviceSupported;
@end
@implementation WTWikitudePlugin
+ (NSString *)composeFailingAPIAuthorizationMessageFromError:(NSError *)error
{
NSDictionary *unauthorizedAPIInfo = [[error userInfo] objectForKey:kWTUnauthorizedAppleiOSSDKAPIsKey];
NSMutableString *detailedAuthorizationErrorLogMessage = [[NSMutableString alloc] initWithFormat:@"The following authorization states do not meet the requirements:"];
NSMutableString *missingAuthorizations = [[NSMutableString alloc] initWithFormat:@"In order to use the Wikitude SDK, please grant access to the following:"];
for (NSString *unauthorizedAPIKey in [unauthorizedAPIInfo allKeys])
{
[missingAuthorizations appendFormat:@"\n* %@", [WTAuthorizationRequestManager humanReadableDescriptionForUnauthorizedAppleiOSSDKAPI:unauthorizedAPIKey]];
[detailedAuthorizationErrorLogMessage appendFormat:@"\n%@ = %@", unauthorizedAPIKey, [WTAuthorizationRequestManager stringFromAuthorizationStatus:[[unauthorizedAPIInfo objectForKey:unauthorizedAPIKey] integerValue] forUnauthorizedAppleiOSSDKAPI:unauthorizedAPIKey]];
}
NSLog(@"%@", detailedAuthorizationErrorLogMessage);
return missingAuthorizations;
}
+ (void)readStartupConfigurationFrom:(NSDictionary *)arguments andApplyTo:(WTStartupConfiguration *)configuration
{
if (arguments && configuration)
{
NSDictionary *featureConfiguration = [arguments objectForKey:kWTWikitudePlugin_ArgumentFeatureConfiguration];
if(featureConfiguration)
{
NSString* captureDeviceResolution = [featureConfiguration objectForKey:kWTWikitudePlugin_ArgumentCaptureDeviceResolution];
if (captureDeviceResolution)
{
if ( [kWTWikitudePlugin_captureDeviceResolution_SD_640x480 isEqualToString:captureDeviceResolution] )
{
configuration.captureDeviceResolution = WTCaptureDeviceResolution_SD_640x480;
}
else if ( [kWTWikitudePlugin_captureDeviceResolution_HD_1280x720 isEqualToString:captureDeviceResolution] )
{
configuration.captureDeviceResolution = WTCaptureDeviceResolution_HD_1280x720;
}
else if ( [kWTWikitudePlugin_captureDeviceResolution_FULL_HD_1920x1080 isEqualToString:captureDeviceResolution] )
{
configuration.captureDeviceResolution = WTCaptureDeviceResolution_FULL_HD_1920x1080;
}
else if ( [kWTWikitudePlugin_captureDeviceResolution_AUTO isEqualToString:captureDeviceResolution] )
{
configuration.captureDeviceResolution = WTCaptureDeviceResolution_AUTO;
}
}
NSString *cameraPosition = [featureConfiguration objectForKey:kWTWikitudePlugin_ArgumentCameraPosition];
if(cameraPosition)
{
if ( [kWTWikitudePlugin_cameraPosition_Front isEqualToString:cameraPosition] )
{
configuration.captureDevicePosition = AVCaptureDevicePositionFront;
}
else if ( [kWTWikitudePlugin_cameraPosition_Back isEqualToString:cameraPosition] )
{
configuration.captureDevicePosition = AVCaptureDevicePositionBack;
}
else
{
configuration.captureDevicePosition = AVCaptureDevicePositionUnspecified;
}
}
NSString *cameraFocusMode = [[featureConfiguration objectForKey:kWTWikitudePlugin_ArgumentCameraFocusMode] stringValue];
if(cameraFocusMode)
{
if ( [kWTWikitudePlugin_cameraFocusMode_Locked isEqualToString:cameraFocusMode] )
{
configuration.captureDeviceFocusMode = AVCaptureFocusModeLocked;
}
else if ( [kWTWikitudePlugin_cameraFocusMode_AutoFocus isEqualToString:cameraFocusMode] )
{
configuration.captureDeviceFocusMode = AVCaptureFocusModeAutoFocus;
}
else if ( [kWTWikitudePlugin_cameraFocusMode_ContinuousAutoFocus isEqualToString:cameraFocusMode] )
{
configuration.captureDeviceFocusMode = AVCaptureFocusModeContinuousAutoFocus;
}
}
NSNumber *captureDeviceFocusDistance = [featureConfiguration objectForKey:kWTWikitudePlugin_ArgumentCaptureDeviceFocusDistance];
if (captureDeviceFocusDistance)
{
configuration.captureDeviceFocusDistance = captureDeviceFocusDistance.floatValue;
}
NSDictionary* iOSConfiguration = [featureConfiguration objectForKey:kWTWikitudePlugin_ArgumentIOSConfiguration];
if(iOSConfiguration)
{
NSString *captureDeviceFocusRestriction = [iOSConfiguration objectForKey:kWTWikitudePlugin_ArgumentCaptureDeviceFocusRangeRestriction];
if(captureDeviceFocusRestriction)
{
if ( [kWTWikitudePlugin_cameraFocusRange_None isEqualToString:captureDeviceFocusRestriction] )
{
configuration.captureDeviceFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNone;
}
else if ( [kWTWikitudePlugin_cameraFocusRange_Near isEqualToString:captureDeviceFocusRestriction] )
{
configuration.captureDeviceFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear;
}
else if ( [kWTWikitudePlugin_cameraFocusRange_Far isEqualToString:captureDeviceFocusRestriction] )
{
configuration.captureDeviceFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionFar;
}
}
NSString *systemDeviceSensorCalibrationSetting = [iOSConfiguration objectForKey:kWTWikitudePlugin_ArgumentSystemDeviceSensorCalibrationDisplay];
if (systemDeviceSensorCalibrationSetting)
{
if ( [kWTWikitudePlugin_useSystemDeviceSensorCalibrationDispaly_NO isEqualToString:systemDeviceSensorCalibrationSetting] )
{
configuration.shouldUseSystemDeviceSensorCalibrationDisplay = NO;
}
else if ( [kWTWikitudePlugin_useSystemDeviceSensorCalibrationDispaly_YES isEqualToString:systemDeviceSensorCalibrationSetting] )
{
configuration.shouldUseSystemDeviceSensorCalibrationDisplay = YES;
}
}
}
}
}
}
+ (WTFeatures)requiredFeaturesFromArray:(NSArray *)stringArray
{
WTFeatures requiredFeatures = 0;
for (id object in stringArray)
{
if ( [object isKindOfClass:[NSString class]] )
{
NSString *featureString = (NSString *)object;
if ( [featureString isEqualToString:kWTWikitudePlugin_requiredFeature_Geo] )
{
requiredFeatures |= WTFeature_Geo;
}
else if ( [featureString isEqualToString:kWTWikitudePlugin_requiredFeature_ImageTracking] )
{
requiredFeatures |= WTFeature_ImageTracking;
}
else if ( [featureString isEqualToString:kWTWikitudePlugin_requiredFeature_InstantTracking] )
{
requiredFeatures |= WTFeature_InstantTracking;
}
else if ( [featureString isEqualToString:kWTWikitudePlugin_requiredFeature_ObjectTracking] )
{
requiredFeatures |= WTFeature_ObjectTracking;
}
else if ( [featureString isEqualToString:kWTWikitudePlugin_requiredFeature_PhotoLibraryScreenshotImport] )
{
requiredFeatures |= WTFeature_PhotoLibraryScreenshotImport;
}
}
}
return requiredFeatures;
}
+ (NSURL *)architectWorldURLFromString:(NSString *)architectWorldFilePath
{
NSURL *architectWorldURL = nil;
if ( architectWorldFilePath && ![architectWorldFilePath isKindOfClass:[NSNull class]] )
{
NSString *worldName = [architectWorldFilePath lastPathComponent];
worldName = [worldName stringByDeletingPathExtension];
NSString *worldNameExtension = [architectWorldFilePath pathExtension];
NSString *architectWorldDirectoryPath = [architectWorldFilePath stringByDeletingLastPathComponent];
architectWorldURL = [[NSBundle mainBundle] URLForResource:worldName withExtension:worldNameExtension subdirectory:architectWorldDirectoryPath];
}
return architectWorldURL;
}
+ (NSString *)readOptionalPathPrefixFromApplicationPlist
{
NSString *optionalPathPrefix = nil;
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
if ( infoDictionary )
{
if ( [infoDictionary objectForKey:kWTWikitudePlugin_localPathPrefix] )
{
optionalPathPrefix = [infoDictionary objectForKey:kWTWikitudePlugin_localPathPrefix];
}
}
return optionalPathPrefix;
}
+ (NSURL *)addPathPrefix:(NSString *)pathPrefix toArchitectWorldURL:(NSURL *)architectWorldURL
{
if ( pathPrefix && architectWorldURL )
{
NSString *architectWorldString = [architectWorldURL absoluteString];
NSRange wwwRange = [architectWorldString rangeOfString:@"www"];
if ( NSNotFound != wwwRange.location )
{
NSString *leadingPart = [architectWorldString substringToIndex:wwwRange.location];
NSString *trailingPart = [architectWorldString substringFromIndex:wwwRange.location];
NSString *modifiedArchitectWorldString = [leadingPart stringByAppendingPathComponent:pathPrefix];
modifiedArchitectWorldString = [modifiedArchitectWorldString stringByAppendingPathComponent:trailingPart];
NSURL *modifiedArchitectWorldURL = [NSURL URLWithString:modifiedArchitectWorldString];
return modifiedArchitectWorldURL;
}
else
{
return architectWorldURL;
}
}
else
{
return architectWorldURL;
}
}
#pragma mark - Plugin Methods
#pragma mark Device Support
- (void)isDeviceSupported:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
self.isDeviceSupported = NO;
if ( [command.arguments count] >= 1 )
{
if ( [[command.arguments objectAtIndex:0] isKindOfClass:[NSArray class]] )
{
WTFeatures requiredFeatures = [WTWikitudePlugin requiredFeaturesFromArray:[command.arguments objectAtIndex:0]];
NSError *isDeviceSupportedError = nil;
self.isDeviceSupported = [WTArchitectView isDeviceSupportedForRequiredFeatures:requiredFeatures error:&isDeviceSupportedError];
if (self.isDeviceSupported)
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:self.isDeviceSupported];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[isDeviceSupportedError localizedDescription]];
}
}
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)requestAccess:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = nil;
if ( [command.arguments count] >= 1 )
{
id componentsArray = [command.arguments objectAtIndex:0];
if ( [componentsArray isKindOfClass:[NSArray class]] )
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
self.accessRequestCallbackId = command.callbackId;
if ( !_authorizationRequestManager )
{
_authorizationRequestManager = [[WTAuthorizationRequestManager alloc] init];
}
WTFeatures requiredFeatures = [WTWikitudePlugin requiredFeaturesFromArray:(NSArray *)componentsArray];
NSOrderedSet<NSNumber *> *restrictedAppleiOSSDKAPIs = [WTAuthorizationRequestManager restrictedAppleiOSSDKAPIAuthorizationsForRequiredFeatures:requiredFeatures];
[_authorizationRequestManager requestRestrictedAppleiOSSDKAPIAuthorization:restrictedAppleiOSSDKAPIs completion:^(BOOL success, NSError * _Nonnull error) {
if ( success )
{
CDVPluginResult *delayedResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:delayedResult callbackId:self.accessRequestCallbackId];
}
else
{
/* Collect information to show an alert and log a message to the console that explains what is missing and how it can be fixed */
NSDictionary *unauthorizedAPIInfo = [[error userInfo] objectForKey:kWTUnauthorizedAppleiOSSDKAPIsKey];
NSMutableString *detailedAuthorizationErrorLogMessage = [[NSMutableString alloc] initWithFormat:@"The following authorization states do not meet the requirements:"];
NSMutableString *missingAuthorizations = [[NSMutableString alloc] initWithFormat:@"In order to use the Wikitude SDK, please grant access to the following:"];
for (NSString *unauthorizedAPIKey in [unauthorizedAPIInfo allKeys])
{
[missingAuthorizations appendFormat:@"\n* %@", [WTAuthorizationRequestManager humanReadableDescriptionForUnauthorizedAppleiOSSDKAPI:unauthorizedAPIKey]];
[detailedAuthorizationErrorLogMessage appendFormat:@"\n%@ = %@", unauthorizedAPIKey, [WTAuthorizationRequestManager stringFromAuthorizationStatus:[[unauthorizedAPIInfo objectForKey:unauthorizedAPIKey] integerValue] forUnauthorizedAppleiOSSDKAPI:unauthorizedAPIKey]];
}
NSDictionary *cordovaAPIErrorInfo = @{@"userDescription": missingAuthorizations, @"developerDescription": detailedAuthorizationErrorLogMessage};
CDVPluginResult *delayedResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:cordovaAPIErrorInfo];
[self.commandDelegate sendPluginResult:delayedResult callbackId:self.accessRequestCallbackId];
}
}];
}
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)openAppSettings:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
NSURL *appSettingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:appSettingsURL];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark - SDK Informations
- (void)getSDKBuildInformation:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[WTArchitectView SDKBuildInformation] toJSONString]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)getSDKVersion:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[WTArchitectView sdkVersion]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark Plugin Lifecycle
- (void)open:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
if ( self.isDeviceSupported && command.arguments.count )
{
id argumentDictionary = [command.arguments firstObject];
if ( [argumentDictionary isKindOfClass:[NSDictionary class]] )
{
NSDictionary *arguments = (NSDictionary *)argumentDictionary;
NSString *sdkKey = [arguments objectForKey:kWTWikitudePlugin_ArgumentKeyLicenseKey];
NSString *architectWorldURLString = [arguments objectForKey:kWTWikitudePlugin_ArgumentKeyARchitectWorldURL];
WTFeatures requiredFeatures = [WTWikitudePlugin requiredFeaturesFromArray:[arguments objectForKey:kWTWikitudePlugin_ArgumentRequiredFeatures]];
if (!_arViewController)
{
self.arViewController = [[WTArchitectViewController alloc] initWithNibName:nil bundle:nil motionManager:nil];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
#pragma clang diagnostic ignored "-Wundeclared-selector"
if ([self.arViewController.architectView respondsToSelector:@selector(setSDKOrigin:)]) {
[self.arViewController.architectView performSelector:@selector(setSDKOrigin:) withObject:@"ORIGIN_PHONEGAP"];
}
#pragma clang diagnostic pop
[self.arViewController.architectView setLicenseKey:sdkKey];
self.arViewController.modalPresentationStyle = UIModalPresentationFullScreen;
self.arViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.arViewController.architectDelegate = self;
}
self.arViewController.startSDKAfterAppResume = YES;
self.arViewController.architectView.requiredFeatures = requiredFeatures;
[self.viewController presentViewController:self.arViewController animated:YES completion:nil];
[self addNotificationObserver];
NSURL *architectWorldURL = [NSURL URLWithString:architectWorldURLString];
if ( !architectWorldURL || ![architectWorldURL scheme] )
{
architectWorldURL = [WTWikitudePlugin architectWorldURLFromString:architectWorldURLString];
}
if ( architectWorldURL )
{
NSString *optionalPathPrefix = [WTWikitudePlugin readOptionalPathPrefixFromApplicationPlist];
if ( optionalPathPrefix ) {
architectWorldURL = [WTWikitudePlugin addPathPrefix:optionalPathPrefix toArchitectWorldURL:architectWorldURL];
}
self.arViewController.currentArchitectWorldNavigation = [self.arViewController.architectView loadArchitectWorldFromURL:architectWorldURL];
self.loadArchitectWorldCallbackId = command.callbackId;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
}
else
{
self.loadArchitectWorldCallbackId = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[NSString stringWithFormat:@"Unable to determine the url to load: %@", architectWorldURLString]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
__weak WTWikitudePlugin *weakSelf = self;
[self.arViewController.architectView start:^(WTArchitectStartupConfiguration *configuration) {
[WTWikitudePlugin readStartupConfigurationFrom:arguments andApplyTo:configuration];
[WTArchitectStartupConfiguration transferArchitectStartupConfiguration:configuration toArchitectStartupConfiguration:weakSelf.arViewController.startupConfiguration];
} completion:^(BOOL success, NSError *error) {
if ( !success && self.errorHandlerCallbackId )
{
CDVPluginResult *pluginResult = nil;
if ( [@"com.wikitude.architect.AuthorizationStates" isEqualToString:[error domain]] )
{
if ( 960 == [error code] )
{
NSString *missingAuthorizations = [WTWikitudePlugin composeFailingAPIAuthorizationMessageFromError:error];
NSError *detailedError = [NSError errorWithDomain:[error domain] code:[error code] userInfo:@{NSLocalizedDescriptionKey: missingAuthorizations}];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"code": @(detailedError.code), @"message": [detailedError localizedDescription]}];
}
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"code": @(error.code), @"message": [error localizedDescription]}];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.errorHandlerCallbackId];
}
}];
}
}
}
- (void)close:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
if (self.arViewController)
{
[self.arViewController.architectView stop];
[self removeNotificationObserver];
__weak WTWikitudePlugin *weakSelf = self;
[self.viewController dismissViewControllerAnimated:YES completion:^
{
/* nil out the strong reference because it’s not longer needed. ‘show’ and ‘hide’ can handle nil controller and are supposed to be only used during an active presentation of our plugin */
weakSelf.arViewController = nil;
}];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
if (command) {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}
- (void)show:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
if (self.arViewController)
{
[self.arViewController setStartSDKAfterAppResume:YES];
[self.viewController presentViewController:self.arViewController animated:YES completion:nil];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_INVALID_ACTION messageAsString:@"Wikitude Plugin not loaded. You first have to call load and then show."];
NSLog(@"Wikitude Plugin not loaded. You first have to call load and then show.");
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)hide:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
if (self.arViewController)
{
[self.arViewController setStartSDKAfterAppResume:NO];
[self.viewController dismissViewControllerAnimated:YES completion:nil];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)onResume:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
/* Intentionally left blank */
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setBackButtonCallback:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
self.onBackButtonCallbackId = command.callbackId;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)onPause:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
/* Intentionally left blank */
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark Screen Capturing
- (void)captureScreen:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = nil;
if (self.arViewController && [self.arViewController.architectView isRunning])
{
if ( 2 == command.arguments.count ) // only proceed if the two required parameters are given
{
self.screenshotCallbackId = command.callbackId;
WTScreenshotCaptureMode captureMode = [[command.arguments objectAtIndex:0] boolValue] ? WTScreenshotCaptureMode_CamAndWebView : WTScreenshotCaptureMode_Cam;
WTScreenshotSaveMode saveMode;
NSString *screenshotBundlePath = nil;
if ( [[command.arguments objectAtIndex:1] isKindOfClass:[NSString class]] )
{
saveMode = WTScreenshotSaveMode_BundleDirectory;
screenshotBundlePath = [command.arguments objectAtIndex:1];
}
else
{
saveMode = WTScreenshotSaveMode_PhotoLibrary;
}
WTScreenshotSaveOptions options = WTScreenshotSaveOption_SavingWithoutOverwriting | WTScreenshotSaveOption_CallDelegateOnSuccess;
[self.arViewController.architectView captureScreenWithMode:captureMode usingSaveMode:saveMode saveOptions:options context: screenshotBundlePath ? @{kWTScreenshotBundleDirectoryKey: screenshotBundlePath} : nil];
}
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark Location Injection
- (void)setLocation:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
if ( self.arViewController && 4 == command.arguments.count )
{
float latitude = [[command.arguments objectAtIndex:0] floatValue];
float longitude = [[command.arguments objectAtIndex:1] floatValue];
float altitude = [[command.arguments objectAtIndex:2] floatValue];
float accuracy = [[command.arguments objectAtIndex:3] floatValue];
if (!self.isUsingInjectedLocation)
{
[self.arViewController.architectView setUseInjectedLocation:YES];
self.isUsingInjectedLocation = YES;
}
[self.arViewController.architectView injectLocationWithLatitude:latitude longitude:longitude altitude:altitude accuracy:accuracy];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark JavaScript
- (void)callJavascript:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
if ( 1 == command.arguments.count )
{
NSMutableString *javascriptToCall = [[command.arguments objectAtIndex:0] mutableCopy];
if (self.arViewController) {
[self.arViewController.architectView callJavaScript:javascriptToCall];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_INVALID_ACTION messageAsString:@"No JavaScript given to evaluate"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)onUrlInvoke:(CDVInvokedUrlCommand *)command
{
CDVPluginResult* pluginResult = nil;
self.urlInvokedCallbackId = command.callbackId;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)onJSONObjectReceived:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = nil;
self.receivedJSONObjectCallbackId = command.callbackId;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark Device sensor calibration
- (void)setDeviceSensorsNeedCalibrationHandler:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = nil;
self.deviceSensorsNeedCalibrationCallbackId = command.callbackId;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setDeviceSensorsFinishedCalibrationHandler:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = nil;
self.deviceSensorsFinishedCalibrationCallbackId = command.callbackId;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark Error handling
- (void)setErrorHandler:(CDVInvokedUrlCommand *)command
{
CDVPluginResult *pluginResult = nil;
self.errorHandlerCallbackId = command.callbackId;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark - Notifications
#pragma mark WTArchitectViewController
- (void)didReceiveWorldDidLoadNotification:(NSNotification *)aNotification
{
NSURL *worldURL = [[aNotification userInfo] objectForKey:WTArchitectNotificationURLKey];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[worldURL absoluteString]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.loadArchitectWorldCallbackId];
}
- (void)didReceiveWorldDidFailToLoadNotification:(NSNotification *)aNotification
{
NSError *error = [[aNotification userInfo] objectForKey:WTArchitectNotificationErrorKey];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.loadArchitectWorldCallbackId];
}
- (void)didReceiveInvokedURLNotification:(NSNotification *)aNotification
{
CDVPluginResult *pluginResult = nil;
NSURL *url = [[aNotification userInfo] objectForKey:WTArchitectNotificationURLKey];
if (url && self.urlInvokedCallbackId)
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[url absoluteString]];
[pluginResult setKeepCallbackAsBool:YES];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.urlInvokedCallbackId];
}
- (void)didReceiveReceivedJSONObjectNotification:(NSNotification *)aNotification
{
CDVPluginResult *pluginResult = nil;
if (self.receivedJSONObjectCallbackId)
{
NSDictionary *jsonObject = [[aNotification userInfo] objectForKey:WTArchitectNotificationJSONObjectKey];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:jsonObject];
[pluginResult setKeepCallbackAsBool:YES];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.receivedJSONObjectCallbackId];
}
- (void)didReceiveDidCapturedScreenNotification:(NSNotification *)aNotification
{
CDVPluginResult *pluginResult = nil;
if (self.screenshotCallbackId)
{
NSDictionary *context = [[aNotification userInfo] objectForKey:WTArchitectNotificationContextKey];
WTScreenshotSaveMode mode = [[context objectForKey:kWTScreenshotSaveModeKey] integerValue];
NSString *resultMessage = nil;
if (WTScreenshotSaveMode_BundleDirectory == mode)
{
resultMessage = [context objectForKey:kWTScreenshotBundleDirectoryKey];
}
else
{
resultMessage = @"Screenshot was added to the device Photo Library";
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:resultMessage];
[pluginResult setKeepCallbackAsBool:YES];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.screenshotCallbackId];
}
- (void)didReceiveCaptureScreenDidFailNotification:(NSNotification *)aNotification
{
CDVPluginResult *pluginResult = nil;
if (self.screenshotCallbackId)
{
NSError *error = [[aNotification userInfo] objectForKey:WTArchitectNotificationErrorKey];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
[pluginResult setKeepCallbackAsBool:YES];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.screenshotCallbackId];
}
- (void)didReceiveArchitectNeedsDeviceSensorCalibrationNotification:(NSNotification *)aNotification
{
if ( self.deviceSensorsNeedCalibrationCallbackId )
{
CDVPluginResult *pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.deviceSensorsNeedCalibrationCallbackId];
}
}
- (void)didReceiveArchitectFinishedDeviceSensorCalibrationNotification:(NSNotification *)aNotification
{
if ( self.deviceSensorsFinishedCalibrationCallbackId )
{
CDVPluginResult *pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.deviceSensorsFinishedCalibrationCallbackId];
}
}
- (void)didReceiveArchitectDebugMessageNotification:(NSNotification *)aNotification
{
if ( self.errorHandlerCallbackId )
{
CDVPluginResult *pluginResult = nil;
NSError *error = [[aNotification userInfo] objectForKey:WTArchitectDebugDelegateMessageKey];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:@{@"code": @(error.code), @"message": [error localizedDescription]}];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.errorHandlerCallbackId];
}
}
#pragma mark - Delegation
#pragma mark WTARViewControllerDelegate
- (void)architectViewControllerWillDisappear:(WTArchitectViewController *)architectViewController
{
CDVPluginResult* pluginResult = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.onBackButtonCallbackId];
}
#pragma mark - Private Methods
- (void)addNotificationObserver
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveWorldDidLoadNotification:) name:WTArchitectDidLoadWorldNotification object:self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveWorldDidFailToLoadNotification:) name:WTArchitectDidFailToLoadWorldNotification object:self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveInvokedURLNotification:) name:WTArchitectInvokedURLNotification object:self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveReceivedJSONObjectNotification:) name:WTArchitectReceivedJSONObjectNotification object: self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveDidCapturedScreenNotification:) name:WTArchitectDidCaptureScreenNotification object:self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveCaptureScreenDidFailNotification:) name:WTArchitectDidFailToCaptureScreenNotification object:self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveArchitectNeedsDeviceSensorCalibrationNotification:) name:WTArchitectNeedsDeviceSensorCalibration object:self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveArchitectFinishedDeviceSensorCalibrationNotification:) name:WTArchitectFinishedDeviceSensorCalibration object:self.arViewController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveArchitectDebugMessageNotification:) name:WTArchitectDebugDelegateNotification object:self.arViewController];
}
- (void)removeNotificationObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end