# Link creation

> Generate smart links programmatically in your iOS app.

Source: https://docs.grovs.io/docs/sdk/ios/link-creation

Create smart links from your app with `generateLink`, then share them.

## Generating a link

Pass preview metadata, a payload and tracking parameters:

```swift
Grovs.generateLink(
    title: "Check out this product",
    subtitle: "Limited time offer",
    imageURL: "https://example.com/image.jpg",
    data: ["productId": "12345", "screen": "product_detail"],
    tags: ["promotion", "share"],
    customRedirects: nil,
    showPreviewiOS: true,
    showPreviewAndroid: nil,
    copyToClipboardiOS: nil,
    copyToClipboardAndroid: nil,
    trackingCampaign: "spring_sale",
    trackingSource: "in_app",
    trackingMedium: "share_button"
) { url in
    guard let url = url else { return }
    print("Generated link: \(url)")
}
```

## Parameters

| Parameter                | Type               | Description                                                                                                                                                                                                                                |
| ------------------------ | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `title`                  | `String?`          | Link preview title                                                                                                                                                                                                                         |
| `subtitle`               | `String?`          | Link preview subtitle                                                                                                                                                                                                                      |
| `imageURL`               | `String?`          | Link preview image URL                                                                                                                                                                                                                     |
| `data`                   | `[String: Any]?`   | Custom payload delivered on deep link open                                                                                                                                                                                                 |
| `tags`                   | `[String]?`        | Tags for filtering and analytics                                                                                                                                                                                                           |
| `customRedirects`        | `CustomRedirects?` | Override default redirect behavior                                                                                                                                                                                                         |
| `showPreviewiOS`         | `Bool?`            | Show link preview on iOS (uses dashboard default if `nil`)                                                                                                                                                                                 |
| `showPreviewAndroid`     | `Bool?`            | Show link preview on Android (uses dashboard default if `nil`)                                                                                                                                                                             |
| `copyToClipboardiOS`     | `Bool?`            | Copy the link to the visitor's clipboard on the iOS preview page, for [clipboard-assisted deferred deep linking](https://docs.grovs.io/docs/sdk/ios/deep-linking#clipboard-assisted-deferred-deep-linking) (uses project default if `nil`) |
| `copyToClipboardAndroid` | `Bool?`            | Same, for the Android preview page (uses project default if `nil`)                                                                                                                                                                         |
| `trackingCampaign`       | `String?`          | UTM campaign parameter                                                                                                                                                                                                                     |
| `trackingSource`         | `String?`          | UTM source parameter                                                                                                                                                                                                                       |
| `trackingMedium`         | `String?`          | UTM medium parameter                                                                                                                                                                                                                       |

## Custom redirects

`CustomRedirects` overrides where a link sends users on each platform:

```swift
let redirects = CustomRedirects(
    ios: CustomLinkRedirect(link: "https://example.com/ios-promo"),
    android: CustomLinkRedirect(link: "https://example.com/android-promo"),
    desktop: CustomLinkRedirect(link: "https://example.com/desktop-promo", openAppIfInstalled: false)
)

Grovs.generateLink(
    title: "Special offer",
    data: ["promoId": "summer25"],
    customRedirects: redirects
) { url in
    guard let url = url else { return }
    print("Generated link: \(url)")
}
```

Each `CustomLinkRedirect` takes a `link` URL and an optional `openAppIfInstalled` flag (default `true`). With `true`, the app opens if installed. With `false`, the redirect URL is always used.

## Sharing the link

Present a share sheet once the link is ready:

```swift
Grovs.generateLink(
    title: "Share this",
    data: ["itemId": "abc"]
) { url in
    guard let url = url else { return }
    let activityVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)
    self.present(activityVC, animated: true)
}
```
