> ## Documentation Index
> Fetch the complete documentation index at: https://documentation.qonversion.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift iOS SDK (beta)

> Install Qonversion Swift SDK to implement in-app subscriptions, validate user receipts, get subscription analytics, and third-party integrations.

We are now working on the brand-new Swift SDK for iOS. While it's still in the early stages and has limited features, we are actively developing it to provide users with a truly exceptional experience.

## Supported and developing features

| Feature                               | Readiness |
| :------------------------------------ | :-------- |
| Analytics Mode                        | ✅         |
| User Properties                       | ✅         |
| Remote Configurations and Experiments | ✅         |
| Subscription Management Mode          | 👨🏻‍💻   |

## Install SDK

Qonversion supports Cocoapods and Swift Package Manager to package your dependencies as a framework.

### Install via Cocoapods

Add dependency to `Podfile`.

```text Podfile theme={null}
pod 'Qonversion'
```

Run `pod install` in the project directory to download the dependency.

```shell Terminal theme={null}
pod install
```

### Install via Apple Swift Package Manager

Open Xcode, go to **File → App packages...** and enter the package URL `https://github.com/qonversion/qonversion-ios-sdk` to the search bar to import the package.

## Initialize SDK

For now, Qonversion Swift SDK supports only [analytics launch mode](/docs/quickstart).

```swift theme={null}
import Qonversion

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let config = Qonversion.Configuration(apiKey: "api_key", launchMode: .analytics)
    Qonversion.initialize(with: config)
    return true
}
```

Once initialized Qonversion SDK can be accessed via the `Qonversion.shared` property. You can also use the instance returned by the `Qonversion.initialize` method.

## Apple Search Ads Attribution

Qonversion Swift SDK allows collecting Apple Search Ads attribution data. To enable it simply call the following SDK method. Also don't forget to complete the rest steps from [this page](/docs/apple-search-ads#1-enable-data-collection).

```swift theme={null}
Qonversion.shared.collectAppleSearchAdsAttribution()
```

## User Properties

### Set User Properties

Via Qonversion Swift SDK you can set both Qonversion-defined and custom [user properties](/docs/user-properties) as follows.

```swift theme={null}
// Setting Qonversion-defined user property
Qonversion.shared.setUserProperty("me@qonversion.io", key: .email)

// Setting custom user property
Qonversion.shared.setCustomUserProperty("yes", key: "liked-app")
```

### Get User Properties

You can also request all the user properties set for the current user using the following method.

```swift theme={null}
let userProperties: Qonversion.UserProperties = try await Qonversion.shared.userProperties()
userProperties.properties.forEach({ userProperty in
    print("User property: ", "key: ", userProperty.key, ", value: ", userProperty.value)
})
```

The object returned by the `userProperties` method contains several utility fields and methods:

```swift theme={null}
let userProperties: Qonversion.UserProperties = try await Qonversion.shared.userProperties()

// List of all user properties
let allProperties: [UserProperty] = userProperties.properties

// Subset of all user properties, which were set using Qonversion-defined keys                                  
let definedProperties: [UserProperty] = userProperties.definedProperties

// Subset of all user properties, which were set using custom keys
let customProperties: [UserProperty] = userProperties.customProperties

// A flattened version of all user properties as a key-value map
let flatPropertiesMap: [String: String] = userProperties.flatPropertiesMap

// A flattened version of defined user properties as a key-value map
let flatDefinedPropertiesMap: [UserPropertyKey: String] = userProperties.flatDefinedPropertiesMap

// A flattened version of custom user properties as a key-value map
let flatCustomPropertiesMap: [String: String] = userProperties.flatCustomPropertiesMap

// Searches for a property with the given property key in all properties list
let customProperty: UserProperty? = userProperties.property(for: "customKey")

// Searches for a property with the given Qonversion-defined property key in the defined properties list.
let emailProperty: UserProperty? = userProperties.definedProperty(for: .email)
```

