我不知道这是否是最好的解决方案,是否会对您有所帮助。对我来说,做到这一点的方法是开设一个单例课程。您应该在应用程序启动时使用 Bool var 溢价初始化他。此时,您应该使用所需的所有数据初始化该类:最喜欢的行数,最后一天的评论,...。
在每次“高级操作”之前,您应该使用以下方法:BOOL authorized = [[AuthorizeSingleton sharedmanager] operation]
. 在这里,您将获得所有需要的测试,以了解他是否可以执行高级操作。
每次有人想要执行高级操作时,您都应该从 viewController 访问这个单例。如果返回为 NO,则弹出错误消息,在其他情况下,您执行操作。
如果用户是高级用户,总是返回是。
快速编码类似的东西
这里的.h
#import <Foundation/Foundation.h>
@interface AuthorizeSingleton : NSObject
@property (strong, nonatomic) NSNumber* premium;
@end
这里是 .m #import AuthorizedSingleton.h
AuthorizeSingleton* _sharedInstance=nil;
@interface AuthorizeSingleton ()
@property (strong, nonatomic) NSDate* timestamp;
@property (strong, nonatomic) NSNumber* numberOfcomentary;
@end
@implementation AuthorizeSingleton
@synthesize timestamp=_timestamp, numberOfcomentary=_numberOfcomentary;
-(id)init{
if (self == [super init]) {
//Here you should take data from your persistence(NSUSerDefaults or something like that) Here I initialize at 0
_timestamp=[[NSDate alloc] init];
_numberOfcomentary= [NSNumber numberWithInt:0];
}
return self;
}
+(AuthorizeSingleton*)sharedInstance{
if (!_sharedInstance) {
_sharedInstance = [[AuthorizeSingleton alloc] init];
}
return _sharedInstance;
}
-(BOOL)shouldDoComentary{
NSDate* today= [[NSDate alloc] init];
NSTimeInterval interval = [_timestamp timeIntervalSinceDate: today];
if (interval>60*60*24) {
_timestamp=today;
_numberOfcomentary= [NSNumber numberWithInt:0];
}
if (_numberOfcomentary.integerValue>5 && !_premium.boolValue) {
return NO;
}
return YES;
}
@end
我不测试它,但这就是想法。您从需要授权的地方调用课程,例如
BOOL auth = [[AuthorizedSingleton sharedInstance] shouldDoComentary]
if(!auth){
//show error
}
else{
//do action
}