2012-04-16 54 views
2

我正在使用JavaScript製作iPhone應用程序。我想打開一個文件作爲結果文件,我需要從JavaScript寫一些結果日誌和數據。隨着iPhone我使用文檔目錄來打開文件和做讀/寫操作。但現在我想在JavaScript中做到這一點。現在我怎樣才能使用這個documentsDirectory寫一個文件並在以後從Xcode訪問它。 有什麼建議嗎?從Xcode項目的Javascript中讀取寫入文件

感謝 Akansha

我使用以下YouTube API代碼。我創建了一個名爲youtubeAPI.html的文件,並從Objective-C代碼中調用此文件。它成功打開頁面。現在我想打開一個文件並在每次改變它的狀態時寫下它,然後需要在obejective-c的最終結果中顯示該文件。請告訴我如何做到這一點。

<!DOCTYPE HTML> 
<html> 
<body> 
<div id="player"></div> 
<script> 
    //Load player api asynchronously. 
    var tag = document.createElement('script'); 
    tag.src = "http://www.youtube.com/player_api"; 
    var firstScriptTag = document.getElementsByTagName('script')[0]; 
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 
    var done = false; 
    var player; 
    function onYouTubePlayerAPIReady() { 
     player = new YT.Player('player', { 
      height: '390', 
      width: '640', 
      videoId: 'JW5meKfy3fY', 
      events: { 
      'onReady': onPlayerReady, 
      'onStateChange': onPlayerStateChange 
      } 
     }); 
    } 
    function onPlayerReady(evt) { 
     evt.target.playVideo(); 
    } 
    function onPlayerStateChange(evt) { 
     if (evt.data == YT.PlayerState.PLAYING && !done) { // NEED TO ADD FILE OPERATION HERE 
      setTimeout(stopVideo, 6000); 
      done = true; 
     } 
    } 
    function stopVideo() { 
     player.stopVideo(); 
    } 
</script> 
</body> 
</html> 

回答

0

我已經寫在PhoneGap的文件,但從來沒有從Objective C的訪問它。 但我很確定任何寫在JS或Obj-C中的文件都寫在文檔目錄中。 如需幫助,可以查看這些鏈接PhoneGap file writingW3 File api。 PhoneGap中的示例很容易理解,我認爲它應該使用W3 JS文件api。

+0

請檢查我編輯的問題 – Akansha 2012-04-16 09:51:29

+0

如果我不想再打開目標C中的文件。那麼我怎麼才能在xcode項目中使用javascript編寫和編寫文件。 – Akansha 2012-04-18 06:01:52

0

這是我寫的一些代碼,用於讀寫寫入適用於我的文件系統。請注意,fileWriter會將絕對路徑傳遞迴文件,但文件讀取器需要相對路徑,因此需要在底部使用getRelativePathToFile方法。

function onPlayerStateChange(evt) { 
if (evt.data == YT.PlayerState.PLAYING && !done) { // NEED TO ADD FILE OPERATION HERE 

PG.saveToDisk('youTubeStateChange.txt', 'on player state change', 
    function (pathToFile){ 
    alert('file written to ' + pathToFile); 
    } 
); 

setTimeout(stopVideo, 6000); 
    done = true; 
} 
} 

// make error codes meaningful 
PG.FileErrors = { 
1:  'File not found', 
2:  'Security error', 
3:  'Action Aborted', 
4:  'File not readable', 
5:  'Encoding error', 
6:  'No modification allowed', 
7:  'Invalid state error', 
8:  'Syntax error', 
9:  'Invalid modification error', 
10: 'Quota Exceeded', 
11: 'Type mismatch', 
12: 'Path exists' 
}; 

