OC-UI阶段学习43-第三方开发平台的使用--环信、ShareSDK和科大讯飞

很多时候我们需要使用一些第三方平台提供的功能,当然自己也可以写,如果够牛逼。

一、环信

即时通信的第三方有很多,融云、Leancloud等,都可以帮助我们快速实现扣扣微信这样的即时聊天功能,以及附带的登录注册等功能。

环信怎么用:

1.这次开发者账号
2.创建应用
3.下载SDK,这里要注意,下载对应的平台和版本
4.导入SDK到工程,并根据要求导入相应的依赖库
5.配置工程,在Build Settings->Linking-> Other Link Flags中添加-Objc,如果-Objc冲突,则添加-force_load来解决

说的很简单,因为官方有详细的说明文档
http://docs.easemob.com/im/300iosclientintegration/20iossdkimport
讲的很明白,如果使用中有问题解决不了,可以找客服帮助

贴点代码,好歹说明我用过


//构造一个消息
-(void)makeMessage
{
    //要发送的消息
    EMTextMessageBody *body = [[EMTextMessageBody alloc]initWithText:_contentTextField.text];
    //当前登录的账号
    NSString *from = [[EMClient sharedClient]currentUsername];

    //生成message ext 字典 传参数的
    EMMessage *message = [[EMMessage alloc]initWithConversationID:_contactName from:from to:_contactName body:body ext:nil];

    //设置消息类型
    message.chatType = EMChatTypeChat;//单聊

    //发送消息
    [[EMClient sharedClient].chatManager asyncSendMessage:message progress:nil completion:^(EMMessage *message, EMError *error) {
        if(error)
        {
            NSLog(@"发送消息失败 -- %d",error.code);
        }
        else
        {
            NSLog(@"发送消息成功");
            [self.allDataMArray addObject:message];
            //回到主线程
            [self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
        }        
    }];
}

#pragma mark -- 接收到消息
-(void)receiveMessage:(NSNotification*)notification
{
    //取出消息们
    NSArray *msgArray = notification.object;
    for (EMMessage *itemMsg in msgArray) {
        //判断消息是否为当前对话的消息
        if ([itemMsg.from isEqualToString:self.contactName]) {
            [_allDataMArray addObject:itemMsg];
        }           
    }
    //刷新
    [_myTableView reloadData];
}

二、SharedSDK

http://www.mob.com/ 里面有很多有用的第三方功能,常用有SharedSDK可以第三方登录,还有一个短信验证的SDK,它还提供很多数据API,可以从上面取得需要的新闻、天气、菜谱等数据。

怎么注册账号,设置工程就不说了
如果我们要使用新浪微博的第三方登录,那么还需要去注册一个新浪微博的开发账号,创建应用,得到appkey。其他微信扣扣,也是一样的。
这个网站是[ShareSDK各社交平台申请APPkey 的网址及申请流程汇总]
http://bbs.mob.com/forum.php?mod=viewthread&tid=275&page=1&extra=#pid860


//基本配置
-(void)sharedSDKOptions{
    //SharedSDK的appkey
    [ShareSDK registerApp:@"113b8684926ce" activePlatforms:@[@(SSDKPlatformTypeSinaWeibo)] onImport:^(SSDKPlatformType platformType) {
        switch (platformType)
        {
                // 新浪微博
            case SSDKPlatformTypeSinaWeibo:
                [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                break;

            default:
                break;
        }
    } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
        //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权
//这些数据要注册新浪微博第三方
        [appInfo SSDKSetupSinaWeiboByAppKey:@"2951738742"
                                  appSecret:@"a7aef8a4b9f877472887bb740be8ab69"
                                redirectUri:@"http://www.sharesdk.cn"
                                   authType:SSDKAuthTypeBoth];
    }];    
}

登录方法

//登录按钮
- (IBAction)loginAction:(id)sender {
    //登录并得到用户信息
    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
         //打印得到的用户信息
             NSLog(@"uid=%@",user.uid);
             NSLog(@"%@",user.credential);
             NSLog(@"token=%@",user.credential.token);
             NSLog(@"nickname=%@",user.nickname);
         }
         else
         {
             NSLog(@"%@",error);
         }         
     }];
}

三、科大讯飞

科大讯飞提供了各种语音功能,主要两种,语音转文字,文字转语音,这种功能我们自己肯定写不出来。
注册等准备工程都是一样的,官方文档也有说明

贴个语音识别成文字的代码


