From 44fdda09c660ce68683cc4f9a3f7de85db21dfa4 Mon Sep 17 00:00:00 2001 From: Anuken Date: Tue, 19 Nov 2019 03:23:40 -0500 Subject: [PATCH] it's 3 am and ios file imports are finally fixed --- ios/src/io/anuke/mindustry/IOSLauncher.java | 131 +++----------------- 1 file changed, 16 insertions(+), 115 deletions(-) diff --git a/ios/src/io/anuke/mindustry/IOSLauncher.java b/ios/src/io/anuke/mindustry/IOSLauncher.java index 7f2998a317..1a46baf921 100644 --- a/ios/src/io/anuke/mindustry/IOSLauncher.java +++ b/ios/src/io/anuke/mindustry/IOSLauncher.java @@ -58,129 +58,31 @@ public class IOSLauncher extends IOSApplication.Delegate{ public void didPickDocumentsAtURLs(UIDocumentBrowserViewController controller, NSArray documentURLs){ if(documentURLs.size() < 1) return; - cont.dismissViewController(true, () -> {}); + NSURL url = documentURLs.first(); NSFileCoordinator coord = new NSFileCoordinator(null); - + url.startAccessingSecurityScopedResource(); try{ - coord.coordinateReadingItem(documentURLs.get(0), NSFileCoordinatorReadingOptions.None, url -> { - if(url.startAccessingSecurityScopedResource()){ + coord.coordinateReadingItem(url, NSFileCoordinatorReadingOptions.ForUploading, result -> { + + FileHandle src = Core.files.absolute(result.getAbsoluteURL().getPath()); + FileHandle dst = Core.files.absolute(getDocumentsDirectory()).child(src.name()); + src.copyTo(dst); + + Core.app.post(() -> { try{ - controller.importDocument(url, new NSURL(getDocumentsDirectory() + "/document"), UIDocumentBrowserImportMode.Copy, (result, error) -> { - if(error != null){ - ui.showErrorMessage("Import error.\n" + error.getLocalizedFailureReason() + "\n" + error.getLocalizedDescription()); - }else{ - try{ - cons.get(Core.files.absolute(url.getPath())); - }catch(Throwable t){ - ui.showException(t); - } - } - }); + cons.get(dst); }catch(Throwable t){ ui.showException(t); } - - /* - - try{ - int[] tread = {0}; - - cons.get(new FileHandle(url.getPath()){ - @Override - public InputStream read(){ - NSInputStream stream = new NSInputStream(url); - - return new InputStream(){ - byte[] tmp = {0}; - - @Override - public int read() throws IOException{ - read(tmp); - return tmp[0]; - } - - @Override - public int read(byte[] bytes, int offset, int length){ - int read = (int)stream.read(bytes, offset, length); - tread[0] += read; - return read; - } - }; - } - }); - Core.app.post(() -> Core.app.post(() -> Core.app.post(() -> ui.showInfo("Read " + tread[0])))); - //cons.get(new FileHandle(url.getAbsoluteString())); - }catch(Throwable t){ - ui.showException(t); - - try{ - cons.get(new FileHandle(url.getPath())); - }catch(Throwable t2){ - ui.showException(t2); - - try{ - cons.get(new FileHandle(url.getAbsoluteURL().getPath())); - }catch(Throwable t3){ - ui.showException(t3); - } - } - }*/ - }else{ - ui.showErrorMessage("Failed to access file."); - } - - /* - try{ - int[] tread = {0}; - - cons.get(new FileHandle(url.getPath()){ - @Override - public InputStream read(){ - NSInputStream stream = new NSInputStream(url); - - return new InputStream(){ - byte[] tmp = {0}; - - @Override - public int read() throws IOException{ - read(tmp); - return tmp[0]; - } - - @Override - public int read(byte[] bytes, int offset, int length){ - int read = (int)stream.read(bytes, offset, length); - tread[0] += read; - return read; - } - }; - } - }); - Core.app.post(() -> Core.app.post(() -> Core.app.post(() -> ui.showInfo("Read " + tread[0])))); - }catch(Throwable t){ - ui.showException(t); - }*/ + }); }); - }catch(Throwable t){ - ui.showException(t); + }catch(Throwable e){ + ui.showException(e); } - /* - try{ - controller.importDocument(documentURLs.get(0), new NSURL(getDocumentsDirectory() + "/document"), UIDocumentBrowserImportMode.Copy, (url, error) -> { - if(error != null){ - ui.showErrorMessage("Import error.\n" + error.getLocalizedFailureReason() + "\n" + error.getLocalizedDescription()); - }else{ - try{ - cons.get(Core.files.absolute(url.getPath())); - }catch(Throwable t){ - ui.showException(t); - } - } - }); - }catch(Throwable t){ - ui.showException(t); - }*/ + url.stopAccessingSecurityScopedResource(); + + cont.dismissViewController(true, () -> {}); } @Override @@ -190,7 +92,6 @@ public class IOSLauncher extends IOSApplication.Delegate{ @Override public void didImportDocument(UIDocumentBrowserViewController controller, NSURL sourceURL, NSURL destinationURL){ - //cons.get(Core.files.absolute(destinationURL.getAbsoluteString())); } @Override