iOS锁屏或home键后luasocket断开解决方案

版本: V3.16

frameworks/runtime-src/proj.ios_mac/ios/AppController.mm

#import "AppController.h"

#import "cocos2d.h"

#import "AppDelegate.h"

#import "RootViewController.h"

@implementation AppController

@synthesize window;

UIBackgroundTaskIdentifier _backIden;

NSInteger count;

NSTimer *_mTimer;

#pragma mark -

#pragma mark Application lifecycle

// cocos2d application instance

static AppDelegate s_sharedApplication;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    cocos2d::Application *app = cocos2d::Application::getInstance();


    // Initialize the GLView attributes

    app->initGLContextAttrs();

    cocos2d::GLViewImpl::convertAttrs();


    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.

    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];

    // Use RootViewController to manage CCEAGLView

    _viewController = [[RootViewController alloc]init];

    _viewController.wantsFullScreenLayout = YES;

    // Set RootViewController to window

    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)

    {

        // warning: addSubView doesn't work on iOS6

        [window addSubview: _viewController.view];

    }

    else

    {

        // use this method on ios6

        [window setRootViewController:_viewController];

    }

    [window makeKeyAndVisible];

    [UIApplication  sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

//    NativeApplication.nativeApplication.executeInBackground = true;

    [[UIApplication sharedApplication] setIdleTimerDisabled: YES];// 禁止ios设备锁屏

    // IMPORTANT: Setting the GLView should be done after creating the RootViewController

    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)_viewController.view);

    cocos2d::Director::getInstance()->setOpenGLView(glview);

    //run the cocos2d-x game scene

    app->run();

    return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application {

    /*

    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.

    */

    // We don't need to call this method any more. It will interrupt user defined game pause&resume logic

    /* cocos2d::Director::getInstance()->pause(); */

}

- (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.

    */

    // We don't need to call this method any more. It will interrupt user defined game pause&resume logic

    /* cocos2d::Director::getInstance()->resume(); */

}

- (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, called instead of applicationWillTerminate: when the user quits.

    */

    _mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:_mTimer forMode:NSRunLoopCommonModes];

    [self beginTask];


//    cocos2d::Application::getInstance()->applicationDidEnterBackground();

}

//计时

-(void)countAction{

    NSLog(@"%li",count++);

}

//申请后台

-(void)beginTask

{

    NSLog(@"begin=============");

    _backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        //在时间到之前会进入这个block,一般是iOS7及以上是3分钟。按照规范,在这里要手动结束后台,你不写也是会结束的(据说会crash)

        NSLog(@"将要挂起=============");

        [self endBack];

    }];

}

//注销后台

-(void)endBack

{

    NSLog(@"end=============");

    [_mTimer invalidate];

    _mTimer = nil;


    cocos2d::Application::getInstance()->applicationDidEnterBackground();


//    [[UIApplication sharedApplication] endBackgroundTask:_backIden];

//    _backIden = UIBackgroundTaskInvalid;

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    /*

    Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.

    */

    NSLog(@"进入前台");

    [self endBack];


    cocos2d::Application::getInstance()->applicationWillEnterForeground();

}

- (void)applicationWillTerminate:(UIApplication *)application {

    /*

    Called when the application is about to terminate.

    See also applicationDidEnterBackground:.

    */

}

#pragma mark -

#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

    /*

    Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.

    */

}

#if __has_feature(objc_arc)

#else

- (void)dealloc {

    [window release];

    [_viewController release];

    [super dealloc];

}

#endif

@end



版本: V3.3

多加两行代码,把之前的绘制屏弃掉,不然会崩溃。

...

-(void)beginTask

{

    NSLog(@"begin=============");

    cocos2d::Director::getInstance()->stopAnimation();

    _backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        //在时间到之前会进入这个block,一般是iOS7及以上是3分钟。按照规范,在这里要手动结束后台,你不写也是会结束的(据说会crash)

        NSLog(@"将要挂起=============");

        [self endBack];

    }];

}

...

- (void)applicationWillEnterForeground:(UIApplication *)application {

    /*

    Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.

    */

    NSLog(@"进入前台");

    cocos2d::Director::getInstance()->startAnimation();

     [self endBack];

    cocos2d::Application::getInstance()->applicationWillEnterForeground();


}

你可能感兴趣的:(iOS锁屏或home键后luasocket断开解决方案)