```objectivec theme={null}
[[Qonversion sharedInstance] userProperties:^(QONUserProperties * _Nullable userProperties, NSError * _Nullable error) {
  ...
  // List of all user properties
  NSMutableArray<QONUserProperty *> *allProperties = userProperties.properties;
  
  // Subset of all user properties, which were set using Qonversion-defined keys
  NSMutableArray<QONUserProperty *> *definedProperties = userProperties.definedProperties;
  
  // Subset of all user properties, which were set using custom keys
  NSMutableArray<QONUserProperty *> *customProperties = userProperties.customProperties;
  
  // A flattened version of all user properties as a key-value map
  NSMutableDictionary<NSString *, NSString *> *flatPropertiesMap = userProperties.flatPropertiesMap;
  
  // A flattened version of defined user properties as a key-value map
  NSMutableDictionary<NSNumber *, NSString *> *flatDefinedPropertiesMap = userProperties.flatDefinedPropertiesMap;
  
  // A flattened version of custom user properties as a key-value map
  NSMutableDictionary<NSString *, NSString *> *flatCustomPropertiesMap = userProperties.flatCustomPropertiesMap;
  
  // Searches for a property with the given property key in all properties list
  QONUserProperty *customProperty = [userProperties propertyForKey:@"customKey"];
  
  // Searches for a property with the given Qonversion-defined property key in the defined properties list.
  QONUserProperty *emailProperty = [userProperties definedPropertyForKey:QONUserPropertyKeyEmail];
}];
```

```java Java theme={null}
Qonversion.getSharedInstance().userProperties(new QonversionUserPropertiesCallback() {
    @Override
    public void onSuccess(@NonNull QUserProperties userProperties) {
			  // List of all user properties
        List<QUserProperty> allProperties = userProperties.getProperties();
  
  		  // Subset of all user properties, which were set using Qonversion-defined keys
        List<QUserProperty> definedProperties = userProperties.getDefinedProperties();
  
  		  // Subset of all user properties, which were set using custom keys
        List<QUserProperty> customProperties = userProperties.getCustomProperties();
  
  		  // A flattened version of all user properties as a key-value map
        Map<String, String> flatPropertiesMap = userProperties.getFlatPropertiesMap();
  
  		  // A flattened version of defined user properties as a key-value map
        Map<QUserPropertyKey, String> flatDefinedPropertiesMap = userProperties.getFlatDefinedPropertiesMap();
  
  		  // A flattened version of custom user properties as a key-value map
        Map<String, String> flatCustomPropertiesMap = userProperties.getFlatCustomPropertiesMap();
  
  		  // Searches for a property with the given property key in all properties list
        QUserProperty customProperty = userProperties.getProperty("customKey");
  
  		  // Searches for a property with the given Qonversion-defined property key in the defined properties list.
        QUserProperty emailProperty = userProperties.getDefinedProperty(QUserPropertyKey.Email);
    }

    ...
});
```

```kotlin theme={null}
Qonversion.shared.userProperties(object : QonversionUserPropertiesCallback {
  override fun onSuccess(userProperties: QUserProperties) {
  	// List of all user properties
    val allProperties = userProperties.properties
  
  	// Subset of all user properties, which were set using Qonversion-defined keys
    val definedProperties = userProperties.definedProperties
  
  	// Subset of all user properties, which were set using custom keys
    val customProperties = userProperties.customProperties
  
  	// A flattened version of all user properties as a key-value map
    val flatPropertiesMap = userProperties.flatPropertiesMap
  
  	// A flattened version of defined user properties as a key-value map
    val flatDefinedPropertiesMap = userProperties.flatDefinedPropertiesMap
  
  	// A flattened version of custom user properties as a key-value map
    val flatCustomPropertiesMap = userProperties.flatCustomPropertiesMap
  
  	// Searches for a property with the given property key in all properties list
    val customProperty = userProperties.getProperty("customKey")
  
  	// Searches for a property with the given Qonversion-defined property key in the defined properties list.
    val emailProperty = userProperties.getDefinedProperty(QUserPropertyKey.Email)
  }
  ...
})
```

```javascript Flutter theme={null}
var userProperties = await Qonversion.getSharedInstance().userProperties();

// List of all user properties
var allProperties = userProperties.properties;

// Subset of all user properties, which were set using Qonversion-defined keys
var definedProperties = userProperties.definedProperties;

// Subset of all user properties, which were set using custom keys
var customProperties = userProperties.customProperties;

// A flattened version of all user properties as a key-value map
var flatPropertiesMap = userProperties.flatPropertiesMap;

// A flattened version of defined user properties as a key-value map
var flatDefinedPropertiesMap = userProperties.flatDefinedPropertiesMap;

// A flattened version of custom user properties as a key-value map
var flatCustomPropertiesMap = userProperties.flatCustomPropertiesMap;
                                     
// Searches for a property with the given property key in all properties list
var customProperty = userProperties.getProperty("customKey");
 
// Searches for a property with the given Qonversion-defined property key in the defined properties list.
var emailProperty = userProperties.getDefinedProperty(QUserPropertyKey.email);
```

