# 6.1 Lock/Unlock File

## &#x36;**.1.1 Lock File**

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

**Method parameters :**

* **filePath** : File path to be locked.
  * dataType : `NSURL`
* **success** : Success block to be executed after file is successfully locked.
* **failure** : Failure block to be executed if file locking fails, returns `BayunError`.

The file at the given file path is overwritten with the locked file. If file locking fails original file is not changed.

{% tabs %}
{% tab title="Objective-C" %}
```objectivec
NSURL *fileURL = [NSURL fileURLWithPath:@"your file path"];

[[BayunCore sharedInstance] lockFile:fileURL success:^{
    NSLog(@"File locked successfully.");
} failure:^(BayunError errorCode) {
    NSLog(@"Error locking file.");
}];
```
{% endtab %}

{% tab title="Swift" %}
```swift

let fileURL : NSURL = NSURL(fileURLWithPath:"file path")
           
BayunCore.sharedInstance().lockFile(fileURL, success: {
   NSLog("File locked successfully.")             
}, failure: { (bayunErrorCode) in
   NSLog("Error locking file.")
})
```
{% endtab %}
{% endtabs %}

## **6.1.2 Lock File** with Encryption Policy, Key Generation Policy

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

* **fileURL** : File path to be locked.
* **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 file is successfully locked.
* **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="ObjectiveC" %}
```objectivec
NSURL *fileURL = [NSURL fileURLWithPath:@"your file path"];

[[BayunCore sharedInstance] lockFile:fileURL
                    encryptionPolicy:encryptionPolicy
                    keyGenerationPolicy:keyGenerationPolicy
                             groupId:groupId
                             success:^{
      NSLog(@"File locked successfully.");                   
} failure:^(BayunError errorCode) {
      NSLog(@"Error locking file.");
}];
```
{% endtab %}

{% tab title="Swift" %}
```swift
let fileURL = URL(fileURLWithPath: "file path")           

BayunCore.sharedInstance().lockFile(fileURL, encryptionPolicy: .company, groupId: "", success: {
  NSLog("File unlocked successfully.")  
}, failure: { (bayunErrorCode) in
   NSLog("Error unlocking file.")             
})
```
{% endtab %}
{% endtabs %}

## **6.1.3 Unlock File**

The `unlockFile` method of `BayunCore` class unlocks a locked file. The method takes the following parameters :

* **filePath** : File path to be unlocked.
  * dataType : `NSURL`
* **success** : Success block to be executed after file is successfully unlocked.
* **failure** : Failure block to be executed if file unlocking fails, returns `BayunError`.

The file at the given file path is overwritten with the unlocked file.

{% tabs %}
{% tab title="ObjectiveC" %}
```objectivec
NSURL *fileURL = [NSURL fileURLWithPath:@"your file path"];

[[BayunCore sharedInstance] unlockFile:fileURL success:^{
    NSLog(@"File unlocked successfully.");
} failure:^(BayunError errorCode) {
    NSLog(@"Error unlocking file.");
}];
```
{% endtab %}

{% tab title="Swift" %}
```swift
let fileURL : NSURL = NSURL(fileURLWithPath:"file path")

BayunCore.sharedInstance().unlockFile(fileURL, success: {
   NSLog("File unlocked successfully.")
}, failure: { (bayunErrorCode) in
   NSLog("Error unlocking file.")
})
```
{% endtab %}
{% endtabs %}
