Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.27 KB | None | 0 0
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. // Override point for customization after application launch.
  5. self.window.backgroundColor = [UIColor whiteColor];
  6.  
  7. UITabBarController *tabbarCtrler = [[UITabBarController alloc]init];
  8.  
  9. MyHomeViewController *myHomeCtrler = [[MyHomeViewController alloc] initWithNibName:@"MyHomeViewController" bundle:NULL];
  10. [myHomeCtrler.tabBarItem setTitle:@"My Home"];
  11.  
  12. ProductsViewController *prodViewCtrler = [[ProductsViewController alloc] initWithNibName:@"ProductsViewController" bundle:NULL];
  13. [prodViewCtrler.tabBarItem setTitle:@"Products"];
  14.  
  15. InboxViewController *inboxViewCtrler = [[InboxViewController alloc] initWithNibName:@"InboxViewController" bundle:NULL];
  16. [inboxViewCtrler.tabBarItem setTitle:@"Inbox"];
  17.  
  18. ContactUSViewController *contactViewCtrler = [[ContactUSViewController alloc] initWithNibName:@"ContactUSViewController" bundle:NULL];
  19. [contactViewCtrler.tabBarItem setTitle:@"Contact Us"];
  20.  
  21. VoiceViewController *voiceViewCtrler = [[VoiceViewController alloc] initWithNibName:@"VoiceViewController" bundle:NULL];
  22. [voiceViewCtrler.tabBarItem setTitle:@"Voice"];
  23.  
  24. UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:myHomeCtrler];
  25. navigationcontroller.title = @"My News;
  26.  
  27. [navigationcontroller.navigationBar setBackgroundImage:[UIImage imageNamed:@"SettingsTitlebar.png"] forBarMetrics:UIBarMetricsDefault];
  28.  
  29. [navigationcontroller setNavigationBarHidden:YES];
  30.  
  31. //create an array of all view controllers that will represent the tab at the bottom
  32. NSArray *arrayViewControllers = [[NSArray alloc] initWithObjects:
  33. navigationcontroller, prodViewCtrler, inboxViewCtrler, contactViewCtrler, voiceViewCtrler, nil];
  34.  
  35. [tabbarCtrler setViewControllers:arrayViewControllers];
  36.  
  37. [self.window makeKeyAndVisible];
  38.  
  39. [self.window setRootViewController:tabbarCtrler];
  40.  
  41. return YES;
  42. }
  43.  
  44. #import "MyHomeViewController.h"
  45. #import "SettingsViewController.h"
  46.  
  47. @interface MyHomeViewController ()
  48.  
  49. @end
  50.  
  51. @implementation MyHomeViewController
  52.  
  53. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  54. {
  55. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  56. if (self) {
  57. // Custom initialization
  58. }
  59. return self;
  60. }
  61.  
  62. - (void)viewDidLoad
  63. {
  64. [super viewDidLoad];
  65. // Do any additional setup after loading the view from its nib.
  66. }
  67.  
  68. - (void)didReceiveMemoryWarning
  69. {
  70. [super didReceiveMemoryWarning];
  71. // Dispose of any resources that can be recreated.
  72. }
  73.  
  74. -(IBAction)goAhead:(id)sender
  75. {
  76. HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
  77. [self.navigationController pushViewController:setttingsViewController animated:YES];
  78. }
  79.  
  80. // HomeViewController.m
  81. //
  82. //
  83.  
  84. #import "HomeViewController.h"
  85. #import "AppDelegate.h"
  86. #import "CobrowseSingletonSocket.h"
  87.  
  88. #import <QuartzCore/QuartzCore.h>
  89. #import <notify.h>
  90.  
  91. @interface HomeViewController ()
  92.  
  93. @end
  94.  
  95. @implementation HomeViewController
  96. @synthesize authTexField;
  97. @synthesize sessionID;
  98. @synthesize sentPing;
  99. @synthesize bScreenOff;
  100. @synthesize responseAlertView;
  101. @synthesize portFld;
  102. @synthesize ipFld;
  103. @synthesize shareScreenTimer;
  104.  
  105. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  106. {
  107. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  108. if (self) {
  109. // Custom initialization
  110. }
  111. return self;
  112. }
  113.  
  114. - (void)viewDidLoad
  115. {
  116. [super viewDidLoad];
  117.  
  118. // Do any additional setup after loading the view from its nib.
  119. authTexField.delegate = self;
  120. responseAlertView.delegate = self;
  121. ipFld.delegate = self;
  122. portFld.delegate = self;
  123. bScreenOff = NO;
  124. cobrowseSingletonIns = [CobrowseSingletonSocket sharedCobrowseSocketInstance];
  125. }
  126.  
  127. -(void) viewWillAppear:(BOOL)animated
  128. {
  129. [super viewWillAppear:animated];
  130.  
  131. [self.navigationController setNavigationBarHidden:NO];
  132. }
  133.  
  134. - (void)didReceiveMemoryWarning
  135. {
  136. [super didReceiveMemoryWarning];
  137. // Dispose of any resources that can be recreated.
  138. }
  139.  
  140. -(IBAction)connectShare:(id)sender
  141. {
  142. // Connect socket freshly here
  143.  
  144. [self initSocketConnection];
  145.  
  146. }
  147.  
  148. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  149. {
  150. [textField resignFirstResponder];
  151.  
  152. return YES;
  153. }
  154.  
  155. - (IBAction)backAction:(id)sender
  156. {
  157. [self dismissModalViewControllerAnimated:YES];
  158. }
  159.  
  160. #pragma mark Socket Connection
  161.  
  162. - (void)initSocketConnection
  163. {
  164. NSString * ipAddrStr = ipFld.text;
  165. NSString * portStr = portFld.text;
  166.  
  167.  
  168. NSLog(@"IPAddress: %@ ; Port: %@", ipAddrStr, portStr);
  169.  
  170. CFReadStreamRef readStream;
  171. CFWriteStreamRef writeStream;
  172.  
  173. NSString *ipaddress = ipAddrStr;
  174.  
  175. ipaddress = [ipaddress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  176.  
  177. NSLog(@"Retrieved socket ip: %@", cobrowseSingletonIns.socketIPAddress);
  178.  
  179. CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)ipaddress, 8081, &readStream, &writeStream);
  180.  
  181. cobrowseSingletonIns.inputStream = (__bridge_transfer NSInputStream *)readStream;
  182. cobrowseSingletonIns.outputStream = (__bridge_transfer NSOutputStream *)writeStream;
  183. [cobrowseSingletonIns.inputStream setDelegate:self];
  184. [cobrowseSingletonIns.outputStream setDelegate:self];
  185.  
  186. [cobrowseSingletonIns.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  187. [cobrowseSingletonIns.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  188. [cobrowseSingletonIns.inputStream open];
  189. [cobrowseSingletonIns.outputStream open];
  190.  
  191. }
  192.  
  193.  
  194. -(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
  195. {
  196. NSString *io;
  197.  
  198. if (theStream == cobrowseSingletonIns.inputStream) io = @">>";
  199. else io = @"<<";
  200.  
  201. NSLog(@"Stream space : %d",[cobrowseSingletonIns.outputStream hasSpaceAvailable]);
  202.  
  203. NSString *event;
  204. switch (streamEvent)
  205. {
  206. case NSStreamEventNone:
  207. event = @"NSStreamEventNone";
  208. //statusText.text = @"Can not connect to the host!";
  209. NSLog(@"NSStreamEventNone - Can not connect to the host");
  210. break;
  211.  
  212. case NSStreamEventOpenCompleted:
  213. event = @"NSStreamEventOpenCompleted";
  214. //pingButton.hidden = NO;
  215. //statusText.text = @"Connected";
  216. NSLog(@"Connected");
  217. break;
  218.  
  219. case NSStreamEventHasBytesAvailable:
  220.  
  221. event = @"NSStreamEventHasBytesAvailable";
  222. NSLog(@"NSStreamEventHasBytesAvailable called");
  223.  
  224. if (theStream == cobrowseSingletonIns.inputStream)
  225. {
  226. //read data
  227. //uint8_t buffer[1024];
  228. uint8_t buffer[2];
  229. NSMutableData *data=[[NSMutableData alloc] init];
  230.  
  231. int len;
  232. while ([cobrowseSingletonIns.inputStream hasBytesAvailable])
  233. {
  234. len = [cobrowseSingletonIns.inputStream read:buffer maxLength:sizeof(buffer)];
  235.  
  236. if(len)
  237. {
  238. [data appendBytes:&buffer length:len];
  239. }
  240. else
  241. {
  242. NSLog(@"no buffer!");
  243. }
  244.  
  245. }
  246. NSString *responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  247. responseStr = [responseStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  248.  
  249. //do something with data
  250. NSLog(@"Server said this-> %@", responseStr);
  251. NSString *successResponse;
  252. responseAlertView = [[UIAlertView alloc] init];
  253. if ( [responseStr isEqualToString:@"SUCCESS"])
  254. {
  255. successResponse = @"Successfully connected! Click OK to starts screen sharing!";
  256. responseAlertView.tag = 3;
  257. [responseAlertView addButtonWithTitle:@"OK"];
  258. [responseAlertView addButtonWithTitle:@"CANCEL"];
  259. }
  260. else
  261. {
  262. successResponse = @"There seems to be problem in connecting..Try connecting it again with proper Random Auth ID!";
  263. responseAlertView.tag = 4;
  264. [responseAlertView addButtonWithTitle:@"OK"];
  265. }
  266. responseAlertView.delegate = self;
  267. [responseAlertView setTitle:@"Cobrowsing"];
  268. [responseAlertView setMessage:successResponse];
  269.  
  270. [responseAlertView show];
  271.  
  272. }
  273. break;
  274.  
  275. case NSStreamEventHasSpaceAvailable:
  276. {
  277. event = @"NSStreamEventHasSpaceAvailable";
  278.  
  279. NSLog(@"space : %d", [cobrowseSingletonIns.outputStream hasSpaceAvailable]);
  280.  
  281.  
  282. if ( !cobrowseSingletonIns.bConnectionEstablished )
  283. {
  284. NSLog(@"NSStreamEventHasSpaceAvailable - Connection established, sharing is going to be established!");
  285.  
  286. if ( theStream == cobrowseSingletonIns.outputStream && !self.sentPing )
  287. {
  288. if ( [sessionID length]<=0 )
  289. sessionID = @"EMPTY";
  290.  
  291. NSLog(@"sessionID : %@", sessionID);
  292. NSData* data = [sessionID dataUsingEncoding:NSUTF8StringEncoding];
  293.  
  294. int num = [cobrowseSingletonIns.outputStream write:[data bytes] maxLength:([data length])];
  295. if (-1 == num) {
  296. NSLog(@"Error writing to stream %@: %@", cobrowseSingletonIns.outputStream, [cobrowseSingletonIns.outputStream streamError]);
  297. }else{
  298. NSLog(@"Wrote %i bytes to stream %@.", num, cobrowseSingletonIns.outputStream);
  299. }
  300. sentPing = YES;
  301. }
  302.  
  303. }
  304. else
  305. {
  306. NSLog(@"NSStreamEventHasSpaceAvailable - Connection already established");
  307.  
  308. if ( [cobrowseSingletonIns.outputStream hasSpaceAvailable] )
  309. {
  310. [self coShareScreen];
  311. }
  312. }
  313. }
  314. break;
  315.  
  316. case NSStreamEventErrorOccurred:
  317. {
  318. event = @"NSStreamEventErrorOccurred";
  319. NSLog(@"NSStreamEventErrorOccurred - Can not connect to the host");
  320. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Co-browsing" message:@"Connection error, Cannot connect to the host!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
  321. alertView.tag = 5;
  322. [alertView show];
  323.  
  324. }
  325. break;
  326.  
  327. case NSStreamEventEndEncountered:
  328. event = @"NSStreamEventEndEncountered";
  329. NSLog(@"NSStreamEventEndEncountered - Connection closed by the server");
  330. break;
  331.  
  332. default:
  333. event = @"** Unknown";
  334. }
  335.  
  336. NSLog(@"%@ : %@", io, event);
  337. }
  338.  
  339. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  340. {
  341. int tag = alertView.tag;
  342.  
  343. NSLog(@"buttonIndex: %d ; alertView.tag: %d", buttonIndex, tag);
  344.  
  345. if ( alertView.tag==3 )
  346. {
  347. if ( buttonIndex==0 ) // for OK button
  348. {
  349. sentPing = NO;
  350.  
  351. cobrowseSingletonIns.bConnectionEstablished = YES;
  352. [cobrowseSingletonIns shareScreen]; // call just once here, then 5 mins thread caller will be called in hasspaceavailable delegate method.
  353. }
  354. else if ( buttonIndex==1 ) // for Cancel button
  355. {
  356. NSLog(@"User selected Cancel, just stop the socket connection");
  357. [cobrowseSingletonIns.outputStream close];
  358. [cobrowseSingletonIns.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  359. }
  360.  
  361. }
  362. else if (alertView.tag==4)
  363. {
  364. NSLog(@"Problem connecting with socket, just stop the socket connection");
  365. sentPing = NO;
  366. [cobrowseSingletonIns.outputStream close];
  367. [cobrowseSingletonIns.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  368. }
  369. else if (alertView.tag==5) // Socket connection closed Abruptly, one of the reasons, user manually locked of the phone. In this case, logout and love to login
  370. {
  371. NSLog(@"Socket connection closed Abruptly due to unknown reasons");
  372. }
  373. }
  374.  
  375. //-(void) shareScreen :(NSTimer *) timerInfo
  376. -(void) coShareScreen
  377. {
  378. NSLog(@"coShareScreen called");
  379.  
  380. [cobrowseSingletonIns shareScreen];
  381. }
  382.  
  383. @end
  384.  
  385. //
  386. // CobrowseSingletonSocket.m
  387. //
  388.  
  389. #import "CobrowseSingletonSocket.h"
  390. #import "AppDelegate.h"
  391. #import "MyUSAAViewController.h"
  392. #import "USAASettingsViewController.h"
  393. #import "ProductsViewController.h"
  394. #import "InboxViewController.h"
  395. #import "ContactUSViewController.h"
  396. #import "VoiceViewController.h"
  397. #import <QuartzCore/QuartzCore.h>
  398.  
  399. @implementation CobrowseSingletonSocket
  400.  
  401. static CobrowseSingletonSocket *sharedCobrowseSocketInstance = nil;
  402.  
  403. @synthesize loginViewController;
  404. @synthesize outputStream;
  405. @synthesize inputStream;
  406. @synthesize bConnectionEstablished;
  407. @synthesize socketIPAddress;
  408. @synthesize servletIPAddress;
  409. @synthesize servletPort;
  410.  
  411. +(CobrowseSingletonSocket *) sharedCobrowseSocketInstance
  412. {
  413. @synchronized ([CobrowseSingletonSocket class])
  414. {
  415. if ( !sharedCobrowseSocketInstance )
  416. {
  417. sharedCobrowseSocketInstance = [[super allocWithZone:NULL] init];
  418. }
  419. }
  420. return sharedCobrowseSocketInstance;
  421. }
  422.  
  423.  
  424. + (id)allocWithZone:(NSZone *)zone
  425. {
  426. return [self sharedCobrowseSocketInstance];
  427. }
  428.  
  429. -(void) takeScreenshotSend :(NSString *) endOrCancelString
  430. {
  431. AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  432. UIGraphicsBeginImageContext(appDelegate.window.bounds.size);
  433. [appDelegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
  434. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  435. UIGraphicsEndImageContext();
  436. NSMutableData *data = [NSMutableData data];
  437. data = (NSMutableData *) UIImagePNGRepresentation(image);
  438. //[data writeToFile:@"screenshot.png" atomically:YES];
  439.  
  440. NSLog(@"shareScreen [data length] %i: ", [data length]);
  441.  
  442. NSData *newData = [endOrCancelString dataUsingEncoding:NSUTF16StringEncoding];
  443. [data appendData:newData];
  444. NSLog(@"shareScreen [data length] %i: ", [data length]);
  445.  
  446. //sentPing = YES;
  447.  
  448. int num = [self.outputStream write:[data bytes] maxLength:([data length])];
  449. if (-1 == num) {
  450. NSLog(@"Error writing to stream %@: %@", self.outputStream, [self.outputStream streamError]);
  451.  
  452. }else{
  453. NSLog(@"Wrote %i bytes to stream %@.", num, self.outputStream);
  454. //[self.outputStream close];
  455. }
  456.  
  457. }
  458.  
  459. -(void) shareScreenAtInterval
  460. {
  461. NSLog(@"Screen sharing going to happen!");
  462. [self takeScreenshotSend:@"END"]; // appending END, to detect the same on the server side and get out of reading data loop there.
  463.  
  464. }
  465.  
  466. -(void) shareScreen
  467. {
  468. NSLog(@"shareScreen called!");
  469. [self performSelector:@selector(shareScreenAtInterval) withObject:nil afterDelay:5.0];
  470. }
  471.  
  472.  
  473. -(void) disconnectSocket
  474. {
  475. NSLog(@"Close the socket connection by user");
  476.  
  477. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(shareScreenAtInterval) object: nil];
  478. //[NSObject cancelPreviousPerformRequestsWithTarget:self];
  479.  
  480. // Send Cancel message to socket
  481. [self takeScreenshotSend:@"CANCEL"]; // appending CANCEL, to detect the same on the server side and get out of reading data loop there.
  482.  
  483. [self.outputStream close];
  484. [self.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  485. self.outputStream = nil;
  486.  
  487. self.bConnectionEstablished = NO;
  488.  
  489. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Co-browsing" message:@"Screen sharing disconnected!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
  490. [alertView show];
  491.  
  492. AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  493. [appDelegate.window.rootViewController dismissModalViewControllerAnimated:YES];
  494.  
  495. }
  496.  
  497. @end
  498.  
  499. [cobrowseSingletonIns.inputStream setDelegate:self];
  500. [cobrowseSingletonIns.outputStream setDelegate:self]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement