Giter Site home page Giter Site logo

mijiaauthsdk-ios's Introduction

#授权方式 目前授权方式分两种:

#iOS 设备授权使用说明

1、注册授权app。

开放平台 注册账户并创建app

2、项目内置sdk。

在AppDelegate 的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    //注册appid
    [MiApi registerApp:@"申请的app id"];
}

在info.plist 里面添加新的URL types

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>OpenMiHome</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>XM申请的appid</string>
        </array>
    </dict>
</array>

假如appid为1234567,AppDelegate 与info.plist里面的写法如下

[MiApi registerApp:@"1234567"];

info.plist写法如下

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
            <string>OpenMiHome</string>
            <key>CFBundleURLSchemes</key>
            <array>
            <string>XM1234567</string>
        </array>
    </dict>
</array>

语音设备授权调用方式

MiSendDeviceAuthReq *request = [[MiSendDeviceAuthReq alloc] init];
request.did = did ;
request.userid = userid;
BOOL opt = [MiApi sendAuthReq:request viewController:self delegate:self];
if(opt == NO){
    NSLog(@"米家 app 没有安装");
    [self downloadApp];
}

可参考Demo 中的 MHBindKeyTableViewController.h 或者 MHBindKeyTableViewController.m 的sendDeviceAuthRequest方法, 绑定绑定账户并授权调用方式

MiSendBindKeyReq *request = [[MiSendBindKeyReq alloc] init];
NSString* bindKey = _bindKeyField.text ;
if(bindKey == nil || [bindKey isEqualToString:@""]){
self.hintLabel.text = @"bindKey 为空";
return;
}

request.bindKey = bindKey ;//@"58067462";
//    request.sid =  _bindKeyField.text;
request.model = _modelField.text;

BOOL opt = [MiApi sendAuthReq:request viewController:self delegate:self];
if(opt == NO){
NSLog(@"米家 app 没有安装");
[self downloadApp];
}

代码里面的delegate 需要实现 MiApiDelegate。当授权完成后,米家调起对应的app。 需要实现

 - (void)onResp:(MiBaseResp *)resp

方法,并且在AppDelegate的application:openURL:sourceApplication 的方法中添加如下代码

BOOL handleRet = [MiApi handleOpenURL:url delegate:self];

可参考Demo 中的 MHBindKeyTableViewController.h 和 MHBindKeyTableViewController.m 的onResp代码 ,具体代码如下

- (void)handleResponseV1:(MiBaseResp *)resp{
    MiSendDeviceAuthResp* dResp = (MiSendDeviceAuthResp*)resp;
    
    if (resp.errCode != MiDeviceAuthRespCodeSuccess) {
        //resp.result
        NSMutableString* ms = [[NSMutableString alloc] initWithCapacity:100];
        [ms appendString:@"授权失败,请查看result字段:"];
        //resp.errStr;
        if(dResp.result != nil){
            
            id obj = [NSJSONSerialization JSONObjectWithData:[dResp.result dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
            if([obj isKindOfClass:[NSArray class]]){
                NSArray* array = (NSArray*)obj;
                //每个设备成功失败都有一个单独的错误码。遍历数组查看
                for(id o in array){
                    if([o isKindOfClass:[NSDictionary class]]){
                        NSDictionary* d = (NSDictionary*)o;
                        NSNumber* n = [d objectForKey:@"code"];
                        if(n){
                            switch(n.integerValue){
                                case MiDeviceAuthRespCodeNotOwner:{
                                    [ms appendString:@"当前用户不是设备所有者"];
                                    break;
                                }
                                    
                                case MiDeviceAuthRespCodeServiceException:{
                                    [ms appendString:@"服务异常,如存储服务访问异常"];
                                    break;
                                }
                                case MiDeviceAuthRespCodeNotVoiceDevice:{
                                    [ms appendString:@"语音设备不存在"];
                                    break;
                                }
                                case MiDeviceAuthRespCodeNotSupportVoiceCtl:{
                                    [ms appendString:@"该设备不支持语音控制"];
                                    
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        self.hintLabel.text = ms;
    } else {
        self.hintLabel.text = @"授权成功";
    }
}

- (void)handleResponseV2:(MiBaseResp *)resp{
    NSMutableString* msg = [[NSMutableString alloc] initWithCapacity:1000];
    [msg appendString:[MiApi respCodeString:resp.respCode]];
    if(resp.errStr){
        [msg appendString:resp.errStr];
    }
    self.hintLabel.text = msg;
    [self.hintLabel sizeToFit];
}

- (void)onResp:(MiBaseResp *)resp{
    if (resp.errCode != MiDeviceAuthRespCodeSuccess) {
    /*
         得到具体的错误码,这里分sdk的版本,如果是2.0版本以后,可以查看resp对象的respCode
         的字段,这个字段的类型是MijiaAuthCode。
         如果是2.0之前的版本,还是通过resp的errCode的属性来判断,errCode的类型为MiDeviceAuthRespCode。
         errCode 与respCode 的区别在于respCode的信息更详细。它包括了errCode里面的所有错误类型。
         而且值的代表的意思是跟android一样的。
         errCode是最早的定义的,错误类型有限,如果只是简单的判断授权是否成功,可以使用errCode来判断,但是如果想給用户更详细的信息,请使用respCode的。
    */
        if(resp.sdkVersion < 2.0){
            [self handleResponseV1:resp];
        }else{
            [self handleResponseV2:resp];
        }
    } else {
        self.hintLabel.text = @"授权成功";
        
    }
    [self getDeviceList];
}

3、Demo 代码说明

设备授权 具体代码在 MHDeviceAuthRequestViewController.m 文件中 。sdk的framework为MiNetworkFramework.framework

如果是要给某个设备访问其他设备的权限,运行Demo后请选择“设备授权”

绑定账户并授权 具体代码在 MHBindKeyTableViewController.m 里面。 这时要注意的是,如果使用2.0的以后的SDK。respCode的值的区别。 如果respCode 为 kMijiaAuthSuccess,就是说绑定和授权都成功了。 如果respCode 为kMijiaAuthBindSuccess,就是说绑定成功,但是授权没有成功,(授权没有成功可能是用户点击返回按钮,或者服务器在授权的时候错误)

bindKey 具体代码在MHBindKeyTableViewController.m 。

mijiaauthsdk-ios's People

Contributors

chundong avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

paladinzh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.