Setup
Get your Application Keys
Signup for CometChat and then:
- Create a new app
- Head over to the API Keys section and note the Auth Key, App ID & Region
- Xcode 12 (or Higher)
- iOS 11 or higher
Add the CometChat Dependency
CocoaPods
We recommend using CocoaPods, as they are the most advanced way of managing iOS project dependencies. Open a terminal window, move to your project directory, and then create a Podfile
by running the following command.
- CometChatSDK supports installation through Cocoapods only. Currently, we are supporting Xcode 11.4 and higher.
- CometChatSDK includes video calling components. We suggest you run on physical devices to avoid errors.
- Swift
$ pod init
Add the following lines to the Podfile.
- Swift
platform :ios, '11.0'
use_frameworks!
target 'MyApp' do
pod 'CometChatSDK', '4.0.52'
end
And then install the CometChatSDK
framework through CocoaPods.
- Swift
$ pod install
Always get the latest version of CometChatSDK
by command
- Swift
$ pod update CometChatSDK
Swift Packages
To install Swift Packages you can use Xcode package manager**.**
- Open Xcode, go to the project's General settings tab and select the project under Project in the left column.
- Go to the Swift packages tab and click on the + button.
![Image](/docs/assets/images/1634032671-f0fc1c00c9f33cca47875131228648cb.png)
- Once the pop-up appears, enter the github repository address in the search bar
https://github.com/cometchat-pro/sdk/ios.git
and set dependency rule toUp to Next Major Version
and set version as4.0.52
. Then click on the Add Package button.
![Image](/docs/assets/images/nh6l7iltomtl4me5y6yzvupevalnh891fcoemppo30e6ijlc2boq1pxyanc6ujnt-438a8e2fee0f935ba81fc7ee14dc5ab7.png)
CometChatSDK
must be checked in the Package Product column and click on the Add Package button. This will add Package Dependencies menu in Xcode.
![Image](/docs/assets/images/ehjnlsoxuq3n7awjtsdjkggol8jegnsjnwgisr4a8nkrr1gt6nfjyd6azzk5c57w-1b3966209f90b9a67a5ad7107b8c25ec.png)
Request Authorization
Prepare your app for this requirement by providing justification strings. The justification string is a localizable message that you add to your app's Info.plist
file to tell the user why your app needs access to the user's photo library, Camera, Microphone. Then, App prompts the user to grant permission for access, the alert displays the justification string you provided, in the language of the locale selected on the user's device. You can do this as follows:
![Image](/docs/assets/images/1623200477-7bdad6acb11779de4a98a154e785d56a.png)
- Info.plist
<key>NSAppTransportSecurity<_key>
<dict>
<key>NSAllowsArbitraryLoads<_key>
<true _>
<_dict>
<key>NSCameraUsageDescription<_key>
<string>$(PRODUCT_NAME) need access to the camera in order to update your avatar<_string>
<key>NSPhotoLibraryUsageDescription<_key>
<string>$(PRODUCT_NAME) need access to the Photo Library in order to send Media Messages<_string>
<key>NSMicrophoneUsageDescription<_key>
<string>$(PRODUCT_NAME) need access to the Microphone in order to connect Audio_Video call <_string>
Setup Bitcode
You can set the Enable Bitcode setting to YES present in build settings in your XCode project.
![Image](/docs/assets/images/1631809611-db96539a98643ad55b7950d86d4b6557.png)
If you are using CometChat Pro SDK having the version less than 2.0.8, then you need to set Enable Bitcode to NO
Swift Standard Libraries
CometChatSDK
framework build on Swift, you have to ensure the required libraries are embedded. This can be done by setting the “Always Embed Swift Standard Libraries”
checkbox in your target’s build settings to “Yes”:
![Image](/docs/assets/images/1623200430-15b0442a710f78a45dd01090dc6a32f1.png)
Set Header Search Path
Set the Header Search Paths
to $SDKROOT/usr/include/libxml2
.
![Image](/docs/assets/images/1631809733-acc69d5f571a8aac9ac8683e9f1ed0eb.png)
Initialize CometChat
The init()
method initializes the settings required for CometChat.
The init()
method takes the below parameters:
- appID - You CometChat App ID
- appSettings - An object of the AppSettings class can be created using the AppSettingsBuilder class. The region field is mandatory and can be set using the
setRegion()
method.
The AppSettings
class allows you to configure three settings:
- Region: The region where you app was created.
- Presence Subscription : Represents the subscription type for user presence (real-time online/offline status)
- autoEstablishSocketConnection(boolean value): This property takes a boolean value which when set to true informs the SDK to manage the web-socket connection internally. If set to false, it informs the SDK that the web-socket connection will be managed manually. The default value for this parameter is true. For more information on this, please check the Managing Web-Socket connections manually section. The default value for this property is true.
- overrideAdminHost(adminHost: string): This method takes the admin URL as input and uses this admin URL instead of the default admin URL. This can be used in case of dedicated deployment of CometChat.
- overrideClientHost(clientHost: string): This method takes the client URL as input and uses this client URL instead of the default client URL. This can be used in case of dedicated deployment of CometChat.
We suggest you call the method on app startup preferably in the didFinishLaunchingWithOptions:
method of the AppDelegate
class.
- Swift
- Objective C
import CometChatSDK
class AppDelegate: UIResponder, UIApplicationDelegate{
{
var window: UIWindow?
let appId: String = "ENTER APP ID"
let region: String = "ENTER REGION CODE"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let mySettings = AppSettings.AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(region: region).build()
CometChat.init(appId: appId ,appSettings: mySettings,onSuccess: { (isSuccess) in
if (isSuccess) {
print("CometChat Pro SDK intialise successfully.")
}
}) { (error) in
print("CometChat Pro SDK failed intialise with error: \\(error.errorDescription)")
}
return true
}
}
#import <CometChatSDK_CometChatSDK-Swift.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *region = @"REGION";
NSString *appID = @"YOUR_APP_ID";
AppSettingsBuilder *appSettingBuilder = [[AppSettingsBuilder alloc]init];
AppSettings *appSettings = [[[appSettingBuilder subscribePresenceForAllUsers]setRegionWithRegion:region]build];
[[CometChat alloc]initWithAppId: appID appSettings:appSettings onSuccess:^(BOOL isSuccess) {
NSLog(isSuccess ? @"CometChat Initialize Success:-YES" : @"CometChat Initialize Success:-NO");
} onError:^(CometChatException * error) {
NSLog(@"Error %@",[error errorDescription]);
}];
return YES;
}
Make sure you replace the appId
with your CometChat App ID in the above code.
Parameter | Description |
---|---|
appID | CometChat App ID |
appSetting | An object of the AppSettings class. |
Publishing to App Store
To publish your App on App Store please follow this guide