# 6.2 Lock/Unlock Text

## &#x36;**.2.1 Lock Text**

The `lockText:success:failure` method of `BayunCore` class locks text with default `encryption-policy` and `key-generation-policy` dictated by server settings.

**Method parameters :**

* **text** : Text to be locked.
  * dataType : `NSString`
* **success** : Success block to be executed after text is successfully locked, returns locked text.
* **failure** : Failure block to be executed if locking fails, returns `BayunError`.

{% tabs %}
{% tab title="Objective-C" %}

```objectivec
[[BayunCore sharedInstance] lockText:@"plain text" success:^(NSString *lockedText) {
    NSLog(@"Text locked successfully.");
} failure:^(BayunError errorCode) {
    NSLog(@"Error locking text.");
}];
```

{% endtab %}

{% tab title="Swift" %}

```swift
BayunCore .sharedInstance().lockText("plain text", success: { (lockedText) in
    NSLog("Text locked successfully.")
 }, failure:  { (bayunErrorCode) in
    NSLog("Error locking text.");
 })
```

{% endtab %}
{% endtabs %}

## **6.2.2 Lock Text with** Encryption Policy, Key Generation Policy

The `lockText:encryptionPolicy:keyGenerationPolicy:groupId:success:failure` method with encryption-policy as an optional parameter locks text with the encryption key dictated by the policy. The method takes the following parameters :

* **text** : Text to be locked.
  * dataType : `NSString`
* **encryptionPolicy** : [BayunEncryptionPolicy](https://docs.bayunsystems.com/bayuncoresdk-ios/5.-bayuncoresdk-methods#encryption-policy) determines the key for locking.
* **keyGenerationPolicy** : [BayunKeyGenerationPolicy](https://docs.bayunsystems.com/bayuncoresdk-ios/5.-bayuncoresdk-methods#key-generation-policy) determines the policy to generate the data encryption key.
* **groupId** : GroupId is required if encryptionPolicy is `BayunEncryptionPolicyGroup`.
* **success** : Success block to be executed after text is successfully locked, returns locked text.
* **failure** : Failure block to be executed if locking fails, returns `BayunError`.

{% hint style="info" %}
If encryption-policy is other than `BayunEncryptionPolicyGroup` then groupId should be nil.
{% endhint %}

{% tabs %}
{% tab title="Objective-C" %}

```objectivec
[[BayunCore sharedInstance] lockText:@"plain text"
                    encryptionPolicy:encryptionPolicy
                 keyGenerationPolicy:(BayunKeyGenerationPolicy)keyGenerationPolicy
                             groupId:groupId
                             success:^(NSString *lockedText)
      NSLog("Text locked successfully.") ;                   
} failure:^(BayunError errorCode) {
      NSLog(@"Error locking text.");
}];
```

{% endtab %}

{% tab title="Swift" %}

```swift
BayunCore.sharedInstance().lockText("plain text", encryptionPolicy: .company, groupId: nil, success: { (lockedText) in
    NSLog("Text locked successfully.")         
}, failure: { (bayunErrorCode) in
    NSLog("Error locking text.");            
})
```

{% endtab %}
{% endtabs %}

## **6.2.3 Unlock Text**

The `unlockText` method of `BayunCore` class unlocks a locked text. The method takes the following parameters :

* **text** : Text to be unlocked.
  * dataType : `NSString`
* **success** : Success block to be executed after text is successfully unlocked, returns unlocked text.
* **failure** : Failure block to be executed if unlocking fails, returns `BayunError`.

{% tabs %}
{% tab title="ObjectiveC" %}

```objectivec
[[BayunCore sharedInstance] unlockText:@"locked text" success:^(NSString *unlockedText) {
    NSLog(@"Text unlocked successfully.");
} failure:^(BayunError errorCode) {
    NSLog(@"Error unlocking text.");
}];
```

{% endtab %}

{% tab title="Swift" %}

```swift
BayunCore.sharedInstance().unlockText("lockedText", success: { (unlockedText) in
    NSLog("Text unlocked successfully.")
}, failure: { (bayunErrorCode) in
    NSLog("Error unlocking text.")
})
```

{% endtab %}
{% endtabs %}
