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需要手動簽名,簽名代碼之後研究一下,暫時沒找到可以使用的簽名代碼直接接入