PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/iOS/AugmentedReality-Wikitude/SampleProjects/Extended/HelloImageRecognition/HelloImageRecognition/Plugins/Wikitude/WTWikitudePlugin.m

https://bitbucket.org/rfrick81/phonegap-plugins
Objective C | 402 lines | 252 code | 126 blank | 24 comment | 19 complexity | edc89988bd7a316a6fd21336b5e4fb6d MD5 | raw file
  1. //
  2. // WTWikitudeSDK.m
  3. // HelloWorld
  4. //
  5. // Created by Andreas Schacherbauer on 8/24/12.
  6. //
  7. //
  8. #import "WTWikitudePlugin.h"
  9. // Wikitude SDK
  10. #import "WTArchitectView.h"
  11. @interface WTWikitudePlugin () <WTArchitectViewDelegate>
  12. @property (nonatomic, strong) WTArchitectView *architectView;
  13. @property (nonatomic, strong) NSString *currentARchitectViewCallbackID;
  14. @property (nonatomic, strong) NSString *currentPlugInErrorCallback;
  15. @property (nonatomic, assign) BOOL isUsingInjectedLocation;
  16. @end
  17. @implementation WTWikitudePlugin
  18. @synthesize architectView=_architectView;
  19. #pragma mark - View Lifecycle
  20. /* View Lifecycle */
  21. - (void)isDeviceSupported:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  22. {
  23. NSString* callbackId = [arguments objectAtIndex:0];
  24. CDVPluginResult* pluginResult = nil;
  25. NSString* javaScript = nil;
  26. @try {
  27. // NSString* echo = [arguments objectAtIndex:1];
  28. BOOL isDeviceSupported = [WTArchitectView isDeviceSupported];
  29. if (isDeviceSupported) {
  30. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:isDeviceSupported];
  31. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  32. } else {
  33. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:isDeviceSupported];
  34. javaScript = [pluginResult toErrorCallbackString:callbackId];
  35. }
  36. } @catch (NSException* exception) {
  37. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  38. javaScript = [pluginResult toErrorCallbackString:callbackId];
  39. }
  40. [self writeJavascript:javaScript];
  41. }
  42. - (void)open:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  43. {
  44. NSString* callbackId = [arguments objectAtIndex:0];
  45. CDVPluginResult* pluginResult = nil;
  46. NSString* javaScript = nil;
  47. @try {
  48. // NSString* echo = [arguments objectAtIndex:1];
  49. BOOL success = [WTArchitectView isDeviceSupported];
  50. if ( success ) {
  51. NSString *sdkKey = [options objectForKey:@"apiKey"];
  52. NSString *architectWorldFilePath = [options objectForKey:@"filePath"];
  53. // First, lets check if we need to init a new sdk view
  54. if ( !_architectView ) {
  55. self.architectView = [[WTArchitectView alloc] initWithFrame:self.viewController.view.bounds];
  56. self.architectView.delegate = self;
  57. [self.architectView initializeWithKey:sdkKey motionManager:nil];
  58. }
  59. // then add the view in its own navController to the ui. we need a own navController to have a backButton which can be used to dismiss the view
  60. UIViewController *viewController = [[UIViewController alloc] init];
  61. viewController.view = self.architectView;
  62. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  63. navController.navigationBar.tintColor = [UIColor blackColor];
  64. navController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissARchitectView)];
  65. [self.viewController presentViewController:navController animated:NO completion:^{
  66. // completion code
  67. }];
  68. // and finaly load the architect world, specified in the open function in js
  69. if (architectWorldFilePath) {
  70. NSString *worldName = [architectWorldFilePath lastPathComponent];
  71. worldName = [worldName stringByDeletingPathExtension];
  72. NSString *worldNameExtension = [architectWorldFilePath pathExtension];
  73. NSString *architectWorldDirectoryPath = [architectWorldFilePath stringByDeletingLastPathComponent];
  74. NSString *loadablePath = [[NSBundle mainBundle] pathForResource:worldName ofType:worldNameExtension inDirectory:architectWorldDirectoryPath];
  75. [self.architectView loadArchitectWorldFromUrl:loadablePath];
  76. }
  77. }
  78. // start the sdk view updates
  79. [self.architectView start];
  80. if ( success ) {
  81. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  82. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  83. } else {
  84. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
  85. javaScript = [pluginResult toErrorCallbackString:callbackId];
  86. }
  87. } @catch (NSException* exception) {
  88. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  89. javaScript = [pluginResult toErrorCallbackString:callbackId];
  90. }
  91. [self writeJavascript:javaScript];
  92. }
  93. - (void)close:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  94. {
  95. NSString* callbackId = [arguments objectAtIndex:0];
  96. CDVPluginResult* pluginResult = nil;
  97. NSString* javaScript = nil;
  98. @try {
  99. // NSString* echo = [arguments objectAtIndex:1];
  100. if (self.architectView) {
  101. [self.architectView stop];
  102. // [self.architectView removeFromSuperview];
  103. }
  104. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  105. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  106. } @catch (NSException* exception) {
  107. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  108. javaScript = [pluginResult toErrorCallbackString:callbackId];
  109. }
  110. [self writeJavascript:javaScript];
  111. }
  112. - (void)show:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  113. {
  114. NSString* callbackId = [arguments objectAtIndex:0];
  115. CDVPluginResult* pluginResult = nil;
  116. NSString* javaScript = nil;
  117. @try {
  118. // NSString* echo = [arguments objectAtIndex:1];
  119. if (self.architectView) {
  120. self.architectView.hidden = NO;
  121. }
  122. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  123. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  124. } @catch (NSException* exception) {
  125. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  126. javaScript = [pluginResult toErrorCallbackString:callbackId];
  127. }
  128. [self writeJavascript:javaScript];
  129. }
  130. - (void)hide:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  131. {
  132. NSString* callbackId = [arguments objectAtIndex:0];
  133. CDVPluginResult* pluginResult = nil;
  134. NSString* javaScript = nil;
  135. @try {
  136. // NSString* echo = [arguments objectAtIndex:1];
  137. if (self.architectView) {
  138. self.architectView.hidden = YES;
  139. }
  140. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  141. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  142. } @catch (NSException* exception) {
  143. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  144. javaScript = [pluginResult toErrorCallbackString:callbackId];
  145. }
  146. [self writeJavascript:javaScript];
  147. }
  148. - (void)onResume:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  149. {
  150. NSString* callbackId = [arguments objectAtIndex:0];
  151. CDVPluginResult* pluginResult = nil;
  152. NSString* javaScript = nil;
  153. @try {
  154. // NSString* echo = [arguments objectAtIndex:1];
  155. if (self.architectView) {
  156. [self.architectView start];
  157. }
  158. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:nil];
  159. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  160. } @catch (NSException* exception) {
  161. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  162. javaScript = [pluginResult toErrorCallbackString:callbackId];
  163. }
  164. [self writeJavascript:javaScript];
  165. }
  166. - (void)onPause:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  167. {
  168. NSString* callbackId = [arguments objectAtIndex:0];
  169. CDVPluginResult* pluginResult = nil;
  170. NSString* javaScript = nil;
  171. @try {
  172. // NSString* echo = [arguments objectAtIndex:1];
  173. if (self.architectView) {
  174. [self.architectView stop];
  175. }
  176. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:nil];
  177. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  178. } @catch (NSException* exception) {
  179. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  180. javaScript = [pluginResult toErrorCallbackString:callbackId];
  181. }
  182. [self writeJavascript:javaScript];
  183. }
  184. #pragma mark - Location Handling
  185. - (void)setLocation:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  186. {
  187. NSString* callbackId = [arguments objectAtIndex:0];
  188. CDVPluginResult* pluginResult = nil;
  189. NSString* javaScript = nil;
  190. @try {
  191. // NSString* echo = [arguments objectAtIndex:1];
  192. if (self.architectView) {
  193. float latitude = [[options objectForKey:@"lat"] floatValue];
  194. float longitude = [[options objectForKey:@"lon"] floatValue];
  195. float altitude = [[options objectForKey:@"alt"] floatValue];
  196. float accuracy = [[options objectForKey:@"acc"] floatValue];
  197. if (!self.isUsingInjectedLocation) {
  198. [self.architectView setUseInjectedLocation:YES];
  199. self.isUsingInjectedLocation = YES;
  200. }
  201. [self.architectView injectLocationWithLatitude:latitude longitude:longitude altitude:altitude accuracy:accuracy];
  202. }
  203. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
  204. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  205. } @catch (NSException* exception) {
  206. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  207. javaScript = [pluginResult toErrorCallbackString:callbackId];
  208. }
  209. [self writeJavascript:javaScript];
  210. }
  211. #pragma mark - Javascript
  212. - (void)callJavascript:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  213. {
  214. NSString* callbackId = [arguments objectAtIndex:0];
  215. CDVPluginResult* pluginResult = nil;
  216. NSString* javaScript = nil;
  217. @try {
  218. if (arguments.count >= 1) {
  219. NSMutableString *javascriptToCall = [[arguments objectAtIndex:1] mutableCopy];
  220. for (NSUInteger i = 2; i < arguments.count; i++) {
  221. [javascriptToCall appendString:[arguments objectAtIndex:i]];
  222. }
  223. if (self.architectView) {
  224. [self.architectView callJavaScript:javascriptToCall];
  225. }
  226. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nil];
  227. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  228. }else
  229. {
  230. // return error no javascript to call found
  231. }
  232. } @catch (NSException* exception) {
  233. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  234. javaScript = [pluginResult toErrorCallbackString:callbackId];
  235. }
  236. [self writeJavascript:javaScript];
  237. }
  238. - (void)onUrlInvoke:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options
  239. {
  240. NSString* callbackId = [arguments objectAtIndex:0];
  241. CDVPluginResult* pluginResult = nil;
  242. NSString* javaScript = nil;
  243. @try {
  244. self.currentARchitectViewCallbackID = callbackId;
  245. self.currentPlugInErrorCallback = callbackId;
  246. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
  247. [pluginResult setKeepCallbackAsBool:YES];
  248. javaScript = [pluginResult toSuccessCallbackString:callbackId];
  249. } @catch (NSException* exception) {
  250. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[exception reason]];
  251. javaScript = [pluginResult toErrorCallbackString:callbackId];
  252. }
  253. [self writeJavascript:javaScript];
  254. }
  255. #pragma mark - WTArchitectView Delegate
  256. - (void)urlWasInvoked:(NSString *)url
  257. {
  258. CDVPluginResult *pluginResult = nil;
  259. NSString *javaScriptResult = nil;
  260. if (url && self.currentARchitectViewCallbackID) {
  261. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:url];
  262. [pluginResult setKeepCallbackAsBool:YES];
  263. javaScriptResult = [pluginResult toSuccessCallbackString:self.currentARchitectViewCallbackID];
  264. }else
  265. {
  266. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
  267. javaScriptResult = [pluginResult toSuccessCallbackString:self.currentPlugInErrorCallback];
  268. }
  269. [self writeJavascript:javaScriptResult];
  270. }
  271. - (void)dismissARchitectView
  272. {
  273. [self.viewController dismissModalViewControllerAnimated:NO];
  274. [self.architectView stop];
  275. }
  276. @end