1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-07 09:20:04 +01:00

fix attachments selection on ios

This commit is contained in:
Kyle Spearrin 2019-06-25 17:46:37 -04:00
parent d7130d9b67
commit 14f3f99218
2 changed files with 53 additions and 23 deletions

View File

@ -45,7 +45,10 @@ namespace Bit.App.Pages
protected override void OnDisappearing()
{
base.OnDisappearing();
_broadcasterService.Unsubscribe(nameof(AttachmentsPage));
if(Device.RuntimePlatform != Device.iOS)
{
_broadcasterService.Unsubscribe(nameof(AttachmentsPage));
}
}
private async void Save_Clicked(object sender, EventArgs e)

View File

@ -161,8 +161,15 @@ namespace Bit.iOS.Services
});
picker.DidPickDocumentPicker += (sender, e) =>
{
if(SystemMajorVersion() < 11)
{
e.DocumentPicker.DidPickDocument += DocumentPicker_DidPickDocument;
}
else
{
e.DocumentPicker.Delegate = new PickerDelegate(this);
}
controller.PresentViewController(e.DocumentPicker, true, null);
e.DocumentPicker.DidPickDocument += DocumentPicker_DidPickDocument;
};
var root = UIApplication.SharedApplication.KeyWindow.RootViewController;
if(picker.PopoverPresentationController != null && root != null)
@ -374,27 +381,7 @@ namespace Bit.iOS.Services
private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
{
e.Url.StartAccessingSecurityScopedResource();
var doc = new UIDocument(e.Url);
var fileName = doc.LocalizedName;
if(string.IsNullOrWhiteSpace(fileName))
{
var path = doc.FileUrl?.ToString();
if(path != null)
{
path = WebUtility.UrlDecode(path);
var split = path.LastIndexOf('/');
fileName = path.Substring(split + 1);
}
}
var fileCoordinator = new NSFileCoordinator();
fileCoordinator.CoordinateRead(e.Url, NSFileCoordinatorReadingOptions.WithoutChanges,
out NSError error, (url) =>
{
var data = NSData.FromUrl(url).ToArray();
SelectFileResult(data, fileName ?? "unknown_file_name");
});
e.Url.StopAccessingSecurityScopedResource();
PickedDocument(e.Url);
}
private void SelectFileResult(byte[] data, string fileName)
@ -444,5 +431,45 @@ namespace Bit.iOS.Services
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return Path.Combine(documents, "..", "tmp");
}
public void PickedDocument(NSUrl url)
{
url.StartAccessingSecurityScopedResource();
var doc = new UIDocument(url);
var fileName = doc.LocalizedName;
if(string.IsNullOrWhiteSpace(fileName))
{
var path = doc.FileUrl?.ToString();
if(path != null)
{
path = WebUtility.UrlDecode(path);
var split = path.LastIndexOf('/');
fileName = path.Substring(split + 1);
}
}
var fileCoordinator = new NSFileCoordinator();
fileCoordinator.CoordinateRead(url, NSFileCoordinatorReadingOptions.WithoutChanges,
out NSError error, (u) =>
{
var data = NSData.FromUrl(u).ToArray();
SelectFileResult(data, fileName ?? "unknown_file_name");
});
url.StopAccessingSecurityScopedResource();
}
public class PickerDelegate : UIDocumentPickerDelegate
{
private readonly DeviceActionService _deviceActionService;
public PickerDelegate(DeviceActionService deviceActionService)
{
_deviceActionService = deviceActionService;
}
public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url)
{
_deviceActionService.PickedDocument(url);
}
}
}
}