Table of Contents
Introduction
Welcome to MGEvents, the comprehensive event handling library for iOS apps!
With MGEvents, you can easily manage events within your iOS app, enabling effective communication between various components. This documentation will guide you through the process of installing and using MGEvents.
Installation
Follow the steps below to install MGEvents into your iOS project:
- Open your Xcode project.
- Navigate to the “File” menu and select “Swift Packages” > “Add Package Dependency”.
- In the search bar, enter “MGEvents” and click “Next”.
- Choose the latest version of MGEvents and click “Next”.
- Select the target where you want to add the package, and click “Finish”.
Usage
Once you have successfully installed MGEvents, you can start using it in your iOS app. Follow the steps below to begin:
- Import the MGEvents module into your Swift file using the following line at the beginning:
import MGEvents
- Create an instance of MGEvents using the following code:
let eventManager = MGEventsManager()
- Start using MGEvents to manage events in your app. Set up listeners using:
eventManager.addListener(for: .eventName) { (eventData) in
// handle event
}
- You can trigger events at the appropriate time using:
eventManager.triggerEvent(.eventName, data: eventData)
These are the basic steps to get started with MGEvents. For more details on advanced usage and additional features, please refer to the Examples section.
Events
MGEvents allows you to define custom events within your app. Here are a few common events that you might find useful:
Event Name | Description |
---|---|
LoginSuccess | Triggered when a user successfully logs in. |
Logout | Triggered when a user logs out of the app. |
NewMessage | Triggered when a new message is received. |
You can define and handle events based on your specific app requirements.
Examples
Here are a few examples that demonstrate how to use MGEvents:
LoginSuccess Event Example
Add the following code in your app to handle the LoginSuccess
event:
eventManager.addListener(for: .loginSuccess) { (eventData) in
guard let user = eventData["user"] as? User else { return }
print("User logged in successfully: \(user.name)")
}
NewMessage Event Example
Add the following code in your app to handle the NewMessage
event:
eventManager.addListener(for: .newMessage) { (eventData) in
guard let message = eventData["message"] as? String else { return }
print("New message received: \(message)")
}
Feel free to adapt these examples to suit your application’s requirements.
FAQ
Here are some frequently asked questions about MGEvents:
1. How do I remove an event listener?
To remove an event listener, call the removeListener(for:)
method of your eventManager instance:
eventManager.removeListener(for: .eventName)
2. Can I pass data when triggering an event?
Yes, you can pass data when triggering an event. Use the data
parameter when calling the triggerEvent(_:data:)
method.
Support
If you require any assistance or have any questions about using MGEvents, please don’t hesitate to contact our support team at support@mgevents.com.