Swift 4 app crashes when open file from icloud











up vote
0
down vote

favorite












I have a problem that my app crashes when it is opening a file from iCloud. If I open this file from my app with a Document Picker, everything is fine. But if I try to open from outside my app, for example from iCloud or safari download it crashes. If I open it from local storage "my iphone" it is also working. It is interesting because it was good one week ago :)



So in AppDelegate, I've implemented the following method:



func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {}


According to the logs the crash occurs because the file does not exist.



file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn


This is the result if I print the URL from the parameter. I think this means that the file is there.



But if i do this: print(fm.fileExists(atPath: url.path)) then this is false.



So it is obvious that after let dataFromFile = fm.contents(atPath: url.path)
this is nil.



I have no idea what could be the problem here. So the real question here is why this is nil?










share|improve this question




















  • 1




    Try calling startAccessingSecurityScopedResource() on your URL first. (See developer.apple.com/documentation/foundation/nsurl/…)
    – Sven
    Nov 21 at 18:02










  • This works if the file has already downloaded to the phone. So if there is a small cloud icon in the upright corner of the file this is not working, anyway I'm one step ahead than before, so thanks!
    – Dániel Grimm
    Nov 21 at 19:08










  • Sorry, I don’t know all the details either. Try to look at the FileManager methods startDownloadingUbiquitousItem(at:) and isUbiquitousItem(at:). Also try using the URLs directly instead of getting the path.
    – Sven
    Nov 21 at 19:54










  • It is interesting, because if I would like to open a file which is not downloaded yet, just the placeholder icon is there, if I try this from a Document Picker, then it downloads first and then opens. But from outside the app, if I tap on it, it just opens immediately (of course, the file does not exist at that point, just its icon). I 've found already this startDownloadingUbiquitousItem before, but I don't understand why it hasn't got any callback? It just starts the download, but I don't know when it is finished. Thanks to your help, I really appreciate that.
    – Dániel Grimm
    Nov 21 at 20:19















up vote
0
down vote

favorite












I have a problem that my app crashes when it is opening a file from iCloud. If I open this file from my app with a Document Picker, everything is fine. But if I try to open from outside my app, for example from iCloud or safari download it crashes. If I open it from local storage "my iphone" it is also working. It is interesting because it was good one week ago :)



So in AppDelegate, I've implemented the following method:



func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {}


According to the logs the crash occurs because the file does not exist.



file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn


This is the result if I print the URL from the parameter. I think this means that the file is there.



But if i do this: print(fm.fileExists(atPath: url.path)) then this is false.



So it is obvious that after let dataFromFile = fm.contents(atPath: url.path)
this is nil.



I have no idea what could be the problem here. So the real question here is why this is nil?










share|improve this question




















  • 1




    Try calling startAccessingSecurityScopedResource() on your URL first. (See developer.apple.com/documentation/foundation/nsurl/…)
    – Sven
    Nov 21 at 18:02










  • This works if the file has already downloaded to the phone. So if there is a small cloud icon in the upright corner of the file this is not working, anyway I'm one step ahead than before, so thanks!
    – Dániel Grimm
    Nov 21 at 19:08










  • Sorry, I don’t know all the details either. Try to look at the FileManager methods startDownloadingUbiquitousItem(at:) and isUbiquitousItem(at:). Also try using the URLs directly instead of getting the path.
    – Sven
    Nov 21 at 19:54










  • It is interesting, because if I would like to open a file which is not downloaded yet, just the placeholder icon is there, if I try this from a Document Picker, then it downloads first and then opens. But from outside the app, if I tap on it, it just opens immediately (of course, the file does not exist at that point, just its icon). I 've found already this startDownloadingUbiquitousItem before, but I don't understand why it hasn't got any callback? It just starts the download, but I don't know when it is finished. Thanks to your help, I really appreciate that.
    – Dániel Grimm
    Nov 21 at 20:19













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a problem that my app crashes when it is opening a file from iCloud. If I open this file from my app with a Document Picker, everything is fine. But if I try to open from outside my app, for example from iCloud or safari download it crashes. If I open it from local storage "my iphone" it is also working. It is interesting because it was good one week ago :)



So in AppDelegate, I've implemented the following method:



func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {}


According to the logs the crash occurs because the file does not exist.



file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn


This is the result if I print the URL from the parameter. I think this means that the file is there.



But if i do this: print(fm.fileExists(atPath: url.path)) then this is false.



So it is obvious that after let dataFromFile = fm.contents(atPath: url.path)
this is nil.



I have no idea what could be the problem here. So the real question here is why this is nil?










share|improve this question















