diff --git a/Classes/ChatConversationView.h b/Classes/ChatConversationView.h index 32fd99ecd..bca159e8d 100644 --- a/Classes/ChatConversationView.h +++ b/Classes/ChatConversationView.h @@ -85,8 +85,11 @@ - (IBAction)onEditionChangeClick:(id)sender; - (IBAction)onEncryptedDevicesClick:(id)sender; - (void)update; -- (void)openFile:(NSString *) filePath; +- (void)openFileWithURL:(NSURL *)url; - (void)clearMessageView; + - (void)autoDownload:(LinphoneChatMessage *)message view:(ChatConversationView *)view inChat:(BOOL)inChat; +- (NSURL *)getICloudFileUrl:(NSString *)name; +- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL; @end diff --git a/Classes/ChatConversationView.m b/Classes/ChatConversationView.m index 0467cf48c..e61c8b8a6 100644 --- a/Classes/ChatConversationView.m +++ b/Classes/ChatConversationView.m @@ -997,20 +997,62 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog * [PhoneMainView.instance popToView:view.compositeViewDescription]; } -- (void)openFile:(NSString *) filePath +- (void)openFileWithURL:(NSURL *)url { // Open the controller. - _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]]; + _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url]; _documentInteractionController.delegate = self; BOOL canOpen = [_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; //NO app can open the file if (canOpen == NO) { - [[[UIAlertView alloc] initWithTitle:@"Info" message:@"There is no app found to open it" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil] show]; + [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", nil) message:NSLocalizedString(@"There is no app to open it.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:nil, nil] show]; } } +- (NSURL *)getICloudFileUrl:(NSString *)name { + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSURL *icloudPath = [[fileManager URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"]; + + if (icloudPath) { + if (![fileManager fileExistsAtPath:icloudPath.path isDirectory:nil]) { + LOGI(@"Create directory"); + [fileManager createDirectoryAtURL:icloudPath withIntermediateDirectories:YES attributes:nil error:nil]; + } + + return [icloudPath URLByAppendingPathComponent:name]; + } + + return nil; +} + +- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL { + NSFileManager *fileManager = [NSFileManager defaultManager]; + if (![[fileManager URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"]) { + //notify : set configuration to use icloud + [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", nil) message:NSLocalizedString(@"ICloud Drive is unavailable.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:nil, nil] show]; + return; + } + + if ([fileManager isUbiquitousItemAtURL:fileURL]) { + // if it exists, replace the file + [data writeToURL:fileURL atomically:TRUE]; + } else { + // get the url of localfile + NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileURL.lastPathComponent]; + NSURL *localURL = nil; + if ([fileManager createFileAtPath:filePath contents:data attributes:nil]) { + localURL = [NSURL fileURLWithPath:filePath]; + } + + NSError *error; + if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:fileURL error:&error]) { + LOGE(@"ICloud file error : %@", error); + } + } +} + - (void)deleteImageWithAssetId:(NSString *)assetId { NSUInteger key = [_assetIdsArray indexOfObject:assetId]; [_imagesArray removeObjectAtIndex:key]; diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index 07dd7152f..9600fa1c4 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -271,18 +271,8 @@ - (IBAction)onFileClick:(id)sender { ChatConversationView *view = VIEW(ChatConversationView); - NSString *cachedFile = [LinphoneManager getMessageAppDataForKey:@"cachedfile" inMessage:self.message]; - if (cachedFile) { - NSFileManager *fileManager = [NSFileManager defaultManager]; - if ([fileManager fileExistsAtPath:cachedFile]) { - [view openFile:cachedFile]; - } else { - [self fileErrorBlock]; - } - } else { - [LinphoneManager setValueInMessageAppData:@"onFileClick" forKey:@"icloudFileOption" inMessage:self.message]; - [super getIcloudFiles]; - } + NSString *name = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message]; + [view openFileWithURL:[view getICloudFileUrl:name]]; } diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.h b/Classes/LinphoneUI/UIChatBubbleTextCell.h index 382398711..93d92fe83 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.h +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.h @@ -51,7 +51,6 @@ - (void)setEvent:(LinphoneEventLog *)event; - (void)setChatMessage:(LinphoneChatMessage *)message; -- (void)getIcloudFiles; - (void)onDelete; - (void)onResend; diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index 337d97a83..b99c63a9d 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -262,36 +262,6 @@ [PhoneMainView.instance presentViewController:errView animated:YES completion:nil]; } -- (void)getIcloudFiles { - _documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"] - inMode:UIDocumentPickerModeImport]; - _documentPicker.delegate = self; - - _documentPicker.modalPresentationStyle = UIModalPresentationOverCurrentContext ; - ChatConversationView *view = VIEW(ChatConversationView); - [view presentViewController:_documentPicker animated:YES completion:nil]; - } - - - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url { - NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; - [fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL * _Nonnull newURL) { - - NSString *fileName = [newURL lastPathComponent]; - NSData *data = [NSData dataWithContentsOfURL:newURL]; - NSString *option = [LinphoneManager getMessageAppDataForKey:@"icloudFileOption" inMessage:self.message]; - - if ([option isEqualToString:@"onResend"]) - [_chatRoomDelegate startFileUpload:data withName:fileName]; - else if ([option isEqualToString:@"onFileClick"]) { - ChatConversationView *view = VIEW(ChatConversationView); - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName]; - [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil]; - [view openFile:filePath]; - } - }]; -} - - #pragma mark - Action Functions - (void)onDelete { @@ -379,11 +349,9 @@ }]; } else if (localFile) { - [LinphoneManager setValueInMessageAppData:@"onResend" forKey:@"icloudFileOption" inMessage:_message]; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), - ^(void) { - [self getIcloudFiles]; - }); + ChatConversationView *view = VIEW(ChatConversationView); + NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]]; + [_chatRoomDelegate startFileUpload:data withName:localFile]; } } else { [self onDelete]; diff --git a/Classes/Utils/FileTransferDelegate.m b/Classes/Utils/FileTransferDelegate.m index 68fdfa8f1..07b6cbae6 100644 --- a/Classes/Utils/FileTransferDelegate.m +++ b/Classes/Utils/FileTransferDelegate.m @@ -173,20 +173,14 @@ static void linphone_iphone_file_transfer_recv(LinphoneChatMessage *message, con [[LinphoneManager.instance fileTransferDelegates] removeObject:thiz]; NSString *key = @"localfile" ; NSString *name =[NSString stringWithUTF8String:linphone_content_get_name(content)]; - + ChatConversationView *view = VIEW(ChatConversationView); [LinphoneManager setValueInMessageAppData:@"saving..." forKey:key inMessage:message]; //write file to path dispatch_async(dispatch_get_main_queue(), ^{ - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name]; - //NSString *filePath = [LinphoneManager documentFile:name]; - [[NSFileManager defaultManager] createFileAtPath:filePath - contents:thiz.data - attributes:nil]; - + [view writeFileInICloud:thiz.data fileURL:[view getICloudFileUrl:name]]; [LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message]; - [LinphoneManager setValueInMessageAppData:filePath forKey:@"cachedfile" inMessage:message]; - + [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneFileTransferRecvUpdate object:thiz @@ -300,6 +294,10 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m } - (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withName:(NSString *)name { + // we will write local files into ours folder of icloud + ChatConversationView *view = VIEW(ChatConversationView); + [view writeFileInICloud:data fileURL:[view getICloudFileUrl:name]]; + [self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil]; } diff --git a/linphone-Info.plist b/linphone-Info.plist index 1a37b864b..7ef79873b 100644 --- a/linphone-Info.plist +++ b/linphone-Info.plist @@ -96,6 +96,18 @@ Add tranfered files to your library NSPhotoLibraryUsageDescription Share photos with your friends and customize avatars + NSUbiquitousContainers + + iCloud.org.linphone.obiane + + NSUbiquitousContainerIsDocumentScopePublic + + NSUbiquitousContainerName + Obiane + NSUbiquitousContainerSupportedFolderLevels + Any + + NSVoIPUsageDescription Make audio/video calls UIApplicationExitsOnSuspend diff --git a/linphone.entitlements b/linphone.entitlements index 371282fab..8a823707e 100644 --- a/linphone.entitlements +++ b/linphone.entitlements @@ -4,6 +4,20 @@ aps-environment development + com.apple.developer.icloud-container-identifiers + + iCloud.$(CFBundleIdentifier) + + com.apple.developer.icloud-services + + CloudDocuments + + com.apple.developer.ubiquity-container-identifiers + + iCloud.$(CFBundleIdentifier) + + com.apple.developer.ubiquity-kvstore-identifier + $(TeamIdentifierPrefix)$(CFBundleIdentifier) com.apple.security.application-groups group.belledonne-communications.linphone.widget diff --git a/linphone.xcodeproj/project.pbxproj b/linphone.xcodeproj/project.pbxproj index f51063f1c..3aa7cc99a 100644 --- a/linphone.xcodeproj/project.pbxproj +++ b/linphone.xcodeproj/project.pbxproj @@ -3652,7 +3652,7 @@ enabled = 1; }; com.apple.iCloud = { - enabled = 0; + enabled = 1; }; }; };