プレースメントを追加する (レガシー)
始める前に
Rokt iOS SDKが、こちらに記載されている手順に従って、すでにアプリケーションに統合されていることを確認してください。
オーバーレイプレースメント
ご希望のViewControllerでRokt iOS SDKを実行し、適切な顧客属性をすべて追加します。以下のコード例では、ViewDidLoadを使用してRoktオーバーレイプレースメントを表示します。
SDKは、ビューがロードおよびアンロードされるときのオプションのコールバックイベントを提供します。
- Swift
- Objective-C
- SwiftUI
import Rokt_Widget
class OrderConfirmationViewController: UIViewController {
// プレースメントを表示する必要があるときにこの関数を呼び出します
func showWidget() {
let attributes = ["email": "j.smith@example.com",
"firstname": "Jenny",
"lastname": "Smith",
"mobile": "(555)867-5309",
"postcode": "90210",
"country": "US"]
Rokt.execute(viewName: "RoktExperience", attributes: attributes, onLoad: {
// Roktプレースメントがロードされたときのオプションのコールバック
}, onUnLoad: {
// Roktプレースメントがアンロードされたときのオプションのコールバック
}, onShouldShowLoadingIndicator: {
// ローディングインジケーターを表示するためのオプションのコールバック
}, onShouldHideLoadingIndicator: {
// ローディングインジケーターを非表示にするためのオプションのコールバック
})
}
}
#import <Rokt_Widget/Rokt_Widget-Swift.h>
// 配置を表示する必要があるときにこの関数を呼び出します
- (void)showWidget {
NSDictionary *attributes = @{
@"email" : @"j.smith@example.com",
@"firstname": @"Jenny",
@"lastname": @"Smith",
@"mobile": @"(555)867-5309",
@"postcode": @"90210",
@"country": @"US"
};
[Rokt executeWithViewName:@"RoktExperience"
attributes:attributes
placements:nil
onLoad:^{
// Rokt配置がロードされたときのオプションのコールバック
} onUnLoad:^{
// Rokt配置がアンロードされたときのオプションのコールバック
} onShouldShowLoadingIndicator:^{
// ローディングインジケーターを表示するためのオプションのコールバック
} onShouldHideLoadingIndicator:^{
// ローディングインジケーターを隠すためのオプションのコールバック
} onEmbeddedSizeChange:^(NSString *selectedPlacement, CGFloat widgetHeight) {
// フルスクリーンオーバーレイには必要ありません
}];
}
import SwiftUI
import Rokt_Widget
class OrderConfirmationViewController: View {
func showPlacement() {
let attributes = ["email": "j.smith@example.com",
"firstname": "Jenny",
"lastname": "Smith",
"mobile": "(555)867-5309",
"postcode": "90210",
"country": "US"]
Rokt.execute(viewName: "RoktExperience",
attributes: attributes,
onLoad: {
// Rokt配置がロードされたときのオプションのコールバック
}, onUnLoad: {
// Rokt配置がアンロードされたときのオプションのコールバック
}, onShouldShowLoadingIndicator: {
// ローディングインジケーターを表示するためのオプションのコールバック
}, onShouldHideLoadingIndicator: {
// ローディングインジケーターを隠すためのオプションのコールバック
})
}
}
オプション機能
| 機能 | 目的 |
|---|---|
Rokt.close() | オーバーレイ配置を自動的に閉じるために使用されます。 |
埋め込み配置
ストーリーボードの変更
SwiftUIを使用している場合は、以下のSwiftUIテーブルの指示を参照してください。SwiftUIを使用していない場合は、次の指示を使用してください。
ストーリーボードにビューを追加し、それをViewControllerに配置します:

