Channel Talk SDKはSwiftUIを公式的にサポートしていないため、SwiftUIに最適化していません。
こちらのガイドは公式の開発ドキュメントではないため最新バージョンと内容が異なる場合があります。
こちらのガイドは以下の環境で作成しています。
Xcode : 13.0 (13A233)
CocoaPod : 1.10.1
Swift Package Manager : available
Channel SDK - iOS : 9.1.2
Installation
基本的にネイティブと同様で、こちらのガイドではCocoaPodsを使用しています。
XcodeバージョンとCocoaPodsバージョンによっては設定方法が異なる場合があります。
1. Initialize ChannelIO
After iOS 14 (without AppDelegate, SceneDelegate)
iOS 14以上でAppDelegateとSceneDelegateが自動的に生成されない場合
1.
AppDelegate classを生成し、以下のコードを作成します。
import ChannelIOFront
class AppDelegate: NSObject, UIApplicationDelegate {
var window: UIWindow?
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
ChannelIO.initialize(application)
return true
}
}
Swift
복사
2.
UIWindowを強く参照できるシングルトンオブジェクトを生成し、以下のコードを作成します。
例では、ChannelIOFrontManagerファイルを作成して使用しました。
class ChannelIOFrontManager {
static let shared: ChannelIOFrontManager = ChannelIOFrontManager()
private var channelWindow: UIWindow?
func initChannelWindow() {
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
channelWindow = ChannelIO.initializeWindow(with: scene)
}
}
}
Swift
복사
3.
@main annotationとApp protocolを採用したApp構造体でwindowをinitializeします。
例では、SwiftUISampleAppです。
// initialize window
@main
struct SwiftUISampleApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
RootView()
.onAppear(perform: ChannelIOFrontManager.shared.initChannelWindow) // *最初の一度だけ実行する必要があります*
}
}
}
Swift
복사
4.
ChannelIO Boot.ChannelIOのBootはbootが必要なところで使用しました。
例では、SomeView(使いたい箇所)のonAppearでbootしました。
// boot ChannelIO
struct SomeView : View {
...
var body: some View {
ContentView()
...
.onAppear(perform: runChannelIO)
}
}
private func runChannelIO() {
// bootOption 例
let config = BootConfig()
config.pluginKey = "your plugin key"
ChannelIO.boot(with: config) { (status, user) in
switch status {
case .success:
ChannelIO.showChannelButton()
ChannelIO.showMessenger()
default:
print(status.rawValue)
}
}
}
Swift
복사
ChannelIO SDKを使用するためにはwindowのinitializeとbootが必要です。
Before iOS 14 (with Appdelegate, SceneDelegate)
iOS 14以下でAppDelegateとSceneDelegateが自動生成された場合
1.
AppDelegate.swiftに以下のコードを実装します。
import ChannelIOFront
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
ChannelIO.initialize(application)
return true
}
}
Swift
복사
2.
SceneDelegate.swiftに以下のコードを実装してwindowをinitializeしてくれます。
class SceneDelegate: NSObject, UISceneDelegate {
var channelWindow: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
channelWindow = ChannelIO.initializeWindow(with: windowScene)
}
}
}
Swift
복사
3.
ChannelIO Boot.ChannelIOのBootはbootが必要なところで使用します。
例では、SomeView(使いたい箇所)のonAppearでbootしました。
// boot ChannelIO
struct SomeView : View {
...
var body: some View {
ContentView()
...
.onAppear(perform: runChannelIO)
}
}
private func runChannelIO() {
// bootOption 例
let config = BootConfig()
config.pluginKey = "your plugin key"
ChannelIO.boot(with: config) { (status, user) in
switch status {
case .success:
ChannelIO.showChannelButton()
ChannelIO.showMessenger()
default:
print(status.rawValue)
}
}
}
Swift
복사
ChannelIO SDKを使用するためにはwindowのinitializeとbootが必要です。
2. Using ChannelIO
initialize以降の動作はネイティブと同様です。
必要な場所でChannel IOのメソッドを呼び出して使用します。
3. Push Notification
ChannelIOのiOS SDKでは、APNSを通じてメッセージをやり取りします。
APNSの使用方法によって様々な設定法がありますが、このガイドでは最も基本的な設定法を示しています。
1.
push tockenの登録
AppDelegate.swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
ChannelIO.initPushToken(deviceToken)
}
Swift
복사
2.
チャネルトークのpushを保存(iOS 10以上)
UNUserNotificationCenterDelegate 実装
// チャネルトークのpushを保存
// iOS 10 and above
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if ChannelIO.isChannelPushNotification(userInfo) {
ChannelIO.receivePushNotification(userInfo)
ChannelIO.storePushNotification(userInfo)
}
completionHandler()
}
Swift
복사
3.
2番で保存されたpushを任意のViewで表示するメソッド
struct SomeView : View {
var body: some View {
ContentView()
...
.onAppear {
if ChannelIO.hasStoredPushNotification() {
ChannelIO.openStoredPushNotification()
}
}
}
}
Swift
복사