123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- #define WFCU_SUPPORT_VOIP 1
- #import "AppDelegate.h"
- #import <WFChatClient/WFCChatClient.h>
- #if WFCU_SUPPORT_VOIP
- #import <WFAVEngineKit/WFAVEngineKit.h>
- #endif
- #import "WFCLoginViewController.h"
- #import "WFCConfig.h"
- #import "WFCBaseTabBarController.h"
- #import <WFChatUIKit/WFChatUIKit.h>
- #import <UserNotifications/UserNotifications.h>
- #import "CreateBarCodeViewController.h"
- #import "PCLoginConfirmViewController.h"
- #import "QQLBXScanViewController.h"
- #import "StyleDIY.h"
- #import "GroupInfoViewController.h"
- #import <Bugly/Bugly.h>
- #import "AppService.h"
- #import "UIColor+YH.h"
- @interface AppDelegate () <ConnectionStatusDelegate, ReceiveMessageDelegate,
- #if WFCU_SUPPORT_VOIP
- WFAVEngineDelegate,
- #endif
- UNUserNotificationCenterDelegate, QrCodeDelegate>
- @property(nonatomic, strong) AVAudioPlayer *audioPlayer;
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
- [Bugly startWithAppId:@"b21375e023"];
-
- [WFCCNetworkService startLog];
- [WFCCNetworkService sharedInstance].connectionStatusDelegate = self;
- [WFCCNetworkService sharedInstance].receiveMessageDelegate = self;
- [[WFCCNetworkService sharedInstance] setServerAddress:IM_SERVER_HOST];
-
- #if WFCU_SUPPORT_VOIP
- [[WFAVEngineKit sharedEngineKit] addIceServer:ICE_ADDRESS userName:ICE_USERNAME password:ICE_PASSWORD];
- [[WFAVEngineKit sharedEngineKit] setVideoProfile:kWFAVVideoProfile360P swapWidthHeight:YES];
- [WFAVEngineKit sharedEngineKit].delegate = self;
- #endif
-
- [WFCUConfigManager globalManager].appServiceProvider = [AppService sharedAppService];
-
- NSString *savedToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"savedToken"];
- NSString *savedUserId = [[NSUserDefaults standardUserDefaults] stringForKey:@"savedUserId"];
-
- self.window.rootViewController = [WFCBaseTabBarController new];
- self.window.backgroundColor = [UIColor whiteColor];
-
- [self setupNavBar];
-
- setQrCodeDelegate(self);
-
-
- if (@available(iOS 10.0, *)) {
-
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
- center.delegate = self;
- [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
- completionHandler:^(BOOL granted, NSError * _Nullable error) {
- if (!error) {
- NSLog(@"succeeded!");
- dispatch_async(dispatch_get_main_queue(), ^{
- [application registerForRemoteNotifications];
- });
- }
- }];
- } else {
- UIUserNotificationSettings *settings = [UIUserNotificationSettings
- settingsForTypes:(UIUserNotificationTypeBadge |
- UIUserNotificationTypeSound |
- UIUserNotificationTypeAlert)
- categories:nil];
- [application registerUserNotificationSettings:settings];
- }
-
-
-
- if (savedToken.length > 0 && savedUserId.length > 0) {
-
- [[WFCCNetworkService sharedInstance] connect:savedUserId token:savedToken];
- } else {
- UIViewController *loginVC = [[WFCLoginViewController alloc] init];
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
- self.window.rootViewController = nav;
- }
-
- return YES;
- }
- - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:
- (UIUserNotificationSettings *)notificationSettings {
-
- [application registerForRemoteNotifications];
- }
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- if ([deviceToken isKindOfClass:[NSData class]]) {
- const unsigned *tokenBytes = [deviceToken bytes];
- NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
- ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
- ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
- ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
- [[WFCCNetworkService sharedInstance] setDeviceToken:hexToken];
- } else {
- NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"
- withString:@""]
- stringByReplacingOccurrencesOfString:@">"
- withString:@""]
- stringByReplacingOccurrencesOfString:@" "
- withString:@""];
-
- [[WFCCNetworkService sharedInstance] setDeviceToken:token];
- }
- }
- - (void)applicationWillResignActive:(UIApplication *)application {
-
-
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
-
-
- WFCCUnreadCount *unreadCount = [[WFCCIMService sharedWFCIMService] getUnreadCount:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]];
- int unreadFriendRequest = [[WFCCIMService sharedWFCIMService] getUnreadFriendRequestStatus];
- [UIApplication sharedApplication].applicationIconBadgeNumber = unreadCount.unread + unreadFriendRequest;
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application {
-
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
-
- }
- - (void)applicationWillTerminate:(UIApplication *)application {
-
- [WFCCNetworkService startLog];
- }
- - (void)onReceiveMessage:(NSArray<WFCCMessage *> *)messages hasMore:(BOOL)hasMore {
- if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
- WFCCUnreadCount *unreadCount = [[WFCCIMService sharedWFCIMService] getUnreadCount:@[@(Single_Type), @(Group_Type), @(Channel_Type)] lines:@[@(0)]];
- int count = unreadCount.unread;
- [UIApplication sharedApplication].applicationIconBadgeNumber = count;
-
- for (WFCCMessage *msg in messages) {
-
- if (([[NSDate date] timeIntervalSince1970] - (msg.serverTime - [WFCCNetworkService sharedInstance].serverDeltaTime)/1000) > 3) {
- continue;
- }
-
- if (msg.direction == MessageDirection_Send) {
- continue;
- }
-
- int flag = (int)[msg.content.class performSelector:@selector(getContentFlags)];
- WFCCConversationInfo *info = [[WFCCIMService sharedWFCIMService] getConversationInfo:msg.conversation];
- if((flag & 0x03) && !info.isSilent && ![msg.content isKindOfClass:[WFCCCallStartMessageContent class]]) {
- UILocalNotification *localNote = [[UILocalNotification alloc] init];
-
- localNote.alertBody = [msg digest];
- if (msg.conversation.type == Single_Type) {
- WFCCUserInfo *sender = [[WFCCIMService sharedWFCIMService] getUserInfo:msg.conversation.target refresh:NO];
- if (sender.displayName) {
- if (@available(iOS 8.2, *)) {
- localNote.alertTitle = sender.displayName;
- } else {
-
- }
- }
- } else if(msg.conversation.type == Group_Type) {
- WFCCGroupInfo *group = [[WFCCIMService sharedWFCIMService] getGroupInfo:msg.conversation.target refresh:NO];
- WFCCUserInfo *sender = [[WFCCIMService sharedWFCIMService] getUserInfo:msg.fromUser refresh:NO];
- if (sender.displayName && group.name) {
- if (@available(iOS 8.2, *)) {
- localNote.alertTitle = [NSString stringWithFormat:@"%@@%@:", sender.displayName, group.name];
- } else {
-
- }
- }else if (sender.displayName) {
- if (@available(iOS 8.2, *)) {
- localNote.alertTitle = sender.displayName;
- } else {
-
- }
- }
- if (msg.status == Message_Status_Mentioned || msg.status == Message_Status_AllMentioned) {
- if (sender.displayName) {
- localNote.alertBody = [NSString stringWithFormat:@"%@在群里@了你", sender.displayName];
- } else {
- localNote.alertBody = @"有人在群里@了你";
- }
-
- }
- }
-
- localNote.applicationIconBadgeNumber = count;
- localNote.userInfo = @{@"conversationType" : @(msg.conversation.type), @"conversationTarget" : msg.conversation.target, @"conversationLine" : @(msg.conversation.line) };
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
- });
- }
- }
-
- }
- }
- - (void)onConnectionStatusChanged:(ConnectionStatus)status {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (status == kConnectionStatusRejected || status == kConnectionStatusTokenIncorrect || status == kConnectionStatusSecretKeyMismatch) {
- [[WFCCNetworkService sharedInstance] disconnect:YES clearSession:YES];
- } else if (status == kConnectionStatusLogout) {
- UIViewController *loginVC = [[WFCLoginViewController alloc] init];
- self.window.rootViewController = loginVC;
- }
- });
- }
- - (void)setupNavBar {
- [[WFCUConfigManager globalManager] setupNavBar];
- }
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
- return [self handleUrl:[url absoluteString] withNav:application.delegate.window.rootViewController.navigationController];
- }
- - (BOOL)handleUrl:(NSString *)str withNav:(UINavigationController *)navigator {
- NSLog(@"str scanned %@", str);
- if ([str rangeOfString:@"wildfirechat://user" options:NSCaseInsensitiveSearch].location == 0) {
- NSString *userId = [str lastPathComponent];
- WFCUProfileTableViewController *vc2 = [[WFCUProfileTableViewController alloc] init];
- vc2.userId = userId;
- vc2.hidesBottomBarWhenPushed = YES;
-
- [navigator pushViewController:vc2 animated:YES];
- return YES;
- } else if ([str rangeOfString:@"wildfirechat://group" options:NSCaseInsensitiveSearch].location == 0) {
- NSString *groupId = [str lastPathComponent];
- GroupInfoViewController *vc2 = [[GroupInfoViewController alloc] init];
- vc2.groupId = groupId;
- vc2.hidesBottomBarWhenPushed = YES;
- [navigator pushViewController:vc2 animated:YES];
- return YES;
- } else if ([str rangeOfString:@"wildfirechat://pcsession" options:NSCaseInsensitiveSearch].location == 0) {
- NSString *sessionId = [str lastPathComponent];
- PCLoginConfirmViewController *vc2 = [[PCLoginConfirmViewController alloc] init];
- vc2.sessionId = sessionId;
- vc2.hidesBottomBarWhenPushed = YES;
- [navigator pushViewController:vc2 animated:YES];
- return YES;
- }
- return NO;
- }
- #if WFCU_SUPPORT_VOIP
- #pragma mark - WFAVEngineDelegate
- - (void)didReceiveCall:(WFAVCallSession *)session {
- UIViewController *videoVC;
- if (session.conversation.type == Group_Type && [WFAVEngineKit sharedEngineKit].supportMultiCall) {
- videoVC = [[WFCUMultiVideoViewController alloc] initWithSession:session];
- } else {
- videoVC = [[WFCUVideoViewController alloc] initWithSession:session];
- }
-
- [[WFAVEngineKit sharedEngineKit] presentViewController:videoVC];
- if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
- UILocalNotification *localNote = [[UILocalNotification alloc] init];
-
- localNote.alertBody = @"来电话了";
-
- WFCCUserInfo *sender = [[WFCCIMService sharedWFCIMService] getUserInfo:session.participantIds[0] refresh:NO];
- if (sender.displayName) {
- if (@available(iOS 8.2, *)) {
- localNote.alertTitle = sender.displayName;
- } else {
-
-
- }
- }
-
- localNote.soundName = @"ring.caf";
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
- });
- }
- }
- - (void)shouldStartRing:(BOOL)isIncoming {
-
- if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
- AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, systemAudioCallback, NULL);
- AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
- } else {
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
-
- [audioSession setCategory:AVAudioSessionCategorySoloAmbient error:nil];
- [audioSession setActive:YES error:nil];
-
- if (self.audioPlayer) {
- [self shouldStopRing];
- }
-
- NSURL *url = [[NSBundle mainBundle] URLForResource:@"ring" withExtension:@"mp3"];
- NSError *error = nil;
- self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
- if (!error) {
- self.audioPlayer.numberOfLoops = -1;
- self.audioPlayer.volume = 1.0;
- [self.audioPlayer prepareToPlay];
- [self.audioPlayer play];
- }
- }
- }
- void systemAudioCallback (SystemSoundID soundID, void* clientData) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
- if ([WFAVEngineKit sharedEngineKit].currentSession.state == kWFAVEngineStateIncomming) {
- AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
- }
- }
- });
- }
- - (void)shouldStopRing {
- if (self.audioPlayer) {
- [self.audioPlayer stop];
- self.audioPlayer = nil;
- [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
- }
- }
- #endif
- #pragma mark - UNUserNotificationCenterDelegate
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)){
- NSLog(@"----------willPresentNotification");
- }
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){
- NSLog(@"============didReceiveNotificationResponse");
- NSString *categoryID = response.notification.request.content.categoryIdentifier;
- if ([categoryID isEqualToString:@"categoryIdentifier"]) {
- if ([response.actionIdentifier isEqualToString:@"enterApp"]) {
- if (@available(iOS 10.0, *)) {
-
- } else {
-
- }
- }else{
- NSLog(@"No======");
- }
- }
- completionHandler();
- }
- #pragma mark - QrCodeDelegate
- - (void)showQrCodeViewController:(UINavigationController *)navigator type:(int)type target:(NSString *)target {
- CreateBarCodeViewController *vc = [CreateBarCodeViewController new];
- vc.qrType = type;
- vc.target = target;
- [navigator pushViewController:vc animated:YES];
- }
- - (void)scanQrCode:(UINavigationController *)navigator {
- QQLBXScanViewController *vc = [QQLBXScanViewController new];
- vc.libraryType = SLT_Native;
- vc.scanCodeType = SCT_QRCode;
-
- vc.style = [StyleDIY qqStyle];
-
-
- vc.isVideoZoom = YES;
-
- vc.hidesBottomBarWhenPushed = YES;
- __weak typeof(self)ws = self;
- vc.scanResult = ^(NSString *str) {
- [ws handleUrl:str withNav:navigator];
- };
- [navigator pushViewController:vc animated:YES];
- }
- @end
|