Detailed steps for integrating iOS development with cloud instant messaging, and ios instant messaging
Detailed steps for integrating iOS development with cloud instant messaging
1. Rong cloud instant messaging iOS SDK http://rongcloud.cn/downloads selection iOS SDK download
2. before developing an application, you must create an application on the rongyun developer platform. If you have registered an rongyun developer account, go to the rongyun developer platform to create an application; if you have not yet registered an Alibaba Cloud developer account, go to the Alibaba Cloud official website to register a developer account and create an application. Register address https://developer.rongcloud.cn/signup
3. log onto the https://developer.rongcloud.cn/signin to create an application
4. After Entering the background, click Create application to enter such a creation interface.
Figure 1
5. Click "CREATE" and then click "my application name" on the left.
Figure 2
6. Click AppKey to enter
Figure 3
7. Manually install the Alibaba Cloud instant messaging SDK
7.1 import the downloaded latest rongyun SDK to your project.
7.2 add dependency library in Build Phases the third option link click + in the lower left corner to add dependency Library
Required dependent Libraries
Figure 4
8. Get Token
In the same way as step 5, go to the Alibaba Cloud background and click my application> your application name> IM service> API debugging.
A page is displayed on the right, where you can obtain the debugging Token.
Figure 5
User Id:
UserId = "1" // the unique Id of the user in the cloud system. It can be any number or string, but must be globally unique.
User name:
Name = "Han Meimei" // User display name, which is used to display the user name when pushing or when the client does not provide user information.
User profile picture:
PortraitUri = "http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png"
Now we have obtained the AppKey and Token.
9. The following is the quick integration.
9.1 import the header file in the AppDelegate. h file of your project.
# Import <RongIMLib/RongIMLib. h>
# Impor <RongIMKit/RongIMKit. h>
Then follow the RCIMConnectionStatusDelegate proxy method, that is, @ interface AppDelegate: UIResponder <UIApplicationDelegate, RCIMConnectionStatusDelegate>
9.2 import the header file in the AppDelegate. m file
// Instant messaging
# Import <RongIMKit/RongIMKit. h>
# Import <RongIMLib/RongIMLib. h>
# Import <UIKit/UIKit. h>
Then, write the obtained AppKey to a macro and replace your AppKey with the following macro \
K51hidwq1bbcdds4b replace this with your own one.
// Melt cloud instant messaging AppKey
# Define RONGCLOUD_IM_APPKEY @ "k51hidwq1bbcdds4b"
10. In the AppDelegate. m file
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
}
Add the following code to the method:
// Instant messaging
// Initialize the rongyun SDK.
[[RCIM sharedRCIM] initWithAppKey: RONGCLOUD_IM_APPKEY];
/**
* Push processing 1
*/
If ([application
RespondsToSelector: @ selector (registerUserNotificationSettings :)]) {
// Register for push, iOS 8
UIUserNotificationSettings * settings = [UIUserNotificationSettings
SettingsForTypes :( UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
Categories: nil];
[Application registerUserNotificationSettings: settings];
} Else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound;
[Application registerForRemoteNotificationTypes: myTypes];
}
// Instant messaging
[[Nsicationicationcenter defacenter center]
AddObserver: self
Selector: @ selector (didReceiveMessageNotification :)
Name: RCKitDispatchMessageNotification
Object: nil];
[[RCIM sharedRCIM] setConnectionStatusDelegate: self];
Add the code to the method here
The following is a separate method directly added to the AppDelegate. m file.
/**
* Send the obtained devicetoken to rongyun for offline push. Your app background must upload the push certificate.
*
* @ Param application <# application description #>
* @ Param deviceToken <# deviceToken description #>
*/
-(Void) application :( UIApplication *) application
DidRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {
NSString * token =
[[[DeviceToken description] stringByReplacingOccurrencesOfString: @ "<"
WithString: @ ""]
StringByReplacingOccurrencesOfString: @ ">"
WithString: @ ""]
StringByReplacingOccurrencesOfString :@""
WithString: @ ""];
[[RCIMClient sharedRCIMClient] setDeviceToken: token];
}
/**
* Network status changes.
*
* @ Param status: network status.
*/
-(Void) onRCIMConnectionStatusChanged :( RCConnectionStatus) status {
If (status = ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT ){
UIAlertView * alert = [[UIAlertView alloc]
InitWithTitle: @ "prompt"
Message: @ "you"
@ "The account is logged on to another device. You are forced to go offline! "
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil, nil];
[Alert show];
// Note that the following four lines can be modified or commented out as needed, but only the four lines can be commented out. The network status change method must be implemented.
ViewController * loginVC = [[ViewController alloc] init];
UINavigationController * _ navi =
[[UINavigationController alloc] initWithRootViewController: loginVC];
Self. window. rootViewController = _ navi;
}
}
-(Void) didReceiveMessageNotification :( NSNotification *) notification {
[UIApplication sharedApplication]. applicationIconBadgeNumber =
[UIApplication sharedApplication]. applicationIconBadgeNumber + 1;
}
11. To create a session, first create a controller that inherits the name of RCConversationListViewController ChatListViewController. The H file is
# Import <UIKit/UIKit. h>
# Import <RongIMKit/RongIMKit. h>
@ Interface ChatListViewController: RCConversationListViewController
@ End
This style
Import the header file in the. h file of the Controller on the interface where you want to create an instant session // enable cloud instant messaging
# Import <RongIMKit/RongIMKit. h>
According to the data source method, RCIMUserInfoDataSource is changed
# Import <RongIMKit/RongIMKit. h>
@ Interface ViewController: UIViewController <RCIMUserInfoDataSource>
Import the header file to the. m file
// Instant messaging
# Import "ChatListViewController. h"
# Import <RongIMKit/RCConversationViewController. h>
Define the Token we get as a macro. just replace the format with your own Token.
// Rongyun instant messaging Token
# Define RONGCLOUD_IM_Token @ "assets/irxlhvshfhvlm8f1_duu + y4TIhufZfJ/fix1_rqybu8cjan2bcaola ="
For example, in my controller, I have a start answer button.
I want to create an instant session by clicking the start answer button on this controller.
In this way, click the start answer button.
/**
* Click start to answer the execution method.
*/
-(Void) startAnswer
{
// Log on to rongyun
// Log on to the rongyun server. You can obtain the token from the rongyun API debugging website in the initial stage. Then, the token must be obtained from the server to the rongyun server.
NSString * token = RONGCLOUD_IM_Token;
[[RCIM sharedRCIM] connectWithToken: token success: ^ (NSString * userId ){
// Set the user information provider. The user profile and nickname displayed on the page will be retrieved from this proxy. This will jump to the session List interface, which means that we usually have
If you want to directly jump to the chat interface
[[RCIM sharedRCIM] setUserInfoDataSource: self];
NSLog (@ "Login successfully with userId: % @.", userId );
Dispatch_async (dispatch_get_main_queue (), ^ {
ChatListViewController * chatListViewController = [[ChatListViewController alloc] init];
[Self. navigationController pushViewController: chatListViewController animated: YES];
});
} Error: ^ (RCConnectErrorCode status ){
NSLog (@ "login error status: % ld.", (long) status );
} TokenIncorrect: ^ {
NSLog (@ "the token is invalid. Make sure that the appkey used to generate the token is consistent with the appkey during initialization ");
}];
} Error: ^ (RCConnectErrorCode status ){
NSLog (@ "login error status: % ld.", (long) status );
} TokenIncorrect: ^ {
NSLog (@ "the token is invalid. Make sure that the appkey used to generate the token is consistent with the appkey during initialization ");
}];
YYCLog (@ "click to start answering ");
}
Then the following method is implemented in this controller:
/**
* In this method, the information to be provided to the cloud users is recommended to be cached locally, and then the method is changed to return information from your cache each time.
*/
-(Void) getUserInfoWithUserId :( NSString *) userId completion :( void (^) (RCUserInfo * userInfo) completion
{
// Here, a user information is written to demonstrate
If ([@ "1" isEqual: userId]) {
RCUserInfo * user = [[RCUserInfo alloc] init];
User. userId = @ "1 ";
User. name = @ "Test 1 ";
User. portraitUri = @ "https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008 ";
Return completion (user );
} Else if ([@ "2" isEqual: userId]) {
RCUserInfo * user = [[RCUserInfo alloc] init];
User. userId = @ "2 ";
User. name = @ "Test 2 ";
User. portraitUri = @ "https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008 ";
Return completion (user );
}
}
This method must also be implemented in this. m file.
Here, we will test how to implement it first by writing it to the end.
The code below is the same
In our ChatListViewController. h file
# Import <RongIMKit/RongIMKit. h>
# Import <RongIMKit/RongIMKit. h>
@ Interface ChatListViewController: RCConversationListViewController
@ End