1.首先在pod文件中,添加mqttclient包
platform :ios, '9.0'
target 'MQTT-ClientText' do
pod 'MQTTClient'
end
2.控制器代码如下
#import "MQTTClient.h"
@interface ViewController ()<MQTTSessionDelegate>
@property (nonatomic,strong)MQTTSessionManager *manager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.manager = [[MQTTSessionManager alloc]init];
self.manager.delegate = self;
NSString *realmName = @"productkey.iot-as-mqtt.cn-shanghai.aliyuncs.com";
NSString *userName = @"test&xxxx";
NSString *password = @"xxxxxxxx";
//
[self.manager connectTo:realmName port:1883
tls:NO
keepalive:60
clean:NO auth:NO
user:userName
pass:password
will:nil
willTopic:nil willMsg:nil
willQos:0
willRetainFlag:YES
withClientId:@"xxxx"
securityPolicy:nil
certificates:nil
protocolLevel:0
connectHandler:nil];
}
ps:此处的password需要手动签名,签名代码之后研究一下,暂时没找到可以使用的签名代码直接接入