Guest User

Untitled

a guest
May 26th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. #import <UIKit/UIKit.h>
  2.  
  3.  
  4. @interface GAGlobal : NSObject {
  5. NSDictionary *settings;
  6. }
  7.  
  8. - (void)fillSettings;
  9. - (void)checkSettings;
  10. - (NSString *) getStringSetting:(NSString *)key;
  11. - (BOOL) getBoolSetting:(NSString *)key;
  12. - (void)setValue:(id)value forKey:(NSString *)key;
  13. - (void)setBool:(int)value forKey:(NSString *)key;
  14. - (NSString *)MD5fromString:(NSString *)string;
  15. - (void)sendString:(NSString *)cadena;
  16.  
  17. @end
  18.  
  19.  
  20. ------------------------
  21.  
  22.  
  23. #import "GAGlobal.h"
  24. #import "Socket.h"
  25. #import <CommonCrypto/CommonDigest.h>
  26.  
  27. @implementation GAGlobal
  28.  
  29.  
  30. - (void)init{
  31. NSLog(@"INICIANDO GLOBAL");
  32. [self fillSettings];
  33. }
  34.  
  35. - (void)fillSettings{
  36. NSFileManager *fileManager = [NSFileManager defaultManager];
  37.  
  38. NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
  39. BOOL existe = [fileManager fileExistsAtPath:path];
  40.  
  41. if (!existe){
  42. NSLog(@"No existe Directorio");
  43. mkdir([[NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()] UTF8String], 0755);
  44. }else{
  45. NSLog(@"Existe Directorio");
  46. NSLog(path);
  47. }
  48.  
  49. NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
  50. existe = [fileManager fileExistsAtPath:filePath];
  51. //NSLog(@"FilePath: %@",filePath);
  52.  
  53. NSString *stdFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Config.plist"];
  54. //NSLog(stdFilePath);
  55.  
  56. NSDictionary *stdSettings = [[NSDictionary alloc] initWithContentsOfFile:stdFilePath];
  57. //NSDictionary *settings = nil;
  58.  
  59. if (!existe){
  60. NSLog(@"No existe Archivo Config");
  61. //mkdir([[NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()] UTF8String], 0755);
  62. [stdSettings writeToFile:filePath atomically:YES];
  63. settings = stdSettings;
  64. }else{
  65. NSLog(@"Existe Archivo Config");
  66. //NSLog(filePath);
  67. settings = [[NSDictionary alloc] initWithContentsOfFile:filePath];
  68. }
  69. }
  70.  
  71. //verifica que todas las configuraciones esten disponibles
  72. - (void)checkSettings{
  73. NSLog(@"Checando Configuraciones");
  74.  
  75.  
  76. NSString *strServerIP = [settings objectForKey:@"SERVER-IP"];
  77. NSString *strServerPort = [settings objectForKey:@"SERVER-PORT"];
  78. NSString *strRemoteIP = [settings objectForKey:@"REMOTE-IP"];
  79. NSString *strRemotePort = [settings objectForKey:@"REMOTE-PORT"];
  80. NSString *strVideoURL = [settings objectForKey:@"VIDEO-URL"];
  81. NSString *strVideoPort = [settings objectForKey:@"VIDEO-PORT"];
  82. NSString *strVideoUser = [settings objectForKey:@"VIDEO-USER"];
  83. NSString *strVideoPass = [settings objectForKey:@"VIDEO-PASS"];
  84.  
  85. if (strServerIP == nil || strServerPort == nil || strRemoteIP == nil || strRemotePort==nil || strVideoURL == nil || strVideoPort == nil || strVideoUser == nil || strVideoPass==nil){
  86. NSLog(@"Faltan Settings");
  87. UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Algunas de las configuraciones hacen falta, para el buen funcionamiento de la aplicacion, rellene los campos faltantes en la parte de Configuracion" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
  88. //UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
  89. //[myTextField setBackgroundColor:[UIColor whiteColor]];
  90. //[myAlertView addSubview:testTextField];
  91. [myAlertView show];
  92. [myAlertView release];
  93. }
  94. //*/
  95. }
  96.  
  97. - (NSString *)getStringSetting:(NSString *)key{
  98.  
  99. NSString *setting = [settings objectForKey:key];
  100. return setting;
  101.  
  102. }
  103.  
  104. - (BOOL) getBoolSetting:(NSString *)key{
  105. BOOL setting = [[settings valueForKey:key] boolValue];
  106. return setting;
  107. }
  108.  
  109. - (int) getIntegerSetting:(NSString *)key{
  110. int setting = [[settings valueForKey:key] intValue];
  111. return setting;
  112. }
  113.  
  114. - (void)setValue:(id)value forKey:(NSString *)key{
  115. NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
  116. NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
  117. NSLog(@"Inicio de guardar valor %@,%@",value,key);
  118. [settings setValue:value forKey:key];
  119. NSLog(@"escribir %@",filePath);
  120. [settings writeToFile:filePath atomically:YES];
  121. NSLog(@"Fin Guardar");
  122. }
  123.  
  124. - (void)setBool:(int)value forKey:(NSString *)key{
  125. NSString *path = [NSString stringWithFormat:@"%@/iControl", NSHomeDirectory()];
  126. NSString *filePath = [path stringByAppendingString:@"/Config.plist"];
  127. NSLog(@"Inicio de guardar valor %d,%@",value,key);
  128. [settings setValue:[NSNumber numberWithInt:value] forKey:key];
  129. //NSLog(@"escribir %@",filePath);
  130. [settings writeToFile:filePath atomically:YES];//*/
  131. NSLog(@"Fin Guardar");
  132. }
  133.  
  134. - (void)sendString:(NSString *)cadena
  135. {
  136. //NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
  137. BOOL demoMode = [self getBoolSetting:@"DEMO-MODE"];
  138. NSLog(@"Demo: %d",demoMode);
  139.  
  140. if (!demoMode){
  141. @try{
  142. Socket *socket = [Socket socket];
  143. [socket connectToHostName:[self getStringSetting:@"SERVER-IP"] port:[self getIntegerSetting:@"SERVER-PORT"]];
  144. [socket writeString:cadena];
  145. NSLog(@"Envio la cadena %@\n",cadena);
  146. NSMutableData *response = [NSMutableData data];
  147. while ([socket readData:response] <= 0) {
  148. /* each read appends to data,
  149. returns number of bytes read or 0 on EOF*/
  150. }
  151.  
  152. [[NSFileHandle fileHandleWithStandardOutput] writeData:response];
  153. NSString *respuesta = [[NSString alloc] initWithData:response encoding:4];
  154. NSLog(@"Recibo respuesta: %@\n", respuesta);
  155. [socket release];
  156. }
  157. @catch (NSException *exeption){
  158. NSLog(@"Error al intentar la conexion");
  159. UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Hubo un error al intentar conectarse al servidor" delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
  160. //UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
  161. //[myTextField setBackgroundColor:[UIColor whiteColor]];
  162. //[myAlertView addSubview:testTextField];
  163. [myAlertView show];
  164. [myAlertView release];
  165. }
  166. }
  167. }
  168.  
  169. - (NSString *) MD5fromString:(NSString *)string{
  170. NSString *md5 = @"";
  171.  
  172. const char *cStr = [string UTF8String];
  173. unsigned char result[CC_MD5_DIGEST_LENGTH];
  174. CC_MD5( cStr, strlen(cStr), result );
  175. md5 = [NSString stringWithFormat:
  176. @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  177. result[0], result[1], result[2], result[3], result[4],
  178. result[5], result[6], result[7],
  179. result[8], result[9], result[10], result[11], result[12],
  180. result[13], result[14], result[15]
  181. ];
  182.  
  183. return md5;
  184. }
  185.  
  186. @end
Add Comment
Please, Sign In to add comment