TUIKit SwiftUI。LoginStore 的 login 接口登录组件。sdkAppID,上文获取的 sdkAppID。userID,操作者的 userID,也就是 快速开始 中创建的 user1。userSig,操作者的 userSig,也就是 快速开始 中创建的 user1 的 userSig。// ContentView.swiftimport SwiftUIstruct ContentView: View {var body: some View {CreateGroupPage()}}// CreateGroupPage.swiftimport AtomicXimport AtomicXCoreimport SwiftUIpublic struct CreateGroupPage: View {@StateObject private var themeState = ThemeState.shared@State private var selectedUsers: [UserPickerItem] = []@State private var isLoggedIn = false@State private var isLoggingIn = true@State private var loginError: String? = nil@State private var showConfigSheet = false@State private var friendList: [ContactInfo] = []@State private var createdGroupID: String? = nilprivate let sdkAppID: Int32 = 1234567890 // TODO: Fill in your sdkAppID hereprivate let userID = "" // TODO: Fill in your userID hereprivate let userSig = "" // TODO: Fill in your generated userSig hereprivate let contactListStore = ContactListStore.create()public var body: some View {Group {if isLoggedIn {createGroupContentView} else if isLoggingIn {ProgressView("Logging in...")} else {VStack(spacing: 12) {Image(systemName: "exclamationmark.triangle").font(.system(size: 40)).foregroundColor(.orange)Text(loginError ?? "Login failed").foregroundColor(.secondary)}}}.environmentObject(themeState).onAppear {login()}}// MARK: - Create Group Contentprivate var createGroupContentView: some View {NavigationView {VStack(spacing: 0) {if let groupID = createdGroupID {// Group created successfullyVStack(spacing: 16) {Image(systemName: "checkmark.circle.fill").font(.system(size: 60)).foregroundColor(.green)Text("Group Created!").font(.system(size: 20, weight: .semibold))Text("Group ID: \\(groupID)").font(.system(size: 15)).foregroundColor(.secondary)Button("Create Another Group") {createdGroupID = nil}.padding(.top, 8)}.padding()} else {// UserPicker for selecting group membersUserPicker(userList: userPickerItems,maxCount: 0,onSelectedChanged: { users inselectedUsers = usersshowConfigSheet = true})}}.navigationBarTitle("Create Group", displayMode: .inline)}.navigationViewStyle(StackNavigationViewStyle()).onAppear {contactListStore.fetchFriendList(completion: { _ in })}.onReceive(contactListStore.state.subscribe(StatePublisherSelector(keyPath: \\ContactListState.friendList))) { newFriendList inself.friendList = newFriendList}.sheet(isPresented: $showConfigSheet, onDismiss: {selectedUsers = []}) {ConfigGroupInfoView(members: selectedUsers,contactListStore: contactListStore,onComplete: { groupID, groupName, conversationId inshowConfigSheet = falseif let gid = groupID {print(">>>>> Group created: groupID=\\(gid), groupName=\\(groupName ?? ""), conversationId=\\(conversationId ?? "")")createdGroupID = gid}},onBack: {showConfigSheet = false})}}// MARK: - Helperprivate var userPickerItems: [UserPickerItem] {friendList.map { contact inUserPickerItem(userID: contact.contactID,avatarURL: contact.avatarURL,title: contact.title ?? contact.contactID)}}// MARK: - Loginprivate func login() {guard !userSig.isEmpty else {isLoggingIn = falseloginError = "userSig is empty. Please fill in a valid userSig."return}// Login is required when page appears.LoginStore.shared.login(sdkAppID: sdkAppID, userID: userID, userSig: userSig) { result inswitch result {case .success:print(">>>>> Login success, userID: \\(userID)")isLoggedIn = trueisLoggingIn = falsecase .failure(let error):print(">>>>> Login failed: \\(error.code), \\(error.message)")loginError = "Login failed: \\(error.code), \\(error.message)"isLoggingIn = false}}}}
搜索联系人 | 发送请求 |
![]() | ![]() |
文档反馈