我正在为iPhone制作应用程序,我正在使用Javascript在UIWebView中制作嵌入youtube。我想在javascript的某些事件上打开/读/写一个文件,然后想在objective-c代码中使用该文件。但是我怎么能这样做呢?我的意思是在哪个位置可以保存并打开文件。
我创建了一个名为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>
感谢 Akansha
答案 0 :(得分:0)
// Get file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString filePath = [documentsDirectory stringByAppendingPathComponent:filename];
// Read file contents
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
// Write file contents
[[content dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO];
答案 1 :(得分:0)
such as window.state = "someting";
第二:通过Objective-C读取状态;
NSString *state = [webView stringByEvaluatingJavaScriptFromString:@"window.state"];