iOS的全局session

最近做項目,要求製作一個全局session

解釋一下,就是軟件在60s無任何操作的時候,會調用手勢鎖屏功能,說實話,這個功能很雞肋,往往要以消耗軟件性能爲代價,下面講講我的方案

思路如下:

製作一個定時器,這個定時器在60秒後會觸發手勢頁面彈出。

但如果用戶有操作,定時器不斷更新時間,即每次操作,都會重置一個60秒

同時我自己覺得這應該是一個單例模式,因爲整個系統只能有一個定時器,否則就亂套了。

現在貼出相關代碼,由於是自己寫的,也許可能並不是很完善。

#import <Foundation/Foundation.h>


@interface PXJNSNotificationTimeOut : NSObject{

    dispatch_queue_t myQueue;

}

@property(atomic,strong)__block NSDate *timeOut;

@property(atomic,strong)NSTimer *timer;

+ (PXJNSNotificationTimeOut *)instance;

- (void)PostSeesionNotification;

@end



#import "PXJNSNotificationTimeOut.h"

#import "GesturePasswordController.h"


#define sessionTime 60.0

static PXJNSNotificationTimeOut *notificationTimeOut = nil;

@implementation PXJNSNotificationTimeOut



- (id)init{

    if (self = [super init]) {

        self.timer = [NSTimer scheduledTimerWithTimeInterval:sessionTime target:self selector:@selector(TimeOutController) userInfo:nil repeats:NO];

    }

    return self;

}


+ (PXJNSNotificationTimeOut *)instance{

    

    if (notificationTimeOut == nil) {

        @synchronized(self) {

            if (notificationTimeOut == nil) {

                notificationTimeOut = [[self alloc] init];

            }

        }

    }

    return notificationTimeOut;

}


- (void)PostSeesionNotification{

    [self.timer invalidate];//runloop中移除,暫停計時器,timer=nil

    self.timer = [NSTimer scheduledTimerWithTimeInterval:sessionTime target:self selector:@selector(TimeOutController) userInfo:nil repeats:NO];

}

//注意最好異步操作,否則程序卡死了

//同時注意dispatch_get_main_queue()很重要,否則容易造成你彈出的頁面不完整,其實是頁面顯示不及時,個人覺得

- (void)TimeOutController{

    dispatch_async(dispatch_get_main_queue(), ^{

        GesturePasswordController *gesture = [[GesturePasswordController alloc] initWithNibName:nil bundle:nil];

        gesture.isDownLoginData = NO;

        gesture.comefromType = otherType;

        gesture.hidesBottomBarWhenPushed = YES;

        UIViewController *vc = [self getCurrentViewController];

        if ([vc isKindOfClass:[GesturePasswordController class]]) {

            

        }else{

            [vc presentViewController:gesture animated:YES completion:NULL];

        }

    });

}

//得到當前頁面,以便於彈出手勢頁面

- (UIViewController *)getCurrentViewController{

    UIViewController *result = nil;

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

    if (window.windowLevel != UIWindowLevelNormal) {

        NSArray *windows = [[UIApplication sharedApplication] windows];

        for (UIWindow *tmpWin in windows) {

            if (tmpWin.windowLevel == UIWindowLevelNormal) {

                window = tmpWin;

                break;

            }

        }

    }

    UIWindow *frontWindow = [[window subviews] objectAtIndex:0];

    id nextResponder = [frontWindow nextResponder];

    if ([nextResponder isKindOfClass:[UIViewController class]]) {

        result = nextResponder;

    }else{

        result = window.rootViewController;

    }

    return result;

}

@end


你以爲這樣就完了嗎?no,no,no

其實更困難的在於布碼,要講這件事情不屬於你的程序相關的所有位置,即用戶的觸發都能調動這件事情,簡直了


所以此時感覺你的程序的所有controller有一個超級基類十分重要,否則,部碼極其困難。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章