matrix-org / matrix-org/matrix-ios-sdk
Can You Please Modified the Swift Calling method as well No one method given in the readme doc works with swift ? it shows Error while calling Xcode 11.3.1 with swift integration
Nobody has claimed this yet.
- Dominant language
- Objective-C
- Stars
- 484
- Forks
- 225
- PR merge metrics
- No merged PRs in 30d
Description
Post in stackoverflow as well
https://stackoverflow.com/questions/62874998/matrix-sdk-ios-public-room
**Use case #1: Get public rooms of an homeserver**
This API does not require the user to be authenticated. So, MXRestClient instantiated with initWithHomeServer does the job:
**Swift**:
```
let homeServerUrl = URL(string: "http://matrix.org")!
let mxRestClient = MXRestClient(homeServer: homeServerUrl, unrecognizedCertificateHandler: nil)
mxRestClient.publicRooms { response in
switch response {
case .success(let rooms):
// rooms is an array of MXPublicRoom objects containing information like room id
print("The public rooms are: \(rooms)")
case .failure: break
}
}
```
**Use case #2: Get the rooms the user has interacted with**
Here the user needs to be authenticated. We will use [MXRestClient initWithCredentials]. You'll normally create and initialise these two objects once the user has logged in, then keep them throughout the app's lifetime or until the user logs out:
**Swift:**
```
let credentials = MXCredentials(homeServer: "http://matrix.org",
userId: "@your_user_id:matrix.org",
accessToken: "your_access_token")
// Create a matrix client
let mxRestClient = MXRestClient(credentials: credentials, unrecognizedCertificateHandler: nil)
// Create a matrix session
let mxSession = MXSession(matrixRestClient: mxRestClient)
// Launch mxSession: it will first make an initial sync with the homeserver
mxSession.start { response in
guard response.isSuccess else { return }
// mxSession is ready to be used
// now wer can get all rooms with:
mxSession.rooms
}
```
**Use case #2 (bis): Get the rooms the user has interacted with (using a permanent MXStore)**
We use the same code as above but we add a MXFileStore that will be in charge of storing user's data on the file system. This will avoid to do a full sync with the homeserver each time the app is resumed. The app will be able to resume quickly. Plus, it will be able to run in offline mode while syncing with the homeserver:
**Swift:**
```
let credentials = MXCredentials(homeServer: "http://matrix.org",
userId: "@your_user_id:matrix.org",
accessToken: "your_access_token")
// Create a matrix client
let mxRestClient = MXRestClient(credentials: credentials, unrecognizedCertificateHandler: nil)
// Create a matrix session
let mxSession = MXSession(matrixRestClient: mxRestClient)
// Make the matrix session open the file store
// This will preload user's messages and other data
let store = MXFileStore()
mxSession.setStore(store) { response in
guard response.isSuccess else { return }
// Launch mxSession: it will sync with the homeserver from the last stored data
// Then it will listen to new coming events and update its data
mxSession.start { response in
guard response.isSuccess else { return }
// mxSession is ready to be used
// now we can get all rooms with:
mxSession.rooms()
}
}
```
**Use case #3: Get messages of a room**
We reuse the mxSession instance created before:
**Swift:**
// Retrieve the room from its room id
```
let room = mxSession.room(withRoomId: "!room_id:matrix.org")
// Add a listener on events related to this room
_ = room?.liveTimeline.listenToEvents { (event, direction, roomState) in
switch direction {
case .forwards:
// Live/New events come here
break
case .backwards:
// Events that occurred in the past will come here when requesting pagination.
// roomState contains the state of the room just before this event occurred.
break
}
}
```
**Let's load a bit of room history using paginateBackMessages:**
**Swift:**
// Reset the pagination start point to now
```
room?.liveTimeline.resetPagination()
room?.liveTimeline.paginate(10, direction: .backwards, onlyFromStore: false) { _ in
// At this point, the SDK has finished to enumerate the events to the attached listeners
}
```
**Use case #4: Post a text message to a room**
This action does not require any business logic from MXSession: We can use MXRestClient directly:
**Swift:**
```
client.sendTextMessage(toRoom: "the_room_id", text: "Hello World!") { (response) in
if case .success(let eventId) = response {
// eventId is for reference
// If you have registered events listener like in the previous use case, you will get
// a notification for this event coming down from the homeserver events stream and
// now handled by MXSession.
}
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the README's Swift examples and reproduce the reported calls using Xcode 11.3.1. Check each documented initializer and method against the Swift API; done means the README contains working Swift calling examples for the listed use cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- documentation, mobile-dev
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100