// writes a file to the fileSystem 
// and sets the path to the file 
// which can be used in the callback 
// @param {} fileContent 
// @param {string} fileName 
// @param {function} callback 
PG.saveToDisk = function (fileContent, fileName, callback){ 
var pathToFile, 
    accessFileSystem = window.requestFileSystem; 

// something went wrong.. 
function fail (error){ 
    console.log('-- saveToDisk: error ' + error + ' ' + PG.FileErrors[error.code]); 
} 



// finally write to a file 
function gotFileWriter (writer){ 
    console.log('-- saveToDisk: gotFileWriter'); 

    // ..yaay! it worked 
    writer.onwrite = function (event){ 
    if(typeof callback == 'function'){ 
    console.log('-- saveToDisk: calling back'); 

    callback.call(this, pathToFile); 
    } 
    }; 

    // working it.. 
    writer.write(fileContent); 

    delete gotFileWriter; 
} 

// now that we have a location 
// on disk we can write to it.. 
function gotFileEntry (fileEntry){ 
    console.log('-- saveToDisk: gotFileEntry'); 

    pathToFile = fileEntry.toURI(); 

    // ..not quite 
    fileEntry.createWriter(gotFileWriter, fail); 

    delete gotFileEntry; 
} 

// get file for writing 
function getFileSystem (fileSystem){ 
    console.log('-- saveToDisk: getFileSystem'); 

    fileSystem.root.getFile(fileName, {create: true, exclusive: false}, gotFileEntry, fail); 

    delete getFileSystem; 
} 


if(typeof accessFileSystem != 'undefined'){ 
    console.log('-- saveToDisk: accessed file system'); 

    accessFileSystem(LocalFileSystem.PERSISTENT, 0, getFileSystem, fail); 

    // clean up as we go to save space in 
    // call stack and prevent 'call stack 
    // size exceeded' error 
    delete accessFileSystem; 
} else { 
    console.log('-- saveToDisk: no requestFileSystem so exiting'); 
} 

}; 

// get a file at a source from 
// disk and pass it to callback 
// @param {function} callback 
PG.getFromDisk = function (source, callback){ 
var accessFileSystem = window.requestFileSystem; 

// something went wrong.. 
function fail (error){ 
    console.log('-- getFromDisk: error ' + PG.FileErrors[error.code]); 
} 

//we read it 
// @param {string} method 
// defaults to text unless image is specified 
function readData (file, method){ 
    console.log('readDataUrl'); 

    var reader = new FileReader(), 
    method = method == 'image' ? 'readAsDataURL' : 'readAsText'; 

    reader.onloadend = function (event){ 
    if(typeof callback == 'function'){ 
    console.log('-- getFromDisk: calling back'); 

    callback.call(this, event.target.result); 
    } 
    }; 

    reader[method](file); 
} 

// what do we do with the file? 
function gotFile (file){ 
    console.log('-- gotFile'); 

    readData(file); 
} 

// get the file 
function gotFileEntry (fileEntry){ 
    console.log('-- gotFileEntry'); 

    fileEntry.file(gotFile, fail); 
} 

// get file reader 
function getFileSystem (fileSystem){ 
    console.log('-- getFileSystem: ' + source); 

    fileSystem.root.getFile(source, null, gotFileEntry, fail); 
} 

    // why did the fileSystem cross the road? 
    // so it could callback 4 times! 
if(typeof accessFileSystem != 'undefined'){ 
    accessFileSystem(LocalFileSystem.PERSISTENT, 0, getFileSystem, fail); 
} else { 
    console.log('-- getFromDisk: no requestFileSystem so exiting'); 
} 

}; 

// get relative path to saved file 
// because that is what the file 
// reader needs 
// @param {string} pathToFile 
PG.getRelativePathToFile = function (pathToFile){ 
if(/localhost|mnt/.exec(pathToFile)){ 
    var root = /localhost|mnt/.exec(pathToFile)[0]; 

    pathToFile = pathToFile.substring(pathToFile.indexOf(root)); 
    pathToFile = pathToFile.substring(pathToFile.indexOf('/')); 
} 

console.log('-- getRelativePathToFile: pathToFile = ' + pathToFile); 

return pathToFile; 
}; 
+0

我編輯了我的問題。 PLease help – Akansha 2012-04-16 09:51:49

+0

你的意思是改變狀態?是否每次調用onPlayerStateChange函數? 我已更新代碼以顯示如何使用JavaScript編寫文件。我不知道你將如何在Objective-C中打開文件,但聽起來你已經解決了這個問題。 – Peter 2012-04-17 06:49:52

+0

Nops,我沒有工作。仍然在與此掙扎。需要檢查可以做什麼。儘管感謝您的幫助。我會試着看看你的代碼是否可以用於我的目的。 – Akansha 2012-04-17 07:08:06