c++ioscocoa-touchlockingnslock

How to lock code execution in iOS application


Problem Description

I have a function StdString ShowLockScreen() in this function I call activateViewController function which shows some UI where user must enter PIN, just after calling activateViewController function I want to lock all processes until user will enter his PIN and press OK button on opened UI. Below you can see code which I try

Source code in 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");
}

I want that my code stops then I call [theLock lock]; and after I will call [theLock unlock]; from my UI and code will continue executing. But it doesn't work in my case.

Source code in Android

I have write similar application in Android and here is code. I want to write same in iOS but I can;t find solution

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

Researches

I think I must use

NSCondition* condLock = [[NSCondition alloc] init];
[condLock wait];

and

[condLock signal];

but how to use this in my code ?


Solution

  • Okay I found solution to this issue, below you can see implemented function and line by line description.

    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]!!!")
    }