customクラスで、ビューのクラスとしてRoktEmbeddedViewを設定します。次に、埋め込み配置が表示される場所に一致するように、上部、先頭、および末尾の制約を定義します。高さについては、高さ制約をゼロに設定することをお勧めします。
RoktEmbeddedViewは、配置のコンテンツに応じて自身の高さのみを変更できます。高さが変更されたときに通知し、新しい高さを返すコールバックがexecuteメソッドにあります。
以下の画像は、オートレイアウトを使用してRoktEmbeddedViewを定義する最も簡単な方法を示しています。
-
ビューのClassとしてRoktEmbeddedViewを、ModuleとしてRokt_Widgetを設定します。
-
上部の制約を定義します。
-
先頭の制約を定義します。
-
末尾の制約を定義します。
-
高さと幅の制約を選択します。
-
高さの制約を設定します。
-
ビューに制約を追加します。

RoktEmbeddedViewはコード内で作成し、レイアウトに動的に含めることもできます。
Rokt SDKの実行
iOS用のRokt SDKを、目的のViewControllerで実行し、適切な顧客属性をすべて追加します。以下のコード例では、ViewDidLoadを使用してプレースメントを起動します。
SDKは、ビューがロードおよびアンロードされるときのオプションのコールバックイベントを提供します。
- Swift
- Objective-C
- SwiftUI
import Rokt_Widget
class OrderConfirmationViewController: UIViewController {
// ステップ5で作成されたRoktEmbeddedViewにリンクされているか、プログラムで作成することもできます
@IBOutlet weak var roktEmbeddedView: RoktEmbeddedView!
...
// プレースメントを表示する必要があるときにこの関数を呼び出します
func showWidget() {
let attributes = ["email": "j.smith@example.com",
"firstname": "Jenny",
"lastname": "Smith",
"mobile": "(555)867-5309",
"postcode": "90210",
"country": "US"]
let placements: [String : RoktEmbeddedView] = ["RoktEmbedded1": roktEmbeddedView]
Rokt.execute(viewName: "RoktEmbeddedExperience", attributes: attributes, placements: placements, onLoad: {
// Roktプレースメントがロードされたときのオプションのコールバック
}, onUnLoad: {
// Roktプレースメントがアンロードされたときのオプションのコールバック
}, onShouldShowLoadingIndicator: {
// ローディングインジケーターを表示するためのオプションのコールバック
}, onShouldHideLoadingIndicator: {
// ローディングインジケーターを非表示にするためのオプションのコールバック
}, onEmbeddedSizeChange: { selectedPlacement, widgetHeight in
// プレースメント の高さが変わるたびに選択されたプレースメントと必要な高さを取得するためのオプションのコールバック
})
}
}
#```objective-c
import <Rokt_Widget/Rokt_Widget-Swift.h>
// 配置を表示する必要があるときにこの関数を呼び出します
- (void)showWidget {
NSDictionary *attributes = @{
@"email" : @"j.smith@example.com",
@"firstname": @"Jenny",
@"lastname": @"Smith",
@"mobile": @"(555)867-5309",
@"postcode": @"90210",
@"country": @"US"
};
// roktEmbeddedView は .h 内で @property (weak, nonatomic) IBOutlet RoktEmbeddedView *roktEmbeddedView; として定義されており、ステップ5で定義された roktEmbeddedView を指しています。またはプログラムで作成することもできます
NSDictionary<NSString *, RoktEmbeddedView *> *placements= [NSDictionary dictionaryWithObject:self.roktEmbeddedView forKey:@"RoktEmbedded1"];
[Rokt executeWithViewName:@"RoktExperience"
attributes:attributes
placements:placements
onLoad:^{
// Rokt 配置がロードされたときのオプションのコールバック
} onUnLoad:^{
// Rokt 配置がアンロードされたときのオプションのコールバック
} onShouldShowLoadingIndicator:^{
// ローディングインジケーターを表示するためのオプションのコールバック
} onShouldHideLoadingIndicator:^{
// ローディングインジケーターを非表示にするためのオプションのコールバック
} onEmbeddedSizeChange:^(NSString *selectedPlacement, CGFloat widgetHeight){
// 配置の高さが変更されるたびに、selectedPlacement と配置に必要な高さを取得するためのオプションのコールバック
}];
}
import Rokt_Widget
import SwiftUI
// UIKit と SwiftUI を接続するための UIViewRepresentable を作成
struct RoktEmbeddedSwiftUIView: UIViewRepresentable {
typealias UIViewType = RoktEmbeddedView
var embedded: RoktEmbeddedView
init() {
embedded = RoktEmbeddedView()
}
func makeUIView(context: Context) -> RoktEmbeddedView {
return embedded
}
func updateUIView(_ uiView: RoktEmbeddedView, context: Context) {
}
}
struct OrderConfirmationViewController: View {
// Rokt 埋め込みビューを作成
let roktEmbedded1 = RoktEmbeddedSwiftUIView()
@State private var embeddedSize: CGFloat = 0
@State private var placementDisplayed = false
var body: some View {
ScrollView {
VStack(spacing: 0){
// Rokt 埋め込みビューを配置したい場所に配置
```swift
roktEmbedded1
.frame(height: self.embeddedSize, alignment: .center)
}
}.onAppear {
if !placementDisplayed {
// ビューが表示されたときにプレースメントを表示
showPlacement()
}
}
}
private func showPlacement() {
var placements = [String: RoktEmbeddedView]()
placements["RoktEmbedded1"] = roktEmbedded1.embedded
let attributes = ["email": "j.smith@example.com",
"firstname": "Jenny",
"lastname": "Smith",
"mobile": "(555)867-5309",
"postcode": "90210",
"country": "US"]
Rokt.execute(viewName: "RoktEmbeddedExperience",
attributes: attributes,
placements: placements,
onLoad: {
// Roktプレースメントがロードされたときのオプションのコールバック
self.placementDisplayed = true
}, onUnLoad: {
// Roktプレースメントがアンロードされたときのオプションのコールバック
}, onShouldShowLoadingIndicator: {
// ローディングインジケーターを表示するためのオプションのコールバック
}, onShouldHideLoadingIndicator: {
// ローディングインジケーターを非表示にするためのオプションのコールバック
}, onEmbeddedSizeChange: { selectedPlacement, widgetHeight in
embeddedSize = widgetHeight
})
}
ビュー名 RoktExperience またはプレースメント名 RoktEmbedded1 を別の値に更新するには、Roktアカウントマネージャーに連絡して、Roktプレースメントが一貫して設定されていることを確認してください。
SwiftUI コンポーネント
Rokt iOS SDK のメジャーバージョン 4 から、RoktLayout コンポーネントを使用して Rokt レイアウトを追加できます。これにより、Rokt.execute を呼び出す必要がなくなり、SwiftUI を使用したよりモダンな宣言型の統合がサポートされます。
コンポーネントの追加
import SwiftUI
import Rokt_Widget
struct OrderConfirmationSwiftUI: View {
let attributes: [String: String]
let pageIdentifier: String
let location: String
@State var sdkTriggered = true
var body: some View {
VStack(alignment: .leading) {
RoktLayout(sdkTriggered: $sdkTriggered,
viewName: pageIdentifier,
locationName: location, // 埋め込みレイアウトを使用する場合
attributes: attributes,
onLoad: {
// Rokt プレースメントがロードされたときのオプションのコールバック
}, onUnLoad: {
// Rokt プレースメントがアンロードされたときのオプションのコールバック
},onShouldShowLoadingIndicator: {
// ローディングインジケーターを表示するためのオプションのコールバック
}, onShouldHideLoadingIndicator: {
// ローディングインジケーターを非表示にするためのオプションのコールバック
})
}.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
}
RoktLayout コンポーネントは、埋め込みレイアウトとオーバーレイレイアウトの両方に使用できます。
イベント
SDKは各ページのイベントをRokt.events APIを通じて提供します。
Rokt.events(viewName: "viewName") { roktEvent in
print("Roktイベントを受信しました \(roktEvent)")
}