如果您还未创建爱萌开发者帐号,请前往爱萌即时通讯官网注册。
其中,用户名、密码、邮件为必填项,其它均为可选项。
如下图,创建应用的表单分为三部分,分别是:基础信息、Android部分、iOS部分。iOS平台只需要填写基础信息和iOS部分
应用创建成功后,将跳转到应用设置页面。
系统生成关键信息:App Key和Master Secret Key。
请前往IMSDK Lib下载页,下载iOS完整SDK。
包名结构:IMSDK-iOS_v[版本号]
解压Lib包:,其目录结构如下图。共包含两个目录,内容相仿,分别对应真机和模拟器开发
以模拟器开发库为例,Release-iphonesimulator共包含一个.a文件和两个子目录
下面以模拟器调试为例,演示如何快速集成IMSDK无界面功能。
该APP有两个界面如上图,当点击左侧的注册或登录,成功会切换至右侧界面;
将Release-iphonesimulator拖至项目中,并勾选“Copy items if needed…”
最终项目目录结构如下
* CoreLocation
* CoreTelephony
* SystemConfiguration
* ImageIO
* QuartzCore
* CoreTelephony
设置链接选项:-licucore、 -ObjC
/* 在AppDelegate.m中,添加如下代码 */
// 添加引用
#import "IMSDK.h"
#import "IMMyself.h"
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MainViewController *rootView = [[MainViewController alloc] init];
rootView.title = @"我的测试应用";
NSLog(@"IMSDK init");
[g_pIMSDK initWithAppKey:@"{此处填写你在步骤2中生成的app_key}"];
return YES;
}
// 以下代码用于IMSDK辅助功能,如接收APNs、set角标或者应用在前后台切换处理
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[g_pIMSDK setDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"register remotoNotification failed with error:%@",error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[g_pIMSDK applicationWillEnterForeground];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[g_pIMSDK applicationDidEnterBackground];
}
/* 注册按钮的点击事件关键代码 */
NSString *strCid = [_customUserIDTextField text];
NSString *strPwd = [_passwordTextField text];
NSLog( @"login - %@|%@", strCid, strPwd);
[g_pIMMyself setCustomUserID:strCid];
[g_pIMMyself setPassword:strPwd];
[g_pIMMyself registerWithTimeoutInterval:0 success:^{
MainViewController *mainView = [[MainViewController alloc] init];
mainView.view.backgroundColor = RGB(255, 255, 255);
[[self navigationController] pushViewController: mainViewanimated:YES];
} failure:^(NSString *e) {
NSLog(@"%@", e);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"注册失败 - %@", e] message:e delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];
[alertView show];
}];
/* 登录按钮的点击事件关键代码 */
NSString *strCid = [_customUserIDTextField text];
NSString *strPwd = [_passwordTextField text];
NSLog( @"login - %@|%@", strCid, strPwd);
[g_pIMMyself setCustomUserID:strCid];
[g_pIMMyself setPassword:strPwd];
[g_pIMMyself loginWithTimeoutInterval:0 success:^{
MainViewController *mainView = [[MainViewController alloc] init];
mainView.view.backgroundColor = RGB(255, 255, 255);
[[self navigationController] pushViewController: mainViewanimated:YES];
} failure:^(NSString *e) {
NSLog(@"%@", e);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"注册失败 - %@", e] message:e delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];
[alertView show];
}];
/* 发送按钮关键代码 */
NSString *strCid = [_txtCid text];
NSString *strMsg = [_txtMsg text];
NSLog( @"send text - %@|%@", strCid, strMsg);
[g_pIMMyself sendText:strMsg toUser:strCid success:^{
NSLog(@"send to %@ success", strCid);
//呈现到UITextView
[self appendTextToView:strCid msg:strMsg time:[NSDate date]];
[_txtMsg setText:nil];
} failure:^(NSString *e) {
NSLog(@"%@", e);
//呈现到UITextView
[self appendTextToView:strCid msg:[NSString stringWithFormat:@"%@ - 发送失败", strMsg] time:[NSDate date]];
} ];
/* 接收消息关键代码 */
// 在处理接收消息的类上实现协议IMMyselfDelegate,此处为第二个Controller
@interface MainViewController ()<UITextFieldDelegate,IMMyselfDelegate>
//实现协议的方法,在p2p消息到达时触发
- (void)didReceiveText:(NSString *)text fromCustomUserID:(NSString *)customUserID serverSendTime:(UInt32)timeIntervalSince1970 {
//呈现至UITextView
[self appendTextToView:customUserID msg:text time:[NSDate dateWithTimeIntervalSince1970:timeIntervalSince1970]];
}
至此,一个简单的聊天APP就完成了。
IMSDK下载 - http://www.imsdk.im/download
更多API - http://docs.imsdk.im/