1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-15 07:43:37 +00:00

clear cache and open file on iOS

This commit is contained in:
Kyle Spearrin
2017-07-13 10:51:45 -04:00
parent fe5cc1f8f3
commit 352c8ee867
10 changed files with 134 additions and 13 deletions

View File

@@ -1,6 +1,8 @@
using System;
using Bit.App.Abstractions;
using UIKit;
using Foundation;
using System.IO;
namespace Bit.iOS.Services
{
@@ -14,12 +16,63 @@ namespace Bit.iOS.Services
public bool OpenFile(byte[] fileData, string id, string fileName)
{
return true;
var filePath = Path.Combine(GetTempPath(), fileName);
File.WriteAllBytes(filePath, fileData);
var url = NSUrl.FromFilename(filePath);
var viewer = UIDocumentInteractionController.FromUrl(url);
var controller = GetVisibleViewController();
return viewer.PresentOpenInMenu(controller.View.Frame, controller.View, true);
}
public bool CanOpenFile(string fileName)
{
// Not sure of a way to check this ahead of time on iOS
return true;
}
public void ClearCache()
{
var url = new NSUrl(GetTempPath());
NSError error;
var tmpFiles = NSFileManager.DefaultManager.GetDirectoryContent(url, null,
NSDirectoryEnumerationOptions.SkipsHiddenFiles, out error);
if(error == null && tmpFiles.Length > 0)
{
foreach(var item in tmpFiles)
{
NSError itemError;
NSFileManager.DefaultManager.Remove(item, out itemError);
}
}
}
private UIViewController GetVisibleViewController(UIViewController controller = null)
{
controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;
if(controller.PresentedViewController == null)
{
return controller;
}
if(controller.PresentedViewController is UINavigationController)
{
return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
}
if(controller.PresentedViewController is UITabBarController)
{
return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
}
return GetVisibleViewController(controller.PresentedViewController);
}
// ref: //https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_the_file_system/
public string GetTempPath()
{
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var tmp = Path.Combine(documents, "..", "tmp");
return tmp;
}
}
}