Titanium JIRA Archive
Appcelerator Community (AC)

[AC-1602] iOS8: WebView not able to access files in applicationDataDirectory anymore

GitHub Issuen/a
TypeBug
Priorityn/a
StatusResolved
ResolutionCannot Reproduce
Resolution Date2014-09-23T23:02:32.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsios
ReporterMichael Gangolf
AssigneeSteven Scott
Created2014-09-23T19:21:42.000+0000
Updated2016-03-08T07:38:04.000+0000

Description

QA Thread: http://developer.appcelerator.com/question/177846/ios8---filesystem-changes-applicationdatadirectory-vs-webview my app used to work like this (up to iOS7): a webview is loading a local webpage. Some json files are copied to applicationDataDirectory and if the user makes an "internal update" newer files get pulled from the internet into the applicationDataDirectory. The webview now opens the json files from there to have the newest files. For this I've just added '../Documents' in front of the files (inside the webpage local js files). That was working fine. But in iOS8 it looks like the content is in two seperate folders now: webview:
../Library/Developer/CoreSimulator/Devices/30.../data/Containers/Bundle/Application/E83.../Documents/file.json
applicationDataDirectory:
../Library/Developer/CoreSimulator/Devices/30.../data/Containers/Data/Application/F97../Documents/file.json
notice that it is now "Data" instead of "Bundle" and even a different ID. So how can I get from the webview folder to the writeable folder now? Even if I copy a index.html to applicationDataDirectory and try to set the webview url to that file it won't open. Passing the applicationDataDirectory into the webview page and setting the filepath via javascript won't load the file Some more infos about the problem: Apple Doc: https://developer.apple.com/library/ios/technotes/tn2406/_index.html Tech Republica: http://www.techrepublic.com/article/why-ios-8-could-break-your-hybrid-mobile-app/ Stackoverflow Thread: http://stackoverflow.com/questions/25794747/xcode-6-ios-8-simulator-data-and-bundle-folder-script Blog post: http://hetzel.net/2014-09-18/ios-8-folder-structure/

Comments

  1. Jon Alter 2014-09-23

    Can't reproduce

    Can't reproduce this issue. See below for code used to try to reproduce this issue. If this issue persists, please provide example code that we can use to reproduce the issue. *Steps to followed* 1. Create the 4 files below in the Resources directory of your app 2. Launch the app and click load (see a blue background...the css loaded) 3. Click the Click Me! button (see an alert dialog...the js file loaded)
       var win  = Ti.UI.createWindow({
       	backgroundColor: '#FFF'
       });
       win.open();
       
       var button = Ti.UI.createButton({
       	title: "load",
       	top: 20,
       	height: 50
       });
       win.add(button);
       button.addEventListener('click', function() {
       	copyFileToAppData('test.css');
       	copyFileToAppData('test.js');
       	wv.url = copyFileToAppData('test.html').nativePath;
       });
       
       function copyFileToAppData(name) {
       	var oldfile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, name);
       	Ti.API.info('\n');
       	Ti.API.info('\n#### oldfile path: ' + oldfile.nativePath);
       	var newfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, name);
       	newfile.write(oldfile.read()); // both old.txt and new.txt exist now
       	Ti.API.info('\n#### newfile path: ' + newfile.nativePath);
       	Ti.API.info('\n');
       	return newfile;
       }
       
       var wv = Ti.UI.createWebView({
       	top: 100,
       	bottom: 0,
       	left: 0, right: 0
       });
       win.add(wv);
       
       <!DOCTYPE html>
       <html>
       <head>
           <head>
               <link rel="stylesheet" type="text/css" href="test.css">
               <script src="test.js">
               </script>
           </head>
       </head>
       <body>
           <h1>This is a test</h1>
           <p>Background should be light blue.</p>
           <input type="button" onclick="testClick()" value="Click Me!">
       </body>
       </html>
       
       body {
           background-color: #d0e4fe;
       }
       
       h1 {
           color: orange;
           text-align: center;
       }
       
       p {
           font-family: "Times New Roman";
           font-size: 20px;
       }
       
       function testClick() {
           alert('Hi');
       }
       
  2. Michael Gangolf 2014-09-24

    Thank you for the testcase. That worked! Is it still possible to access the test.css/test.js file inside the applicationDataDirectory when the index.html is inside the Ressource folder? Or do I have to copy all files from Ressource to applicationDataDirectory first? On iOS7 you could just add "../Documents" before locally referenced files.
  3. Jon Alter 2014-09-24

    Not a problem. No, the folders have actually been moved which is why your workaround stopped working. You would have to figure out what the relative path is and use that, but Apple says that you should not do that [here](https://developer.apple.com/library/ios/technotes/tn2406/_index.html).
       Code which attempts to derive the path to the Documents or Library directories will return an invalid path on iOS 8. 
       Attempting to access this path will fail, and may terminate your app.
       
    Even if you ignore what Apple says and could get it to work, there is no guarantee that it won't break again in the future. Copying all of the files over is definitely the way to do it.

JSON Source