如何在iOS应用程序中锁定代码执行
How to lock code execution in iOS application
问题描述
我在这个函数中有一个函数StdString ShowLockScreen()
,我称之为activateViewController
函数,它显示了一些用户必须输入PIN的UI,在调用activateViewController
函数后,我想锁定所有进程,直到用户输入PIN并在打开的UI上按下OK按钮。下面你可以看到我尝试的代码
iOS中的源代码
StdString ShowLockScreen()
{
// Create a lock.
NSLock* theLock = [[NSLock alloc] init];
// Create a UI in which user must enter his PIN.
PinLockController* controller = [[PinLockController alloc] initWithStyle:PinLockTypeSet];
// Set delegate.
controller.delegate = m_Context;
// !!! Here I show a UI and just after that I lock my lock in order code stop executing there.
[controller activateViewController:nil];
@synchronized(theLock) {
[theLock lock];
}
NSLog(@"User in UI unlock the lock");
}
我希望我的代码停止,然后我调用[theLock lock];
,然后我将调用[theLock unlock];从我的UI和代码将继续执行。但这对我来说不起作用。
Android中的源代码
我已经在安卓系统中编写了类似的应用程序,下面是代码。我想在iOS中写同样的东西,但我可以;无法找到解决方案
Intent intent = new Intent(mCtx, SoftPinActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
SharedObject lock = new SharedObject("");
int lockId = SharedObject.acquireLockId(lock);
Logger.i(TAG, "lockId = " + lockId);
intent.putExtra(SharedObject.LOCK_ID, lockId);
intent.putExtra(SoftPinActivity.UI_ID, style);
synchronized (lock) {
mCtx.startActivity(intent);
try {
Logger.i(TAG, "lock.wait()...");
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
Logger.i(TAG, "InterruptedException");
}
}
Logger.i(TAG, "lock.wait()...done");
SharedObject.releaseLockId(lockId);
String pin = lock.object();
研究
我想我必须使用
NSCondition* condLock = [[NSCondition alloc] init];
[condLock wait];
和
[condLock signal];
但是如何在我的代码中使用它呢?
问题答案
您可以使用NSLock
锁定线程,但在您的情况下,这似乎不适用。原因是锁定主要用于在从多个线程访问数据时提供线程安全性。您所要求的是域级锁,它可以防止用户使用该应用程序,除非他们输入了PIN。这两个概念共用一个词"锁",但它们在实现上完全不同。如果您要使用NSLock
及其相关的对应程序,那么您将强制实现到单独的线程中,纯粹是为了阻止用户交互,并有可能使您的项目复杂化和调试困难(死锁很多?)。
建议的解决方案
由于这个概念是一个域级别的锁定机制,我建议我们保持这种方式来实现它。如果你想让它类似于Android,那么你需要创建自己的"SharedObject"概念,其他一切都可以查询。如果这个对象说"用户没有解锁应用程序",那么什么都不会处理。这使您远离手动管理线程,并在您真正需要它们的时候释放线程(例如异步处理)。
为了实现这个对象,我们将其称为UserContext
,它可以作为单例使用。如何实现这个sharedInstance可以在这里看到。
一旦你有了它,你就可以向它添加各种在整个应用程序中全局的属性(根据名称的建议,它拥有属于特定用户的所有全局属性)。其中一个属性是用户是否锁定了应用程序:
[[UserContext sharedInstance] isLocked] // Returns BOOL
在整个应用程序中使用它,您就可以控制(在域概念级别)一个方法是否可以计算某些东西(当然,您需要使UserContext
线程安全,因为它可以在任何时候在任何地方查询)。它会让阅读代码的开发人员清楚地知道,除非用户解锁了应用程序,否则某个方法什么都做不了。停止
旁注
我希望我的代码停止,然后我调用
[theLock lock];
,然后我将从UI调用[theLock unlock];
,代码将继续执行。
在任何情况下都不要锁定UI线程在发布的应用程序中,看门狗会杀死你的应用程序,它实际上会崩溃。
ViTo,正如我所关心的NSLock一样,我们在多线程的情况下使用它,在多线程中,我们锁定特定的线程,并强制它在没有解锁的情况下,没有其他线程变为活动线程或执行他所需的任务。因此,我们可以做的可能是,首先,我们以线程的形式启动您的所有进程,在这一点上,当您尝试打开UI时,我们称之为"锁定",当用户在输入文本框后按下按钮时,我们则称之为"解锁"。但是,为此,我们必须确保这个线程具有高优先级。这就是我现在的想法,但请在我的示例代码中尝试一下,并将相应地更新您。
检查该部分代码:
+(void)aMethod:(id)param{
int x;
for(x=0;x<50;++x)
{enter code here
[lock lock];
printf("Object Thread says x is %in",x);
usleep(1);
[lock unlock];
}
}
- (void)viewDidLoad
{
int x;
lock = [[NSLock alloc] init];
[NSThread detachNewThreadSelector:@selector(aMethod:) toTarget:[MViewController class] withObject:nil];
for(x=0;x<50;++x)
{
[lock lock];
printf("Main thread says x is %in",x);
usleep(10000);
printf("Main thread lets go %in",x);
[lock unlock];
usleep(100);
}
printf("Now getting the process");
[super viewDidLoad];
}
检查日志,你会得到你想要的。希望,这正是你所需要的。如果有任何顾虑,请对我大喊大叫。
好的,我找到了这个问题的解决方案,下面你可以看到实现的函数和逐行的描述。
StdString ShowLockScreen()
{
// Create NSCondition lock object.
NSCondition* conditionLock = [[NSCondition alloc] init];
// Here I create my UI which must ask user to enter PIN.
PinLockController* controller = [[PinLockController alloc] initWithStyle:PinLockTypeSet];
controller.delegate = m_Context;
// Here I lock the thread but not main thread (this is very important) I start
// ShowLockScreen function in new thread and lock it.
[conditionLock lock];
dispatch_sync(dispatch_get_main_queue(), ^{
// I call function which shows my UI in main thread as UI can be shown
// only in MAIN THREAD. (This is important too.)
[controller ShowLockController:conditionLock];
});
// Then I set lock to wait, how you can see I pass conditionLock as an
// argument to ShowLockController function in that function when user
// enter his PIN and press okay button I call [conditionLock signal];
// and my code code here after wait and continue executing.
[conditionLock wait];
NSLog(@"Come here then I call [conditionLock signal]!!!")
}
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 带有示波器/代码框的静音/锁定
- 如何在iOS应用程序中锁定代码执行
- 从C 中的Mutex锁定代码中的函数返回的好方法
- 在设计良好的代码中,您应该期望锁定weak_ptr始终成功
- 用于锁定键盘和鼠标的代码
- 为什么开发人员在多线程代码的读/写过程中锁定字长数据
- 编译器在发布中优化了我的部分代码,即使返回值被照顾并锁定到if语句
- 用于防止代码中死锁的锁定策略和技术