/************************************************************ * * Hyphenate CONFIDENTIAL * __________________ * Copyright (C) 2016 Hyphenate Inc. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of Hyphenate Inc. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Hyphenate Inc. */ #import #import "EaseSDKHelper.h" #import "EaseConvertToCommonEmoticonsHelper.h" //@interface EMChatImageOptions : NSObject // //@property (assign, nonatomic) CGFloat compressionQuality; // //@end static EaseSDKHelper *helper = nil; @implementation EaseSDKHelper @synthesize isShowingimagePicker = _isShowingimagePicker; - (instancetype)init { self = [super init]; if (self) { [self commonInit]; } return self; } +(instancetype)shareHelper { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ helper = [[EaseSDKHelper alloc] init]; }); return helper; } #pragma mark - private - (void)commonInit { } #pragma mark - app delegate notifications /** @brief 注册App切入后台和进入前台的通知 */ - (void)_setupAppDelegateNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackgroundNotif:) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void)appDidEnterBackgroundNotif:(NSNotification*)notif { [[EMClient sharedClient] applicationDidEnterBackground:notif.object]; } - (void)appWillEnterForeground:(NSNotification*)notif { [[EMClient sharedClient] applicationWillEnterForeground:notif.object]; } #pragma mark - register apns /** @brief 注册远程通知 */ - (void)_registerRemoteNotification { UIApplication *application = [UIApplication sharedApplication]; application.applicationIconBadgeNumber = 0; if (NSClassFromString(@"UNUserNotificationCenter")) { [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError *error) { if (granted) { #if !TARGET_IPHONE_SIMULATOR dispatch_async(dispatch_get_main_queue(), ^{ [application registerForRemoteNotifications]; }); #endif } }]; return; } if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil]; [application registerUserNotificationSettings:settings]; } #if !TARGET_IPHONE_SIMULATOR if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) { [application registerForRemoteNotifications]; }else{ UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes]; } #endif } #pragma mark - init Hyphenate - (void)hyphenateApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions appkey:(NSString *)appkey apnsCertName:(NSString *)apnsCertName otherConfig:(NSDictionary *)otherConfig { [self _setupAppDelegateNotifications]; [self _registerRemoteNotification]; EMOptions *options = [EMOptions optionsWithAppkey:appkey]; options.apnsCertName = apnsCertName; options.isAutoAcceptGroupInvitation = NO; if ([otherConfig objectForKey:kSDKConfigEnableConsoleLogger]) { options.enableConsoleLog = YES; } BOOL isHttpsOnly = NO; if ([otherConfig objectForKey:@"httpsOnly"]) { isHttpsOnly = [[otherConfig objectForKey:@"httpsOnly"] boolValue]; } options.usingHttpsOnly = isHttpsOnly; NSUserDefaults *udefaults = [NSUserDefaults standardUserDefaults]; if ([udefaults objectForKey:@"autoTransferMessageFile"]) { options.isAutoTransferMessageAttachments = [[udefaults objectForKey:@"autoTransferMessageFile"] boolValue]; } if ([udefaults objectForKey:@"autoDownloadMessageThumbnail"]) { options.isAutoDownloadThumbnail = [[udefaults objectForKey:@"autoDownloadMessageThumbnail"] boolValue]; } BOOL sandBox = [otherConfig objectForKey:@"easeSandBox"] && [[otherConfig objectForKey:@"easeSandBox"] boolValue]; if (!sandBox) { [[EMClient sharedClient] initializeSDKWithOptions:options]; } } - (void)hyphenateApplication:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [[EMClient sharedClient] application:application didReceiveRemoteNotification:userInfo]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - send message + (EMMessage *)getTextMessage:(NSString *)text to:(NSString *)to messageType:(EMChatType)messageType messageExt:(NSDictionary *)messageExt { NSString *willSendText = [EaseConvertToCommonEmoticonsHelper convertToCommonEmoticons:text]; EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:willSendText]; NSString *from = [[EMClient sharedClient] currentUsername]; EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt]; message.chatType = messageType; return message; } + (EMMessage *)getCmdMessage:(NSString *)action to:(NSString *)to messageType:(EMChatType)messageType messageExt:(NSDictionary *)messageExt cmdParams:(NSArray *)params { EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:action]; if (params) { body.params = params; } NSString *from = [[EMClient sharedClient] currentUsername]; EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt]; message.chatType = messageType; return message; } + (EMMessage *)getLocationMessageWithLatitude:(double)latitude longitude:(double)longitude address:(NSString *)address to:(NSString *)to messageType:(EMChatType)messageType messageExt:(NSDictionary *)messageExt { EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:latitude longitude:longitude address:address]; NSString *from = [[EMClient sharedClient] currentUsername]; EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt]; message.chatType = messageType; return message; } + (EMMessage *)getImageMessageWithImageData:(NSData *)imageData to:(NSString *)to messageType:(EMChatType)messageType messageExt:(NSDictionary *)messageExt { EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:imageData displayName:@"image"]; NSString *from = [[EMClient sharedClient] currentUsername]; EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt]; message.chatType = messageType; return message; } + (EMMessage *)getImageMessageWithImage:(UIImage *)image to:(NSString *)to messageType:(EMChatType)messageType messageExt:(NSDictionary *)messageExt { NSData *data = UIImageJPEGRepresentation(image, 1); return [self getImageMessageWithImageData:data to:to messageType:messageType messageExt:messageExt]; } + (EMMessage *)getVoiceMessageWithLocalPath:(NSString *)localPath duration:(NSInteger)duration to:(NSString *)to messageType:(EMChatType)messageType messageExt:(NSDictionary *)messageExt { EMVoiceMessageBody *body = [[EMVoiceMessageBody alloc] initWithLocalPath:localPath displayName:@"audio"]; body.duration = (int)duration; NSString *from = [[EMClient sharedClient] currentUsername]; EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt]; message.chatType = messageType; return message; } + (EMMessage *)getVideoMessageWithURL:(NSURL *)url to:(NSString *)to messageType:(EMChatType)messageType messageExt:(NSDictionary *)messageExt { EMVideoMessageBody *body = [[EMVideoMessageBody alloc] initWithLocalPath:[url path] displayName:@"video.mp4"]; NSString *from = [[EMClient sharedClient] currentUsername]; EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:body ext:messageExt]; message.chatType = messageType; return message; } @end