```javascript React Native theme={null}
const userProperties = await Qonversion.getSharedInstance().userProperties();

// List of all user properties
const allProperties = userProperties.properties;

// Subset of all user properties, which were set using Qonversion-defined keys
const definedProperties = userProperties.definedProperties;

// Subset of all user properties, which were set using custom keys
const customProperties = userProperties.customProperties;

// A flattened version of all user properties as a key-value map
const flatPropertiesMap = userProperties.flatPropertiesMap;

// A flattened version of defined user properties as a key-value map
const flatDefinedPropertiesMap = userProperties.flatDefinedPropertiesMap;

// A flattened version of custom user properties as a key-value map
const flatCustomPropertiesMap = userProperties.flatCustomPropertiesMap;
                                     
// Searches for a property with the given property key in all properties list
const customProperty = userProperties.getProperty('customKey');
 
// Searches for a property with the given Qonversion-defined property key in the defined properties list.
const emailProperty = userProperties.getDefinedProperty(UserPropertyKey.EMAIL);
```

```csharp Unity theme={null}
Qonversion.GetSharedInstance().UserProperties((userProperties, error) =>
{
    ...
		// List of all user properties
    List<UserProperty> allProperties = userProperties.Properties;

		// Subset of all user properties, which were set using Qonversion-defined keys
    List<UserProperty> definedProperties = userProperties.DefinedProperties;

		// Subset of all user properties, which were set using custom keys
    List<UserProperty> customProperties = userProperties.CustomProperties;

		// A flattened version of all user properties as a key-value map
    Dictionary<string, string> flatPropertiesMap = userProperties.FlatPropertiesMap;

		// A flattened version of defined user properties as a key-value map
    Dictionary<UserPropertyKey, string> flatDefinedPropertiesMap = userProperties.FlatDefinedPropertiesMap;

		// A flattened version of custom user properties as a key-value map
    Dictionary<string, string> flatCustomPropertiesMap = userProperties.FlatCustomPropertiesMap;
                                     
		// Searches for a property with the given property key in all properties list
    UserProperty customProperty = userProperties.GetProperty("customKey");
 
		// Searches for a property with the given Qonversion-defined property key in the defined properties list.
    UserProperty emailProperty = userProperties.GetDefinedProperty(UserPropertyKey.Email);
});
```

```typescript Cordova theme={null}
const userProperties = await Qonversion.getSharedInstance().userProperties();

// List of all user properties
const allProperties = userProperties.properties;

// Subset of all user properties, which were set using Qonversion-defined keys
const definedProperties = userProperties.definedProperties;

// Subset of all user properties, which were set using custom keys
const customProperties = userProperties.customProperties;

// A flattened version of all user properties as a key-value map
const flatPropertiesMap = userProperties.flatPropertiesMap;

// A flattened version of defined user properties as a key-value map
const flatDefinedPropertiesMap = userProperties.flatDefinedPropertiesMap;

// A flattened version of custom user properties as a key-value map
const flatCustomPropertiesMap = userProperties.flatCustomPropertiesMap;
                                     
// Searches for a property with the given property key in all properties list
const customProperty = userProperties.getProperty('customKey');
 
// Searches for a property with the given Qonversion-defined property key in the defined properties list.
const emailProperty = userProperties.getDefinedProperty(Qonversion.UserPropertyKey.EMAIL);
```

### (Optional) Set IDFA (Identifier for Advertisers, iOS 14.5+ only)

Qonversion can also collect [Advertising identifier](/docs/user-properties#optional-set-idfa-identifier-for-advertisers-ios-145-only) if you receive the app tracking permission using ATT.

```swift theme={null}
ATTrackingManager.requestTrackingAuthorization { status in
    switch status {
    case .authorized:
        Qonversion.shared.collectAdvertisingId()

    // handle other states below
    }
}
```