I have a problem that my app crashes when it is opening a file from iCloud. If I open this file from my app with a Document Picker, everything is fine. But if I try to open from outside my app, for example from iCloud or safari download it crashes. If I open it from local storage "my iphone" it is also working. It is interesting because it was good one week ago :)



So in AppDelegate, I've implemented the following method:



func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {}


According to the logs the crash occurs because the file does not exist.



file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn


This is the result if I print the URL from the parameter. I think this means that the file is there.



But if i do this: print(fm.fileExists(atPath: url.path)) then this is false.



So it is obvious that after let dataFromFile = fm.contents(atPath: url.path)
this is nil.



I have no idea what could be the problem here. So the real question here is why this is nil?







ios swift file icloud






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 18:26









Surjeet

7,84622542




7,84622542










asked Nov 21 at 17:51









Dániel Grimm

189




189








  • 1




    Try calling startAccessingSecurityScopedResource() on your URL first. (See developer.apple.com/documentation/foundation/nsurl/…)
    – Sven
    Nov 21 at 18:02










  • This works if the file has already downloaded to the phone. So if there is a small cloud icon in the upright corner of the file this is not working, anyway I'm one step ahead than before, so thanks!
    – Dániel Grimm
    Nov 21 at 19:08










  • Sorry, I don’t know all the details either. Try to look at the FileManager methods startDownloadingUbiquitousItem(at:) and isUbiquitousItem(at:). Also try using the URLs directly instead of getting the path.
    – Sven
    Nov 21 at 19:54










  • It is interesting, because if I would like to open a file which is not downloaded yet, just the placeholder icon is there, if I try this from a Document Picker, then it downloads first and then opens. But from outside the app, if I tap on it, it just opens immediately (of course, the file does not exist at that point, just its icon). I 've found already this startDownloadingUbiquitousItem before, but I don't understand why it hasn't got any callback? It just starts the download, but I don't know when it is finished. Thanks to your help, I really appreciate that.
    – Dániel Grimm
    Nov 21 at 20:19














  • 1




    Try calling startAccessingSecurityScopedResource() on your URL first. (See developer.apple.com/documentation/foundation/nsurl/…)
    – Sven
    Nov 21 at 18:02










  • This works if the file has already downloaded to the phone. So if there is a small cloud icon in the upright corner of the file this is not working, anyway I'm one step ahead than before, so thanks!
    – Dániel Grimm
    Nov 21 at 19:08










  • Sorry, I don’t know all the details either. Try to look at the FileManager methods startDownloadingUbiquitousItem(at:) and isUbiquitousItem(at:). Also try using the URLs directly instead of getting the path.
    – Sven
    Nov 21 at 19:54










  • It is interesting, because if I would like to open a file which is not downloaded yet, just the placeholder icon is there, if I try this from a Document Picker, then it downloads first and then opens. But from outside the app, if I tap on it, it just opens immediately (of course, the file does not exist at that point, just its icon). I 've found already this startDownloadingUbiquitousItem before, but I don't understand why it hasn't got any callback? It just starts the download, but I don't know when it is finished. Thanks to your help, I really appreciate that.
    – Dániel Grimm
    Nov 21 at 20:19








1




1




Try calling startAccessingSecurityScopedResource() on your URL first. (See developer.apple.com/documentation/foundation/nsurl/…)
– Sven
Nov 21 at 18:02




Try calling startAccessingSecurityScopedResource() on your URL first. (See developer.apple.com/documentation/foundation/nsurl/…)
– Sven
Nov 21 at 18:02












This works if the file has already downloaded to the phone. So if there is a small cloud icon in the upright corner of the file this is not working, anyway I'm one step ahead than before, so thanks!
– Dániel Grimm
Nov 21 at 19:08




This works if the file has already downloaded to the phone. So if there is a small cloud icon in the upright corner of the file this is not working, anyway I'm one step ahead than before, so thanks!
– Dániel Grimm
Nov 21 at 19:08












Sorry, I don’t know all the details either. Try to look at the FileManager methods startDownloadingUbiquitousItem(at:) and isUbiquitousItem(at:). Also try using the URLs directly instead of getting the path.
– Sven
Nov 21 at 19:54




Sorry, I don’t know all the details either. Try to look at the FileManager methods startDownloadingUbiquitousItem(at:) and isUbiquitousItem(at:). Also try using the URLs directly instead of getting the path.
– Sven
Nov 21 at 19:54












It is interesting, because if I would like to open a file which is not downloaded yet, just the placeholder icon is there, if I try this from a Document Picker, then it downloads first and then opens. But from outside the app, if I tap on it, it just opens immediately (of course, the file does not exist at that point, just its icon). I 've found already this startDownloadingUbiquitousItem before, but I don't understand why it hasn't got any callback? It just starts the download, but I don't know when it is finished. Thanks to your help, I really appreciate that.
– Dániel Grimm
Nov 21 at 20:19




