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

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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值