Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // j2lAppDelegate.m
- // decent
- //
- // Created by Lucas Yan on 4/5/14.
- // Copyright (c) 2014 j2l. All rights reserved.
- //
- #import "j2lAppDelegate.h"
- #import "j2lPost.h"
- @implementation j2lAppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- self.posts = [[NSMutableArray alloc] init];
- return YES;
- }
- - (void)reset
- {
- acceptPosts = true;
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- // 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.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- // 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.
- // 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.
- NSLog(@"RESIGNACTIVE");
- [acceptReset invalidate];
- [peerAdvertiser stopAdvertisingPeer];
- [peerBrowser stopBrowsingForPeers];
- [self.mainSession disconnect];
- self.mainSession = false;
- self.isBrowsing = false;
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- // 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.
- }
- - (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error
- {
- self.isBrowsing = false;
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- // 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.
- self.myIdentifier = [[MCPeerID alloc] initWithDisplayName:[self.class createHash:20]];
- self.mainSession = [[MCSession alloc] initWithPeer:self.myIdentifier];
- self.mainSession.delegate = self;
- peerAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.myIdentifier discoveryInfo:nil serviceType: service];
- peerAdvertiser.delegate = self;
- peerBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.myIdentifier serviceType: service];
- peerBrowser.delegate = self;
- acceptReset = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(reset) userInfo:nil repeats:YES];
- acceptPosts = true;
- [peerBrowser startBrowsingForPeers];
- [peerAdvertiser startAdvertisingPeer];
- self.isBrowsing = true;
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- }
- #pragma mark MultipeerConnectivity methods
- - (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
- {
- if(self.mainSession == nil)
- {
- NSLog(@"WHY");
- }
- [peerBrowser invitePeer:peerID toSession:self.mainSession withContext:nil timeout:1000];
- }
- - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
- {
- if(state == MCSessionStateConnected)
- {
- j2lPost *newPost = [[j2lPost alloc] init];
- newPost.displayName = @"Anonymous";
- newPost.displayPicture = [[UIImage alloc] init];
- newPost.postContent = @"";
- newPost.postPicture = [[UIImage alloc] init];
- NSDictionary *postDictionary = [newPost postToDictionary];
- NSMutableData *data = [[NSMutableData alloc] init];
- NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
- [archiver encodeObject:postDictionary forKey:@"post"];
- [archiver finishEncoding];
- NSError *error;
- [self.mainSession sendData:data toPeers:self.mainSession.connectedPeers withMode:MCSessionSendDataUnreliable error:&error];
- }
- }
- - (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
- {
- NSLog(@"WAITING");
- if(acceptPosts)
- {
- @try
- {
- NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
- NSDictionary *postDictionary = [unarchiver decodeObjectForKey:@"post"];
- if(postDictionary == nil)
- {
- return;
- }
- [unarchiver finishDecoding];
- j2lPost *newPost = [[j2lPost alloc] initWithDictionary:postDictionary];
- for(j2lPost *existingPost in self.posts)
- {
- if([existingPost equalToPost:newPost])
- {
- return;
- }
- }
- if(self.posts.count + 1 > limit)
- {
- [self.posts removeLastObject];
- }
- if(newPost == nil)
- {
- return;
- }
- [self.posts insertObject:newPost atIndex:0];
- NSLog(newPost.displayName);
- }
- @catch(NSException *e)
- {
- }
- }
- }
- + (NSString *)createHash:(int)length
- {
- NSString *alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
- NSMutableString *s = [NSMutableString stringWithCapacity:length];
- for (NSUInteger i = 0; i < length; i++)
- {
- u_int32_t r = arc4random() % [alphabet length];
- unichar c = [alphabet characterAtIndex:r];
- [s appendFormat:@"%C", c];
- }
- return s;
- }
- - (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID
- {
- }
- - (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
- didReceiveInvitationFromPeer:(MCPeerID *)peerID
- withContext:(NSData *)context
- invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
- {
- invitationHandler(YES, self.mainSession);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement