Guest User

Untitled

a guest
Jan 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. #import <UIKit/UIKit.h>
  2. #import <GameKit/GameKit.h>
  3.  
  4.  
  5. @class TashouNoEnViewController;
  6.  
  7. @interface TashouNoEnAppDelegate : NSObject <UIApplicationDelegate,GKSessionDelegate,GKPeerPickerControllerDelegate> {
  8. UIWindow *window;
  9. TashouNoEnViewController *viewController;
  10. GKSession *mySession;
  11. IBOutlet UITextField *messageTextField;
  12. UIBackgroundTaskIdentifier backgroundTaskIdentifer;
  13. }
  14.  
  15. @property (nonatomic, retain) IBOutlet UIWindow *window;
  16. @property (nonatomic, retain) IBOutlet TashouNoEnViewController *viewController;
  17.  
  18. @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
  19. @end
  20.  
  21. #import "TashouNoEnAppDelegate.h"
  22. #import "TashouNoEnViewController.h"
  23. #define kSessionID @"_tashounoen"
  24.  
  25. @implementation TashouNoEnAppDelegate
  26.  
  27. @synthesize window;
  28. //=_window;
  29. @synthesize viewController;
  30. @synthesize navigationController=_navigationController;
  31.  
  32. NSString *message102;
  33.  
  34. NSString* a_home_dir;
  35. NSString* a_doc_dir;
  36. NSString* a_path;
  37. NSString *switchcheck;
  38. NSInteger myselfint;
  39. NSInteger swithchcheckint;
  40. NSInteger runcount;
  41. NSInteger runcount2;
  42. NSInteger totalruncount;
  43. UIBackgroundTaskIdentifier bgTask;
  44. NSTimer *caallTimer;
  45. NSTimer *caallTimer2;
  46. NSString *message100;
  47. NSString *message200;
  48.  
  49. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  50.  
  51. // Override point for customization after app launch
  52. [window addSubview:viewController.view];
  53. [window makeKeyAndVisible];
  54. }
  55.  
  56.  
  57. - (void)dealloc {
  58. [viewController release];
  59. [window release];
  60. [super dealloc];
  61. }
  62.  
  63.  
  64. - (void)applicationWillResignActive:(UIApplication *)application
  65. {
  66. /**
  67. NSLog(@"applicationWillResignActive");
  68. // NSLog(@"%s", __PRETTY_FUNCTION__);
  69. UIApplication* app = [UIApplication sharedApplication];
  70. NSAssert(backgroundTaskIdentifer == UIBackgroundTaskInvalid, nil);
  71. backgroundTaskIdentifer = [app beginBackgroundTaskWithExpirationHandler:^{
  72.  
  73. // NSLog(@"expired!");
  74. dispatch_async(dispatch_get_main_queue(), ^{
  75. if (backgroundTaskIdentifer != UIBackgroundTaskInvalid) {
  76. [app endBackgroundTask:backgroundTaskIdentifer];
  77. backgroundTaskIdentifer = UIBackgroundTaskInvalid;
  78. }
  79. });
  80. }];
  81. **/
  82. }
  83.  
  84. - (void)applicationDidEnterBackground:(UIApplication *)application
  85. {
  86. self.window.rootViewController = self.navigationController;
  87. runcount=0;
  88. totalruncount=0;
  89. NSLog(@"applicationDidEnterBackground");
  90. caallTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(CAALL2) userInfo:nil repeats:NO];
  91.  
  92. void (^handler)(void) = ^{
  93. bgTask = UIBackgroundTaskInvalid;
  94. };
  95. bgTask = [application beginBackgroundTaskWithExpirationHandler:handler];
  96. [application setKeepAliveTimeout:600 handler:^{
  97.  
  98.  
  99. bgTask = [application beginBackgroundTaskWithExpirationHandler:handler];
  100. [caallTimer invalidate];
  101. caallTimer = nil;
  102.  
  103. caallTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(CAALL2) userInfo:nil repeats:NO];
  104.  
  105.  
  106. }];
  107.  
  108. }
  109.  
  110. - (void)applicationWillEnterForeground:(UIApplication *)application
  111. {
  112.  
  113.  
  114. NSLog(@"applicationWillEnterForeground");
  115. }
  116.  
  117. - (void)applicationDidBecomeActive:(UIApplication *)application
  118. {
  119.  
  120.  
  121. NSLog(@"applicationDidBecomeActive");
  122. }
  123.  
  124. - (void)applicationWillTerminate:(UIApplication *)application
  125. {
  126.  
  127.  
  128. NSLog(@"applicationWillTerminate");
  129. }
  130.  
  131. - (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error {
  132. }
  133.  
  134. - (void)session:(GKSession *)session didFailWithError:(NSError *)error {
  135.  
  136. }
  137.  
  138. - (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
  139.  
  140. NSError *error;
  141. if(![mySession acceptConnectionFromPeer:peerID error:&error]) {
  142. } else {
  143.  
  144. }
  145. }
  146.  
  147.  
  148. -(void)CAALL2
  149.  
  150. {
  151.  
  152.  
  153. [mySession disconnectFromAllPeers];
  154. [mySession release];
  155. mySession = nil;
  156. mySession = [[GKSession alloc] initWithSessionID:kSessionID displayName:nil sessionMode:GKSessionModePeer];
  157. mySession.delegate = self;
  158. [mySession setDataReceiveHandler:self withContext:nil];
  159. mySession.available = YES;
  160.  
  161. caallTimer = [NSTimer scheduledTimerWithTimeInterval:500 target:self selector:@selector(CAALL2) userInfo:nil repeats:NO];
  162. // if (totalruncount<21){
  163.  
  164. // }
  165.  
  166. }
  167. - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
  168. switch (state) {
  169. case GKPeerStateAvailable:
  170. NSLog(@"GKPeerStateAvailable");
  171. [mySession connectToPeer:peerID withTimeout:31.0f];
  172. break;
  173. case GKPeerStateUnavailable:
  174. break;
  175. case GKPeerStateConnected:
  176. [self btnSend:peerID];
  177. break;
  178. case GKPeerStateDisconnected:
  179. break;
  180. case GKPeerStateConnecting:
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186.  
  187. - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {
  188.  
  189.  
  190. message100 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  191. // NSLog(@"frompeerid%@",peer);
  192. // NSLog(@"get%@",message100);
  193. int checkdata = [message100 intValue ];
  194. if (checkdata==111)
  195. {
  196. message100 =@"111";
  197. UIApplication *app = [UIApplication sharedApplication];
  198. UILocalNotification *notification = [[UILocalNotification alloc] init];
  199. NSDate *notificationDate = [NSDate dateWithTimeIntervalSinceNow:0];
  200. notification.soundName = UILocalNotificationDefaultSoundName;
  201. notification.fireDate = notificationDate;
  202. notification.timeZone = [NSTimeZone systemTimeZone];
  203. notification.alertBody = message100;
  204. [app scheduleLocalNotification:notification];
  205. [notification release];
  206. }
  207. else if (checkdata==110)
  208. {
  209. message100 =@"110";
  210. UIApplication *app = [UIApplication sharedApplication];
  211. UILocalNotification *notification = [[UILocalNotification alloc] init];
  212. NSDate *notificationDate = [NSDate dateWithTimeIntervalSinceNow:0];
  213. notification.soundName = UILocalNotificationDefaultSoundName;
  214. notification.fireDate = notificationDate;
  215. notification.timeZone = [NSTimeZone systemTimeZone];
  216. notification.alertBody = message100;
  217. [app scheduleLocalNotification:notification];
  218. [notification release];
  219. }
  220.  
  221. }
  222.  
  223. - (void) mySendDataToPeers:(NSData *) data:(NSString *)peerID
  224. {
  225. NSError* error = nil;
  226. NSLog(@"senddata");
  227. if (mySession)
  228. NSLog(@"mypeerid%@",peerID);
  229.  
  230. [mySession sendData:data toPeers:[NSArray arrayWithObject:peerID] withDataMode:GKSendDataReliable error:&error];
  231. if (error) {
  232. NSLog(@"%@", error);
  233. }
  234. }
  235.  
  236.  
  237. -(void) btnSend:(NSString *)peerID{
  238.  
  239. a_home_dir = NSHomeDirectory();
  240. NSString* a_doc_dir = [a_home_dir stringByAppendingPathComponent:@"Documents"];
  241. a_path = [a_doc_dir stringByAppendingPathComponent:@"userdata.plist"];
  242.  
  243. NSDictionary *plist2 = [NSDictionary dictionaryWithContentsOfFile:a_path];
  244. switchcheck =[plist2 objectForKey:@"switchcheck"];
  245. swithchcheckint = [switchcheck intValue];
  246.  
  247. if(swithchcheckint==1)
  248. {
  249. message102 =@"111";
  250. }
  251. else if(swithchcheckint==0)
  252. {
  253. message102 =@"110";
  254. }
  255. //---convert an NSString object to NSData---
  256. NSData* data;
  257. [messageTextField setText:message102];
  258. data = [message102 dataUsingEncoding:NSUTF8StringEncoding];
  259. [self mySendDataToPeers:data:peerID];
  260. }
  261.  
  262.  
  263. @end
Add Comment
Please, Sign In to add comment