It is interesting, because if I would like to open a file which is not downloaded yet, just the placeholder icon is there, if I try this from a Document Picker, then it downloads first and then opens. But from outside the app, if I tap on it, it just opens immediately (of course, the file does not exist at that point, just its icon). I 've found already this startDownloadingUbiquitousItem before, but I don't understand why it hasn't got any callback? It just starts the download, but I don't know when it is finished. Thanks to your help, I really appreciate that.
– Dániel Grimm
Nov 21 at 20:19












1 Answer
1






active

oldest

votes

















up vote
0
down vote













It appears that the error can be many things, all not related to the class you are applying the code (AppDelegate) nor the methods you are calling.



My guess is that the URL you are calling is not correctly built (not pointing to the correct object you are trying to point to). For many reasons.



See if one of this reasons fix your issue:



(1) The end of the URL you are calling had the suffix "pgn". If you are looking to load a picture, maybe the suffix is wrong. In that case it could have been some known and supported format like "png", "jpeg" or "jpg".



(2) The "%20"symbol at the middle of your code also lifts a flag. Does not seem to be a correct URL object of swift. Maybe the URL you are using is not represented in the correct way.



(3) com~apple~CloudDocs also lifts a flag, since it would unlikely have a "~" symbol in a URL passed. This also strongly suggests that maybe the URL you are using is not represented in the correct way.



I think your URL is not pointing to where you are trying to point to, resulting in the "does exist" method return false and the loading resulting in nil.



If all of this does not fix your issue, post more details of the code. Specially what method you are calling to build/create this URL object you are using, that points to: file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn






share|improve this answer























  • This URL is from the delegate method, that gets called when you open a file that has a registered file type with the application.
    – Dániel Grimm
    Nov 21 at 19:10










  • The URL is not the problem. %20 is the correct way to encode spaces in an URL. And the com~apple~CloudDocs part is typical for URLs of files stored in iCloud. Also nobody said that it is supposed to be an image file - pgn is a common file format to store chess games.
    – Sven
    Nov 21 at 19:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417918%2fswift-4-app-crashes-when-open-file-from-icloud%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













It appears that the error can be many things, all not related to the class you are applying the code (AppDelegate) nor the methods you are calling.



My guess is that the URL you are calling is not correctly built (not pointing to the correct object you are trying to point to). For many reasons.



See if one of this reasons fix your issue:



(1) The end of the URL you are calling had the suffix "pgn". If you are looking to load a picture, maybe the suffix is wrong. In that case it could have been some known and supported format like "png", "jpeg" or "jpg".



(2) The "%20"symbol at the middle of your code also lifts a flag. Does not seem to be a correct URL object of swift. Maybe the URL you are using is not represented in the correct way.



(3) com~apple~CloudDocs also lifts a flag, since it would unlikely have a "~" symbol in a URL passed. This also strongly suggests that maybe the URL you are using is not represented in the correct way.



I think your URL is not pointing to where you are trying to point to, resulting in the "does exist" method return false and the loading resulting in nil.



If all of this does not fix your issue, post more details of the code. Specially what method you are calling to build/create this URL object you are using, that points to: file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn






share|improve this answer























  • This URL is from the delegate method, that gets called when you open a file that has a registered file type with the application.
    – Dániel Grimm
    Nov 21 at 19:10










  • The URL is not the problem. %20 is the correct way to encode spaces in an URL. And the com~apple~CloudDocs part is typical for URLs of files stored in iCloud. Also nobody said that it is supposed to be an image file - pgn is a common file format to store chess games.
    – Sven
    Nov 21 at 19:50















up vote
0
down vote













It appears that the error can be many things, all not related to the class you are applying the code (AppDelegate) nor the methods you are calling.



My guess is that the URL you are calling is not correctly built (not pointing to the correct object you are trying to point to). For many reasons.



See if one of this reasons fix your issue:



(1) The end of the URL you are calling had the suffix "pgn". If you are looking to load a picture, maybe the suffix is wrong. In that case it could have been some known and supported format like "png", "jpeg" or "jpg".



(2) The "%20"symbol at the middle of your code also lifts a flag. Does not seem to be a correct URL object of swift. Maybe the URL you are using is not represented in the correct way.



(3) com~apple~CloudDocs also lifts a flag, since it would unlikely have a "~" symbol in a URL passed. This also strongly suggests that maybe the URL you are using is not represented in the correct way.



I think your URL is not pointing to where you are trying to point to, resulting in the "does exist" method return false and the loading resulting in nil.



If all of this does not fix your issue, post more details of the code. Specially what method you are calling to build/create this URL object you are using, that points to: file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn






share|improve this answer























  • This URL is from the delegate method, that gets called when you open a file that has a registered file type with the application.
    – Dániel Grimm
    Nov 21 at 19:10










  • The URL is not the problem. %20 is the correct way to encode spaces in an URL. And the com~apple~CloudDocs part is typical for URLs of files stored in iCloud. Also nobody said that it is supposed to be an image file - pgn is a common file format to store chess games.
    – Sven
    Nov 21 at 19:50













up vote
0
down vote










up vote
0
down vote









It appears that the error can be many things, all not related to the class you are applying the code (AppDelegate) nor the methods you are calling.



My guess is that the URL you are calling is not correctly built (not pointing to the correct object you are trying to point to). For many reasons.



See if one of this reasons fix your issue:



(1) The end of the URL you are calling had the suffix "pgn". If you are looking to load a picture, maybe the suffix is wrong. In that case it could have been some known and supported format like "png", "jpeg" or "jpg".



(2) The "%20"symbol at the middle of your code also lifts a flag. Does not seem to be a correct URL object of swift. Maybe the URL you are using is not represented in the correct way.



(3) com~apple~CloudDocs also lifts a flag, since it would unlikely have a "~" symbol in a URL passed. This also strongly suggests that maybe the URL you are using is not represented in the correct way.



I think your URL is not pointing to where you are trying to point to, resulting in the "does exist" method return false and the loading resulting in nil.



If all of this does not fix your issue, post more details of the code. Specially what method you are calling to build/create this URL object you are using, that points to: file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn






share|improve this answer














It appears that the error can be many things, all not related to the class you are applying the code (AppDelegate) nor the methods you are calling.



My guess is that the URL you are calling is not correctly built (not pointing to the correct object you are trying to point to). For many reasons.



See if one of this reasons fix your issue:



(1) The end of the URL you are calling had the suffix "pgn". If you are looking to load a picture, maybe the suffix is wrong. In that case it could have been some known and supported format like "png", "jpeg" or "jpg".



(2) The "%20"symbol at the middle of your code also lifts a flag. Does not seem to be a correct URL object of swift. Maybe the URL you are using is not represented in the correct way.



(3) com~apple~CloudDocs also lifts a flag, since it would unlikely have a "~" symbol in a URL passed. This also strongly suggests that maybe the URL you are using is not represented in the correct way.



I think your URL is not pointing to where you are trying to point to, resulting in the "does exist" method return false and the loading resulting in nil.



If all of this does not fix your issue, post more details of the code. Specially what method you are calling to build/create this URL object you are using, that points to: file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/twic1121.pgn







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 22:51

























answered Nov 21 at 18:09









Luke Quinn

12




12












  • This URL is from the delegate method, that gets called when you open a file that has a registered file type with the application.
    – Dániel Grimm
    Nov 21 at 19:10










  • The URL is not the problem. %20 is the correct way to encode spaces in an URL. And the com~apple~CloudDocs part is typical for URLs of files stored in iCloud. Also nobody said that it is supposed to be an image file - pgn is a common file format to store chess games.
    – Sven
    Nov 21 at 19:50


















  • This URL is from the delegate method, that gets called when you open a file that has a registered file type with the application.
    – Dániel Grimm
    Nov 21 at 19:10










  • The URL is not the problem. %20 is the correct way to encode spaces in an URL. And the com~apple~CloudDocs part is typical for URLs of files stored in iCloud. Also nobody said that it is supposed to be an image file - pgn is a common file format to store chess games.
    – Sven
    Nov 21 at 19:50
















This URL is from the delegate method, that gets called when you open a file that has a registered file type with the application.
– Dániel Grimm
Nov 21 at 19:10




This URL is from the delegate method, that gets called when you open a file that has a registered file type with the application.
– Dániel Grimm
Nov 21 at 19:10












The URL is not the problem. %20 is the correct way to encode spaces in an URL. And the com~apple~CloudDocs part is typical for URLs of files stored in iCloud. Also nobody said that it is supposed to be an image file - pgn is a common file format to store chess games.
– Sven
Nov 21 at 19:50




The URL is not the problem. %20 is the correct way to encode spaces in an URL. And the com~apple~CloudDocs part is typical for URLs of files stored in iCloud. Also nobody said that it is supposed to be an image file - pgn is a common file format to store chess games.
– Sven
Nov 21 at 19:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417918%2fswift-4-app-crashes-when-open-file-from-icloud%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...