# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bayun.gitbook.io/bayuncoresdk-ios/5.-bayuncoresdk-methods/5.1-file-locking-unlocking.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
