Advertisement
Guest User

AppDelegate.m

a guest
Apr 5th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  j2lAppDelegate.m
  3. //  decent
  4. //
  5. //  Created by Lucas Yan on 4/5/14.
  6. //  Copyright (c) 2014 j2l. All rights reserved.
  7. //
  8.  
  9. #import "j2lAppDelegate.h"
  10. #import "j2lPost.h"
  11.  
  12. @implementation j2lAppDelegate
  13.  
  14.  
  15. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  16. {
  17.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  18.     // Override point for customization after application launch.
  19.     self.window.backgroundColor = [UIColor whiteColor];
  20.     [self.window makeKeyAndVisible];
  21.     self.posts = [[NSMutableArray alloc] init];
  22.     return YES;
  23. }
  24.  
  25. - (void)reset
  26. {
  27.     acceptPosts = true;
  28. }
  29.  
  30. - (void)applicationWillResignActive:(UIApplication *)application
  31. {
  32. }
  33.  
  34. - (void)applicationDidEnterBackground:(UIApplication *)application
  35. {
  36.     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  37.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  38.         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  39.     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  40.     NSLog(@"RESIGNACTIVE");
  41.     [acceptReset invalidate];
  42.     [peerAdvertiser stopAdvertisingPeer];
  43.     [peerBrowser stopBrowsingForPeers];
  44.     [self.mainSession disconnect];
  45.     self.mainSession = false;
  46.     self.isBrowsing = false;
  47. }
  48.  
  49. - (void)applicationWillEnterForeground:(UIApplication *)application
  50. {
  51.     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  52.    
  53. }
  54.  
  55. - (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error
  56. {
  57.     self.isBrowsing = false;
  58. }
  59.  
  60. - (void)applicationDidBecomeActive:(UIApplication *)application
  61. {
  62.     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  63.     self.myIdentifier = [[MCPeerID alloc] initWithDisplayName:[self.class createHash:20]];
  64.    
  65.     self.mainSession = [[MCSession alloc] initWithPeer:self.myIdentifier];
  66.     self.mainSession.delegate = self;
  67.    
  68.     peerAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.myIdentifier discoveryInfo:nil serviceType: service];
  69.     peerAdvertiser.delegate = self;
  70.     peerBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.myIdentifier serviceType: service];
  71.     peerBrowser.delegate = self;
  72.    
  73.     acceptReset = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(reset) userInfo:nil repeats:YES];
  74.     acceptPosts = true;
  75.     [peerBrowser startBrowsingForPeers];
  76.     [peerAdvertiser startAdvertisingPeer];
  77.    
  78.     self.isBrowsing = true;
  79. }
  80.  
  81. - (void)applicationWillTerminate:(UIApplication *)application
  82. {
  83.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  84. }
  85.  
  86. #pragma mark MultipeerConnectivity methods
  87.  
  88. - (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
  89. {
  90.     if(self.mainSession == nil)
  91.     {
  92.         NSLog(@"WHY");
  93.     }
  94.     [peerBrowser invitePeer:peerID toSession:self.mainSession withContext:nil timeout:1000];
  95. }
  96.  
  97. - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
  98. {
  99.     if(state == MCSessionStateConnected)
  100.     {
  101.         j2lPost *newPost = [[j2lPost alloc] init];
  102.         newPost.displayName = @"Anonymous";
  103.         newPost.displayPicture = [[UIImage alloc] init];
  104.         newPost.postContent = @"";
  105.         newPost.postPicture = [[UIImage alloc] init];
  106.         NSDictionary *postDictionary = [newPost postToDictionary];
  107.         NSMutableData *data = [[NSMutableData alloc] init];
  108.         NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
  109.         [archiver encodeObject:postDictionary forKey:@"post"];
  110.         [archiver finishEncoding];
  111.         NSError *error;
  112.         [self.mainSession sendData:data toPeers:self.mainSession.connectedPeers withMode:MCSessionSendDataUnreliable error:&error];
  113.        
  114.     }
  115. }
  116.  
  117. - (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
  118. {
  119.     NSLog(@"WAITING");
  120.     if(acceptPosts)
  121.     {
  122.         @try
  123.         {
  124.             NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  125.             NSDictionary *postDictionary = [unarchiver decodeObjectForKey:@"post"];
  126.             if(postDictionary == nil)
  127.             {
  128.                 return;
  129.             }
  130.             [unarchiver finishDecoding];
  131.             j2lPost *newPost = [[j2lPost alloc] initWithDictionary:postDictionary];
  132.             for(j2lPost *existingPost in self.posts)
  133.             {
  134.                 if([existingPost equalToPost:newPost])
  135.                 {
  136.                     return;
  137.                 }
  138.             }
  139.             if(self.posts.count + 1 > limit)
  140.             {
  141.                 [self.posts removeLastObject];
  142.             }
  143.             if(newPost == nil)
  144.             {
  145.                 return;
  146.             }
  147.             [self.posts insertObject:newPost atIndex:0];
  148.             NSLog(newPost.displayName);
  149.         }
  150.         @catch(NSException *e)
  151.         {
  152.         }
  153.     }
  154. }
  155.  
  156. + (NSString *)createHash:(int)length
  157. {
  158.     NSString *alphabet  = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
  159.     NSMutableString *s = [NSMutableString stringWithCapacity:length];
  160.     for (NSUInteger i = 0; i < length; i++)
  161.     {
  162.         u_int32_t r = arc4random() % [alphabet length];
  163.         unichar c = [alphabet characterAtIndex:r];
  164.         [s appendFormat:@"%C", c];
  165.     }
  166.     return s;
  167. }
  168.  
  169. - (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID
  170. {
  171. }
  172.  
  173. - (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
  174.     didReceiveInvitationFromPeer:(MCPeerID *)peerID
  175.        withContext:(NSData *)context
  176.  invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
  177. {
  178.     invitationHandler(YES, self.mainSession);
  179. }
  180. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement