A Microsoft framework for building cross-platform mobile apps using .NET and C# with native performance and user interfaces.
xamarin forms ios error msg: The file “CFNetworkDownload_qkPMxP.tmp” couldn’t be opened because you
One of the things that our app does is create and share photos. Staff take photos and upload them. Later other staff may need to download them to use on their device.
For iOS devices we use a CustomSessionDownloadDelegate to download several photos.
The Download stores the photo in a location that looks like this:
location=file:///var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/*/CFNetworkDownload_sfwacR.tmp
We want to copy the photo to a location, so we can easily access it in our app, that looks like this:
destinationURL=file:///var/mobile/Containers/Data/Application/B61707E5-758F-49AB-9529-37D2D3756B66/Documents/Playroom%20Overview.jpg
For some years now the following has done the job.
bool success = fileManager.Copy(location, destinationURL, out error);
Recently it started throwing an error and not doing the job. The error is:
The file “CFNetworkDownload_sfwacR.tmp” couldn’t be opened because you don’t have permission to view it.
I suspect that iOS tightened up its Permissions, but I can't find the one I need to allow this task to proceed.
Thanks for looking at this.
Developer technologies | .NET | Xamarin
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-10-18T03:28:43.027+00:00 I'm afraid this is not caused by the permission, I check the docs, there is no no update after 2018.
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
You could try to download one photo and named "xxx.jpg" or "xxx.png" intoCachespath , then copy it toDocuments. I think theCFNetworkDownload_sfwacR.tmpfile has been damaged.
In addition, may I ask do you have the same issue on both the simulator and the real device? -
WillAutioItrax • 211 Reputation points
2021-10-19T05:51:57.483+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Thanks for your reply. I have not been able to run it on an iOS simulator yet. I am also following up on your comment that the file may be damaged. I hope to hear more on that soon.
I will let you know what I find. -
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-10-19T08:32:23.987+00:00 Ok, you could also check the disk space then uninstall the app and install the app to download images again, the doc said the system may delete the Caches directory on rare occasions when the system is very low on disk space
-
WillAutioItrax • 211 Reputation points
2021-10-20T00:04:29.707+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Thanks for the tip. It is worth checking. In this case the real iphone that I am testing with has 20 of 64 GB used. So I should be safe there.
-
WillAutioItrax • 211 Reputation points
2021-10-20T00:38:14.387+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Hi. I've been trying to submit a longish reply with some of the details that you requested, but the Submit button does nothing. So, I am trying shorter replies that are hopefully reasonably complete. Thanks for your understanding.
-
WillAutioItrax • 211 Reputation points
2021-10-20T00:39:18.753+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Part 1) I was able to get the Simulator working today (I usually use a real phone). It failed differently from the real phone. I got this bundle of errors:
2021-10-19 16:05:34.274258-0700 myApp.iOS[93467:7666508] BackgroundSession <85B2CA91-FD66-41D1-8D9D-2ACE97A0E220> an error occurred on the xpc connection to setup the background session: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.nsurlsessiond" UserInfo={NSDebugDescription=connection to service named com.apple.nsurlsessiond} 2021-10-19 16:05:34.274536-0700 myApp.iOS[93467:7666508] BackgroundSession <85B2CA91-FD66-41D1-8D9D-2ACE97A0E220> failed to create a background NSURLSessionDownloadTask, as remote session is unavailable -
WillAutioItrax • 211 Reputation points
2021-10-20T00:40:38.17+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Part 2) [looks like smaller groups of text works -0 so far]
2021-10-19 16:06:08.604758-0700 myApp.iOS[93467:7666731] Task <6A21F6D2-EE39-43E1-926D-3C0213DF01E4>.<6> finished with error [-1] Error Domain=NSURLErrorDomain Code=-1 "unknown error" UserInfo=NSErrorFailingURLStringKey=**https://itraxthumbs.azureedge.net/100thumbs/350/1322756/Photos/Rooms/Main%20-%20Playroom/Overview/2103J004%20Playroom%20Overview%204WA.jpg**, NSErrorFailingURLKey=**https://itraxthumbs.azureedge.net/100thumbs/350/1322756/Photos/Rooms/Main%20-%20Playroom/Overview/2103J004%20Playroom%20Overview%204WA.jpg**, _NSURLErrorRelatedURLSessionTaskErrorKey=( "BackgroundDownloadTask <6A21F6D2-EE39-43E1-926D-3C0213DF01E4>.<6>" -
WillAutioItrax • 211 Reputation points
2021-10-20T00:44:57.077+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Part 3)
), _NSURLErrorFailingURLSessionTaskErrorKey=BackgroundDownloadTask <6A21F6D2-EE39-43E1-926D-3C0213DF01E4>.<6>, NSLocalizedDescription=unknown error}Note: the bolded URLs in Note 2) can be copied and pasted into a browser and they download a photo. So, they work.
In part 1) there is a mention that
failed to create a background NSURLSessionDownloadTask, as remote session is unavailable
That may be the main reason that the Simulator fails.
So, what causes that? -
WillAutioItrax • 211 Reputation points
2021-10-20T00:55:56.577+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Part 4)
In the real iphone, I still get:error.LocalizedDescription = The file “CFNetworkDownload_6ZiDFs.tmp” couldn’t be opened because you don’t have permission to view it.
where error is in
bool success = fileManager.Copy(location, destinationURL, out error);
and it used to work but now it doesn't.
I am not concerned about the Simulation at this time (unless a solution to it also solves the real phone issue :- )
I you can point me to a working example that downloads multiple photos and stores them in a place that the app can access, that would also be most welcomed.
I think the file “CFNetworkDownload_6ZiDFs.tmp” is named by the iOS code somehow as I do not name it.
-
WillAutioItrax • 211 Reputation points
2021-10-20T00:59:21.87+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Part 5)
When you mentioned earlier to
download a photo to caches path, I think that is supposed to happen with the existing code as it tries to save the downloaded file to something like:/var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/*/CFNetworkDownload_6ZiDFs.tmp
or did you mean something else.
Thanks so much for wading thru all this!
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-10-20T05:55:16.627+00:00 About Simulator, it means it doesn't allow the background task, you need to make sure the code sign is correct. About your project, would you mind sharing me a minimal reproducible demo to test? You could upload it to github and attach the link here.
In addition, there is a Xamarin.iOS demo to download a image. you can also refer to
https://stackoverflow.com/questions/59632849/xamarin-forms-how-to-download-an-image-save-it-locally-and-display-it-on-scree
https://stackoverflow.com/questions/65599968/downloading-multiple-images-to-cache-in-xamarin
https://social.msdn.microsoft.com/Forums/en-US/6c9cf6d8-d567-4493-ba67-ea6e0c61c1dd/how-to-download-multiple-files-in-the-background-for-a-xamarin-forms-ios-project?forum=xamarinforms -
WillAutioItrax • 211 Reputation points
2021-10-26T00:07:21.577+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Hi. I am still working on the minimal reproduceable demo. I came across SimpleBackgroundTransfer here:
https://learn.microsoft.com/en-us/samples/xamarin/ios-samples/simplebackgroundtransfer/It seems to work fine.
My problem is that it is in Xamarin.iOS which is different enough from Xamarin Forms that I am stumbling over how to implement it. I have searched for but not found an example that downloads a pic/file in Xamarin Forms in the background.
If you come across something like that I would be most appreciative. Meanwhile I'll keep at it.
Thanks. -
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-10-26T05:53:29.16+00:00 You could try to use
DependencyServiceto load theSimpleBackgroundTransferViewControllerin the demo, refer to https://stackoverflow.com/questions/63812069/display-viewcontroller-from-native-forms?rq=1 -
WillAutioItrax • 211 Reputation points
2021-10-28T23:17:20.77+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) I was able to find/modify an app that replicates the behavior that I am seeing in our app.
I used the following GitHub entry to make my sample.
And here is a link to my variation:
iosbackroundingissue1.gitI changed the file to be downloaded as the original did not seem to exist at that location any more.
I also added a couple of WriteLines before and after the following code found in CustomSessionDownloadDelegate:
bool success = fileManager.Copy(location.Path, targetFileName, out error);The Copy command above results in the following error:
Error during the copy: The file “CFNetworkDownload_5HOsEE.tmp” couldn’t be opened because you don’t have permission to view it.Thanks for taking a look at this!
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-10-29T09:48:23.157+00:00 Sorry, I can't visit the
.gitlink, would you mind uploading it to github and attach the link here? I will follow your step to test thisXamarinFormsBackgroundingdemo. -
WillAutioItrax • 211 Reputation points
2021-10-29T15:22:26.183+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Sorry about that.
Try this:
iosbackgroundingissue1 -
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-11-01T09:18:11.88+00:00 I download the demo and test, may I ask if
XamarinFormsBackgrounding/XamarinFormsBackgrounding.zipthis file is necessary to your project? I delete this file, go to change the" iOS build" , change the "Sopported architectures " tox86_64, add this key toinfo.plist, then I updateXamarin.forms, it can build and run. However, there is a unknown NSurlsession error when I click " Start Internet Download". And it means the remote session is unavailable, I change the URL, the unknown error still exist. So weird, I will keep exploring, you could have a try.<string> NSAppTransportSecurity</string> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> -
WillAutioItrax • 211 Reputation points
2021-11-01T15:58:23.963+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Thanks for persisting with looking at this.
That .zip file came from the original version in GitHub. I expect that it can be dropped.
I'll let you know if I make any progress. -
WillAutioItrax • 211 Reputation points
2021-11-02T05:23:54.847+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Sorry - I spent most of my day getting the iOS provisioning profile working - again. (it had been working but then it had to have it's annual renewal) It even meant going into the office to try stuff (I'm still working from home) The provisioning seemed to work in the office but not at home?? I'll try again in the morning. I'll let you know how it goes.
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-11-02T06:18:18.27+00:00 OK, I will try to use this demo to test again.
https://learn.microsoft.com/en-us/samples/xamarin/ios-samples/simplebackgroundtransfer/ -
WillAutioItrax • 211 Reputation points
2021-11-03T01:07:34.017+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Another day another step closer.
0) I got the provisioning profile to work toward the end of the day. It should have been real simple - I had one that worked all year. After renewing it, I could not test on an iPhone without a Mac. But that is another story.1) I have three iOS devices at my disposal.
a) iPhone 6 plus with iOS 12.5.5
b) iPhone 8 with iOS 15.1
c) iPad Air 4th gen with 14.8.1I managed to get two sets of tests done. Each of these devices with:
i) the original iosbackgroundingissue1 version of the app.
ii) upgrading to XF 5.0.0.2196each device behaved differently from each other.
The iPhone 6, iOS12.5.5 worked with both versions of the app.
iPhone 8 with iOS 15.1 "could not be deployed to the device". it looks a lot like this one:
xamarin-ios-deployment-failing-after-ios-15-update.htmlThe iPad Air 4th gen with 14.8.1 gets into the downloading and fails with something like "Error during the copy: The file “CFNetworkDownload_0dDwVR.tmp” couldn’t be opened because you don’t have permission to view it."
Previously I had a look at SimpleBackgroundTransfer. It looks to me to be written for Xamarin iOS - and I am not sure how to convert that into something that works in Xamarin Forms iOS.
-
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-11-03T05:33:30.803+00:00 The demo works in your iPhone 6 and iPhone 8, right? About the deployment fail error, you could update Xcode and VS, iOS 15 has been supported, or you could refer to https://learn.microsoft.com/en-us/answers/questions/567336/xamarin-apple-account-error-there-was-an-error-whi.html About converting the SimpleBackgroundTransfer demo to Xamarin Forms, you could create a new thread and mention me, then we can work together to figure it out.
-
WillAutioItrax • 211 Reputation points
2021-11-03T23:59:24.14+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Thanks for persisting with this. For Reference, these are the devices that I am testing on:
a) iPhone 6 plus with iOS 12.5.5
b) iPhone 8 with iOS 15.1
c) iPad Air 4th gen with 14.8.1The iPhone 6 plus with iOS 12.5.5 works, well, sometimes and sometimes it hangs on the splash screen.
That was in VS 2019 ver 16.11.5.Upon reading your comments and following the links, I installed VS 2022 RC3. That got rid of the iOS 15 related error of "could not be deployed to the device".
Here are my results for testing on VS 2022 RC3
iPhone 8
2021-11-03 15:23:08.413 Xamarin.PreBuilt.iOS[704:179306] location.Path : /var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/*/CFNetworkDownload_JQk2Ec.tmp
2021-11-03 15:23:11.741 Xamarin.PreBuilt.iOS[704:179306] Error during the copy: The file “CFNetworkDownload_JQk2Ec.tmp” couldn’t be opened because you don’t have permission to view it.
iPad
2021-11-03 15:27:31.965 Xamarin.PreBuilt.iOS[3312:1296530] location.Path : /var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/*/CFNetworkDownload_RQDus2.tmp
2021-11-03 15:27:36.526 Xamarin.PreBuilt.iOS[3312:1296530] Error during the copy: The file “CFNetworkDownload_RQDus2.tmp” couldn’t be opened because you don’t have permission to view it.
iPhone 6
it worked sometimes and sometimes it hung on the splash screen.
What I am mainly interested in is how to fix the errors The file “CFNetworkDownload_RQDus2.tmp” couldn’t be opened because you don’t have permission to view it.
Regards,
Will -
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-11-04T09:29:17.983+00:00 I still can't reporoduce your issue. There is a similar issue about remove temp file, it caused by Xcode. You could also try to use
cache folderrefer to https://developer.apple.com/forums/thread/93498 -
WillAutioItrax • 211 Reputation points
2021-11-04T23:45:35.56+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Thanks for trying again. Somehow I missed that you were not able to reproduce the error. Are you using a real device or a simulator? I have mainly been using real devices.
I have been searching for clues on the web. A colleague and I have tried a variety of suggested solutions, but none solved it. Several show the error that the "From" file is not accessible and some approaches show an error in copying to the "To" file.
I ask myself, what is different with my setup that I get an error and no one else does?
I went back to the source - cloned https://github.com/RobGibbens/XamarinFormsBackgrounding
and replaced
http://xamarinuniversity.blob.core.windows.net/ios210/huge_monkey.png
withhttps://upload.wikimedia.org/wikipedia/commons/9/97/The_Earth_seen_from_Apollo_17.jpgand then I ran it on a real device (iPad Air 4th gen on VS2019, and iPhone 8 on VS2022) and got the same message:
The file “CFNetworkDownload_XqBN1F.tmp” couldn’t be opened because you don’t have permission to view it.Life's little mysteries...
-
WillAutioItrax • 211 Reputation points
2021-11-05T00:47:39.113+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Something else that I tried was to list all the files in the directory of the location file (location that the file gets downloaded to):
/var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/*/CFNetworkDownload_4kUppc.tmpand took the directory from there and used it in this loop:
var entries = Directory.EnumerateFiles("/var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/*/");
foreach (var item in entries)
{
Console.WriteLine(item);
}and that gave me an unhandled exception:
System.UnauthorizedAccessException
Message=Access to the path '/var/mobile/Library/Caches/com.apple.nsurlsessiond/Downloads/*' is denied.How do I get access to that directory to copy the recently downloaded file (if it actually got downloaded) to a location that I can use it further in my app?
Thanks,
Will -
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) • 36,456 Reputation points • Microsoft External Staff
2021-11-05T02:11:23.94+00:00 I deploy to my iOS 15 iphone7 plus , and still can't see this error. This is a issue about Apple, you can refer to https://developer.apple.com/forums/thread/73426
After re-signing, this file belongs to the new bundleid, and the info.plist file does not match the new bundleid, for examalpe A application cannot save the file in B application.
There is a workaround you could have a try, uninstall the app in your device, shut down and restart your device first.Add the App Groups entitlement to your app
Add a "groups.<your identifier>" sharedContainerIdentifier to your URLSession config. -
WillAutioItrax • 211 Reputation points
2021-11-05T14:18:20.447+00:00 @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) Thanks again! Looks like we are getting closer.
The following is added for reference by my future self...
To add the AppGroups entitlement to our app we will be following these steps:
https://learn.microsoft.com/en-us/xamarin/ios/deploy-test/provisioning/capabilities/app-groups-capabilitiesfollowed by
https://learn.microsoft.com/en-us/xamarin/ios/deploy-test/provisioning/entitlements?tabs=windows
How do I add a sharedContainerIdentifier to your URLSession configuration?
in Downloader.cs there is this code where I added the sharedContainerIdentifier:
void InitializeSession () { using (var sessionConfig = UIDevice.CurrentDevice.CheckSystemVersion (8, 0) ? NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration (sessionId) : NSUrlSessionConfiguration.BackgroundSessionConfiguration (sessionId)) { sessionConfig.AllowsCellularAccess = true; sessionConfig.NetworkServiceType = NSUrlRequestNetworkServiceType.Default; sessionConfig.HttpMaximumConnectionsPerHost = 2; **sessionConfig.SharedContainerIdentifier("groups.com.company.app");** var sessionDelegate = new CustomSessionDownloadDelegate (targetFilename); this.session = NSUrlSession.FromConfiguration (sessionConfig, sessionDelegate, null); } }
Sign in to comment