MASConnecta 2.0.00

MASConnecta 2.0.00

许可证 MIT
发布最后发布2019年10月

Luis SanchesRob WeberJames GoBritton KatnichYasmeen TajSyed YusufSyed Yusuf 维护。



 
依赖项
MASFoundation= 2.0.00
MASIdentityManagement= 2.0.00
 

MASConnecta 是 iOS 移动 SDK 的核心消息框架,它是 CA 移动 API 网关的一部分。它使开发者能够创建用户可以相互发送消息和数据的社交协作应用。

功能

MASConnecta 框架提供以下功能

  • 安全可靠的用户间消息传递
  • 内置相互 SSL 和 OAuth 支持的 MQTT 客户端

开始使用

通信

  • 有任何疑问或需要帮助?,请使用 Stack Overflow。 (标记为 'massdk')
  • 发现错误?,打开问题报告并说明如何重现操作。
  • 要求新功能或有想法?,打开问题报告。

您可以如何做出贡献

欢迎并感谢贡献。要了解更多信息,请查看 贡献指南

安装

MASConnecta 支持多种方法将库安装到项目中。

CocoaPods (Podfile) 安装

要使用 CocoaPods 将 MASConnecta 集成到 Xcode 项目中,请在您的 Podfile 中指定它:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'

pod 'MASConnecta'

然后,使用您的项目文件夹的命令提示符运行以下命令:

$ pod install

手动安装

手动安装时,您需要在 Xcode 项目中添加 Mobile SDK。注意,您必须添加 MASFoundation 库。为了完全启用 MAS 功能,安装所有MAS库,如所示。

  1. 在 Xcode 中打开您的项目。
  2. 将 SDK 库文件拖放到 Xcode 左侧导航面板中的项目中。选择选项 如果需要则复制项
  3. 选择 文件->将文件添加到 '项目名称' 并将项目文件夹中的 msso_config.json 文件添加到项目中。
  4. 在 Xcode "项目"目标中,将 -ObjC 添加为 其他链接器标志 的值。
  5. 将以下 Mobile SDK 库头文件导入到类中或如果您的项目有一个 .pch 文件,则导入到 .pch 文件中。
#import <MASFoundation/MASFoundation.h>
#import <MASConnecta/MASConnecta.h>

使用方法

消息传递

将 MASConnecta 添加到项目后,MASFoundation 库的一些对象将自动显示消息传递方法。这为您节省了开发时间,并使代码更加整洁。您只需编写几行代码,库就会自动处理与服务器连接的所有设置。

发送消息

//Authenticated users have the ability to send messages (Text, Data, Image) to a user

MASUser *myUser = [MASUser currentUser];
MASUser *userB = Some user retrieved from the server

[myUser sendMessage:@"Hello World" toUser:userB completion:^(BOOL success, NSError * _Nullable error) {
    
    NSLog(@"Message Sent : %@\nerror : %@", success ? @"YES":@"NO", error);
}];

//Authenticated users can send messages (Text, Data, Image) to a user on a specific topic

MASUser *myUser = [MASUser currentUser];
MASUser *userB = Some user retrieved from the server

//
// Get image from App Bundle
//
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"];
NSData *message = [NSData dataWithContentsOfFile:filePath];

//
// Create MASMessage object
//
MASMessage *messageImage = [[MASMessage alloc] initWithPayloadData:message contentType:@"image/jpeg"];
//
// Send Message to Recipient
//
[myUser sendMessage:messageImage toUser:userB onTopic:@"vacations" completion:^(BOOL success, NSError * _Nullable error) {
    
    NSLog(@"Message Sent : %@\nerror : %@", success ? @"YES":@"NO", error);
}];
        

开始监听消息

开始监听我的消息

- (void)viewDidLoad
{
  //
  //Get the current authenticated user
  //
  MASUser *myUser = [MASUser currentUser];
  
    //
    //Listen to Messages sent to my User
    //
    [myUser startListeningToMyMessages:^(BOOL success, NSError *error) {
        
        if (success) {
            
            NSLog(@"Success subscribing to myUser topic!");
        }
        else {
            
            NSLog(@"%@",error.localizedDescription);
        }
    }];
}

停止监听消息

停止监听我的消息

- (void)viewDidLoad
{
  //
  //Get the current authenticated user
  //
  MASUser *myUser = [MASUser currentUser];
  
    //
    //Stop Listening to Messages sent to my User
    //
    [myUser stoplisteningToMyMessages:nil];
}

处理传入的消息

使用通知

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveMessageNotification:)
                                                 name:MASConnectaMessageReceivedNotification
                                               object:nil];
}
- (void)didReceiveMessageNotification:(NSNotification *)notification
{    
    //
    //Get the Message Object from the notification
    //
    __weak typeof(self) weakSelf = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        MASMessage *myMessage = notification.userInfo[MASConnectaMessageKey];

        [weakSelf.profileImage setImage:myMessage.payloadTypeAsImage];
        [weakSelf.messagePayload setText:myMessage.payloadTypeAsString];
    });   
}

发布/订阅

MASConnecta库公开了对象,允许您:

例如

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //
    //Creating a new MQTT client
    //
    MASMQTTClient *client = [[MASMQTTClient alloc] initWithClientId:@"myClientID" cleanSession:YES];
    
    
    //
    //Connecting the mqtt client to a host
    //
    [client connectWithHost:@"mas.ca.com" withPort:8883 enableTLS:YES completionHandler:^(MQTTConnectionReturnCode code) {
        
        //Your code here
    }];
    
    
    //
    //Handling messages that arrive
    //
    [client setMessageHandler:^(MASMQTTMessage *message) {
        
        //Your code here
    }];
    
    
    //
    //Subscribing to a topic
    //
    [client subscribeToTopic:@"caTopic" withQos:ExactlyOnce completionHandler:^(NSArray *grantedQos) {
        
        //Your code here
    }];
    
    
    //
    //Publishing a message to a topic
    //
    [client publishString:@"Hello World" toTopic:@"caTopic" withQos:ExactlyOnce retain:YES completionHandler:^(int mid) {
        
        //Your code here
    }];
}

许可证

版权所有(C) 2016 CA。保留所有权利。

本软件可根据MIT许可证的条款进行修改和分发。
有关详情,请参阅LICENSE文件。