# Step 2: Basic Integration

***

## How to use AppConsentUnified

### 1. Get your AppKey

The first step is to create your source / notice and retrieve your generated **YOUR\_APP\_KEY** from AppConsent :[ https://app.appconsent.io](https://app.appconsent.io)

<figure><img src="https://4229351976-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkyFlZKFaKP4MUM0zILOg%2Fuploads%2F66oLF36aIOado64LfHVW%2FAppKey_Android_SDK.png?alt=media&#x26;token=3095d05c-203c-4317-af96-4b457111c6c9" alt=""><figcaption></figcaption></figure>

### 2. Initialise AppConsentUnified

The second step is to initialise the SDK\
\
Our AppConsentUnified iOS SDK supports the following target:

* iOS, (iPhone, iPad and macOS through mac Catalyst) minimum deployment target 13.0

Our AppConsentUnified framework is built in Swift and UIKit, and supported integration within a Swift and Objective-C app.  It is possible to use within a SwiftUI app following various approaches (one of them is in the example below)

{% hint style="warning" %}
**INFO**\
AppConsentUnified SDK supports the new App Tracking Transparency framework (> iOS 14+ available). You must register the **NSUserTrackingUsageDescription key in your Info.plist** of your application otherwise your app will crash. \
\
See [App Tracking Transparency](https://docs.sfbx.io/ios-api-reference/documentation/appconsent/apptrackingtransparency) for details.
{% endhint %}

{% tabs %}
{% tab title="UIKit (Swift)" %}
{% hint style="info" %}
**INFO**

In the example below, the code focuses on using the SDK **exclusively**.
{% endhint %}

<pre class="language-swift"><code class="lang-swift">import AppConsentUnified

class ExampleViewController: UIViewController {

   // AppConsent Object
    private(set) var appConsent: ACNotice!
    
<strong>    override func viewDidLoad() {
</strong>        super.viewDidLoad()
       
        self.appConsent = ACNotice(withAppKey: "YOUR_APP_KEY")
                
        // Present CMP when you need it (it can after some internal logic on your end not necessarely here as the example shows)
        self.appConsent.presentNotice(viewController: self)
    }
}   
</code></pre>

{% endtab %}

{% tab title="UIKit (Objective-C)" %}
{% hint style="info" %}
**INFO**\
\
In the example below, the code focuses on using the SDK **exclusively**.
{% endhint %}

First import our Swift header declaration&#x20;

```objectivec
#import <AppConsentUnified/AppConsentUnified-Swift.h>
#import <AppConsentUnified/ACNotice.h>
```

If you are mixing Swift + Objective-C you will need to important as well your `"App-Swift.h"` file. \
\
For example:&#x20;

```objectivec
#import "YourAppModule-Swift.h"
```

Once this is done, here below a basic implementation:

```objectivec
#import <UIKit/UIKit.h>
#import <AppConsentUnified/ACNotice.h>
#import <AppConsentUnified/AppConsentUnified-Swift.h>

NS_ASSUME_NONNULL_BEGIN
@interface ExampleObjcViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
```

Create a property ACNotice in your implementation file (.m)

```objectivec
#import "ExampleObjcViewController.h"

@interface ExampleObjcViewController ()
@property(nonatomic, strong) ACNotice *appConsent;
@end

@implementation ExampleObjcViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // initialise notice
    self.appConsent = [[ACNotice alloc] initWithAppKey:@"YOUR_APP_KEY"];
    // Present Notice
    [self.appConsent presentNoticeWithViewController:self];
}
@end

```

{% endtab %}

{% tab title="SwiftUI" %}

{% hint style="info" %}
**INFO**\
\
Our SDK is developed with UIKit therefore it requires some adjustments for SwiftUI. \
\
For this example below we showcased an integration using top keyWindow. In order to achieve it, some extension class have been created.\
\
You can other ways of integrating our SDK, for example using **UIViewRepresentable**.&#x20;
{% endhint %}

*(Optional)* First create a ViewModel in order to encapsulate all business logic from our SDK into a view model that conform to observable protocol. Later this object will be use within the view&#x20;

```swift
final class AppConsentViewModel: ObservableObject {
    
    private(set) var appConsent: ACNotice

    init(ac: ACNotice) {
        self.appConsent = ac
    }
    
    func presentNotice() {
        if let rootVC = UIApplication.shared.keyWindow?.rootViewController {
            appConsent.presentNotice(viewController: rootVC)
        } else {
            noticeError = NSError(domain: "your_bundle_id", 
            code: 999, // Choose your internal code if needed
            userInfo: [
            NSLocalizedFailureReasonErrorKey: 
            "Unable to present notice, keyWindow is nil"
            ])
        }
    }
}
```

*(Optional)* If you are using Scene Delegate or Scenes, then (if not already done on your end) create a small extension in order to be able to grab the proper keyWindow.

```swift
extension UIApplication {
    var keyWindow: UIWindow? {
        if #available(iOS 15.0, *) {
            return connectedScenes
                .compactMap { $0 as? UIWindowScene }
                .first(where: { $0.activationState == .foregroundActive })?
                .windows
                .first(where: { $0.isKeyWindow })
        } else {
            // iOS 13–14 fallback
            return windows.first { $0.isKeyWindow }
        }
    }
}
```

Your final View should looks like this and is fully implemented&#x20;

```swift
import SwiftUI
import AppConsentUnified

struct ContentView: View {
    @StateObject private var appConsentViewModel: AppConsentViewModel
    
    init(appConsentViewModel: AppConsentViewModel) {
        self._appConsentViewModel = StateObject(wrappedValue: appConsentViewModel)
    }
    var body: some View {
        VStack {
            // YOUR VIEW ELEMENTS
            Text("My View")
        }.onAppear {
            let appConsent = ACNotice(withAppKey: "YOUR_APP_KEY")
            // present notice
            appConsentViewModel.presentNotice()
        }
    }
}
```

{% endtab %}
{% endtabs %}