#import "RootViewController.h"
#import <iflyMSC/IFlyRecognizerViewDelegate.h>
#import <iflyMSC/IFlyRecognizerView.h>
#import <iflyMSC/IFlySpeechConstant.h>
#import <iflyMSC/IFlySpeechError.h>
#import "SecondViewController.h"

@interface RootViewController ()<IFlyRecognizerViewDelegate>

@property (nonatomic,retain)UITextView *myTextView;//用来显示文字
@property (nonatomic,retain)UIButton *myBtn;//开始录音识别
@property (nonatomic,retain)IFlyRecognizerView *myRecognizerView;//讯飞提供的录音界面

@end

@implementation RootViewController

//懒加载
-(UITextView *)myTextView
{
    if (!_myTextView) {
        _myTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 60, CGRectGetWidth(self.view.bounds), 200)];
    }
    return _myTextView;
}
-(UIButton *)myBtn
{
    if (!_myBtn) {
        _myBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _myBtn.frame = CGRectMake(100, 340, 100, 50);
        [_myBtn setTitle:@"开始录音" forState:UIControlStateNormal];
        [_myBtn addTarget:self action:@selector(startReAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _myBtn;
}

-(IFlyRecognizerView *)myRecognizerView
{
    if (!_myRecognizerView) {
        _myRecognizerView = [[IFlyRecognizerView alloc]initWithCenter:self.view.center];
        _myRecognizerView.delegate = self;
        //设置应用领域
        [_myRecognizerView setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];
        //设置录音文件保存路径
        [_myRecognizerView setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
        //启动识别服务
// [_myRecognizerView start];
    }    
    return _myRecognizerView;
}

//实现按钮的回调方法
-(void)startReAction
{
    if (self.myRecognizerView) {
        //如果存在,先取消
        [self.myRecognizerView cancel];
        // [self.myRecognizerView removeFromSuperview];//不用手动
        [self.view addSubview:self.myRecognizerView];
       //开始录音
        [self.myRecognizerView start];
    }
    else
    {
        NSLog(@"myRecognizer 懒加载有问题");
    }
}

#pragma mark -- 识别语音带界面的代理方法

//识别结果的返回
-(void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast
{
    //声明可变字符串,用来拼接所有的key
    NSMutableString *keyString = [[NSMutableString alloc]init];
    //从数组中取出字典
    NSDictionary *dic_fromResults = resultArray.firstObject;
    //拼接key
    for (NSString *key in dic_fromResults) {
        [keyString appendString:key];
    }
    //从拼接好的keyString中取出我们所需要的文字
    NSString * resultFromJson =  [self stringFromJson:keyString];
    if (self.myTextView.text) {
        //说明有文字
        self.myTextView.text = [NSString stringWithFormat:@"%@%@",self.myTextView.text,resultFromJson];
    }
    else
    {
        self.myTextView.text = @"没有文字";
    }

    NSLog(@"array - %@",resultArray);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.myBtn];
    [self.view addSubview:self.myTextView];

    self.view.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];  
}

/** 语音识别返回的数据是json格式 解析听写json格式的数据 params例如: {"sn":1,"ls":true,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"w":"白日","sc":0}]},{"bg":0,"cw":[{"w":"依山","sc":0}]},{"bg":0,"cw":[{"w":"尽","sc":0}]},{"bg":0,"cw":[{"w":"黄河入海流","sc":0}]},{"bg":0,"cw":[{"w":"。","sc":0}]}]} ****/
- (NSString *)stringFromJson:(NSString*)params
{
    if (params == NULL) {
        return nil;
    }

    NSMutableString *tempStr = [[NSMutableString alloc] init];
    NSDictionary *resultDic  = [NSJSONSerialization JSONObjectWithData:    //返回的格式必须为utf8的,否则发生未知错误
                                [params dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];

    if (resultDic!= nil) {
        NSArray *wordArray = [resultDic objectForKey:@"ws"];

        for (int i = 0; i < [wordArray count]; i++) {
            NSDictionary *wsDic = [wordArray objectAtIndex: i];
            NSArray *cwArray = [wsDic objectForKey:@"cw"];

            for (int j = 0; j < [cwArray count]; j++) {
                NSDictionary *wDic = [cwArray objectAtIndex:j];
                NSString *str = [wDic objectForKey:@"w"];
                [tempStr appendString: str];
            }
        }
    }
    return tempStr;
}
@end

你可能感兴趣的:(sdk,第三方,开发人员,即时通信,科大讯飞)