Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Title option when opening files on iOS #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,12 @@ FlutterDownloader.remove(taskId: taskId, shouldDeleteContent:false);
#### Open and preview a downloaded file:

````dart
FlutterDownloader.open(taskId: taskId);
FlutterDownloader.open(taskId: taskId, title: "Title (Optional)");
````

- Note: in Android, you can only open a downloaded file if it is placed in the external storage and there's at least one application that can read that file type on your device.
**Note:**
- In Android, you can only open a downloaded file if it is placed in the external storage and there's at least one application that can read that file type on your device.
- The title option is only supported in iOS.

## Bugs/Requests
If you encounter any problems feel free to open an issue. If you feel the library is
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
}

Future<bool> _openDownloadedFile(_TaskInfo task) {
return FlutterDownloader.open(taskId: task.taskId);
return FlutterDownloader.open(taskId: task.taskId, title: task.name);
}

void _delete(_TaskInfo task) async {
Expand Down
10 changes: 8 additions & 2 deletions ios/Classes/FlutterDownloaderPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define KEY_OPEN_FILE_FROM_NOTIFICATION @"open_file_from_notification"
#define KEY_QUERY @"query"
#define KEY_TIME_CREATED @"time_created"
#define KEY_TITLE @"title"

#define NULL_VALUE @"<null>"

Expand Down Expand Up @@ -216,11 +217,15 @@ - (void)sendUpdateProgressForTaskId: (NSString*)taskId inStatus: (NSNumber*) sta
[_flutterChannel invokeMethod:@"updateProgress" arguments:info];
}

- (BOOL)openDocumentWithURL:(NSURL*)url {
- (BOOL)openDocumentWithURL:(NSURL*)url title: (NSString*) title {
NSLog(@"try to open file in url: %@", url);
BOOL result = NO;
UIDocumentInteractionController* tmpDocController = [UIDocumentInteractionController
interactionControllerWithURL:url];

if (title != (NSString*) [NSNull null] && ![NULL_VALUE isEqualToString: title]) {
tmpDocController.name = title;
}
if (tmpDocController)
{
NSLog(@"initialize UIDocumentInteractionController successfully");
Expand Down Expand Up @@ -547,13 +552,14 @@ - (void)retryMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {

- (void)openMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
NSString *taskId = call.arguments[KEY_TASK_ID];
NSString *title = call.arguments[KEY_TITLE];
NSDictionary* taskDict = [self loadTaskWithId:taskId];
if (taskDict != nil) {
NSNumber* status = taskDict[KEY_STATUS];
if ([status intValue] == STATUS_COMPLETE) {
NSURL *downloadedFileURL = [self fileUrlFromDict:taskDict];

BOOL success = [self openDocumentWithURL:downloadedFileURL];
BOOL success = [self openDocumentWithURL:downloadedFileURL title:title];
result([NSNumber numberWithBool:success]);
} else {
result([FlutterError errorWithCode:@"invalid_status"
Expand Down
4 changes: 2 additions & 2 deletions lib/flutter_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ class FlutterDownloader {
/// - The current device has at least an application that can read the file
/// type of the file
///
static Future<bool> open({@required String taskId}) async {
static Future<bool> open({@required String taskId, String title}) async {
try {
return await platform.invokeMethod('open', {'task_id': taskId});
return await platform.invokeMethod('open', {'task_id': taskId, 'title': title});
} on PlatformException catch (e) {
print(e.message);
return false;
Expand Down