2012-04-10 6 views
1
나는 모바일 웹 응용 프로그램을 개발하고 가능한 안드로이드 등 나중에 다른 플랫폼에 전송하는 폰갭을 사용하고

,폰갭 등 filewriter

나는 장치에 로컬 파일에 쓸 수 있도록하려면

, 그러나 , 상관없이, 내가하는 일은 효과가 없다. 이러한 문제를 지원할 적절한 문서가 없습니다.

누구든지 도움을받을 수 있다면 크게 감사하겠습니다.

<!DOCTYPE html> 
<html> 
    <head> 
    <!--<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />--> 
    <meta name="viewport" content="width=device-width,initial-scale=1"/> 
    <link rel="stylesheet" href="jquery/jquery.mobile-1.0.1.min.css"/> 
    <script src="jquery/jquery-1.6.4.min.js"></script> 
    <link rel="stylesheet" type="text/css" href="styles.css"/> 
    <script src="jquery/jquery.mobile-1.0.1.min.js"></script> 
    <title>Hello World</title> 

    <!-- iPad/iPhone specific css below, add after your main css > 
    <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" /> 
    <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />-->   

    <!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here --> 
    <script type="text/javascript" charset="utf-8" src="cordova-1.5.0.js"></script> 
    <script type="text/javascript" charset="utf-8" src="NativeControls.js"></script> 
    <script type="text/javascript" charset="utf-8" src="js/main.js"></script> 
    </head> 
    <body onload="onBodyLoad()"> 
     <div data-role="page" id="mainpage"> 
      <div data-role="header"> 
       <h1>LOGIN</h1> 
      </div> 
      <div data-role="content"> 
      <img src="images/cslogosmall2.png" style="margin-top: -10px; margin-left: -5px;"/> 
      <form id="login" method="post"> 
       <p> 
       <ul data-role="listview" data-theme="g" id="contentlist"> 
        <b>Program:</b> 
        <input type="text" id="program"/> 
        <b>Username:</b> 
        <input type="text" id="username"/> 
        <b>Password:</b> 
        <input type="password" id="password"/> 
       </ul> 
       </p> 
       <br/> 
       <button type="submit" data-theme="a" id="submit" value="Submit"></button> 
       <a href="offline.html" onclick="test();">Offline</a> 
      </form> 
      </div> 
      <!--<div data-role="footer"> 
       <h1> Main Page Footer </h1> 
      </div>--> 
     </div> 
     <div data-role="page" id="contentpage"> 
      <div data-role="header"> 
       <h1> Content Page </h1> 

      </div> 
      <div data-role="content"> 
       <a href="#" data-role="button" onclick="history.go(-1);return false;" onClick=buttonDemo()> Back to Main Page </a> 
       <a href="#" data-role="button" id="beepbtn" onClick=beepbeep()> Beep!</a> 
      </div> 
      <div data-role="footer"> 
       <h1> Content Page Footer </h1> 
      </div> 
     </div>  
    </body> 
</html> 

그리고 여기 내 자바 스크립트 파일 :이 라인이 작동하지 않습니다

function validate() { 

    navigator.notification.alert("validate called"); 
    var program = document.getElementById('program'); 
    var username = document.getElementById('username'); 
    var password = document.getElementById('password'); 

    if (!program || program == "") { 
     navigator.notification.alert("Please enter a program"); 
     return false; 
    } 
    else if (!username || username == "") { 
     navigator.notification.alert("Please enter a username"); 
     return false; 
    } 
    else if (!password || password == "") { 
     navigator.notification.alert("Please enter a password"); 
     return false; 
    } 
    return true; 
} 

function beepbeep() { 
    //navigator.notification.alert("Beep function called"); 
    navigator.notification.beep(); 
} 


$(document).ready(function() { 

    $('#submit').click(function() { 
     var program = document.getElementById('program').value; 
     var username = document.getElementById('username').value; 
     var password = document.getElementById('password').value; 

     if (!program || program == "") { 
      alert("Please enter a program", null, "ClearSpider", "OK"); 
      return false; 
     } 
     else if (!username || username == "") { 
      alert("Please enter a username", null, "ClearSpider", "OK"); 
      return false; 
     } 
     else if (!password || password == "") { 
      alert("Please enter a password", null, "ClearSpider", "OK"); 
      return false; 
     } 
     return true; 
    }); 

    document.addEventListener("deviceready", onDeviceReady, false); 
    window.addEventListener("resize", orientationChange, false); 

}); 

function test() { 

    console.log("fail"); 
    var fs = FileWriter.write(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
    alert(LocalFileSystem.PERSISTENT); 
    alert("Test called"); 
} 

function onTabBarItem(id) 
{ 
    alert("hello"); 
    //jQT.goTo("#" + id, "slide"); 
    // if you need to modify the tabbar items that are shown, you should do 
    // so here. 
} 

function fail(error) { 
    console.log(error.code); 
} 

function gotFS(fileSystem) { 
    alert("at gotFS"); 
    fileSystem.root.getFile("readme.txt", {create: true}, gotFileEntry, fail); 
} 

function gotFileEntry(fileEntry) { 
    alert("at gotFileEntry"); 
    fileEntry.createWriter(gotFileWriter, fail); 
} 

function gotFileWriter(writer) { 
    alert("at gotFileWriter"); 
    writer.onwrite = function(evt) { 
     console.log("write success"); 
    }; 
    writer.write("some sample text"); 
    // contents of file now 'some sample text' 
    writer.truncate(11); 
    // contents of file now 'some sample' 
    writer.seek(4); 
    // contents of file still 'some sample' but file pointer is after the 'e' in 'some' 
    writer.write(" different text"); 
    // contents of file now 'some different text' 
} 


/* When this function is called, Cordova has been initialized and is ready to roll */ 
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch. 
see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html 
for more details -jm */ 
function onDeviceReady() { 
    // Initializating TabBar 

    //window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
    navigator.notification.alert("hi kaan", null, "hey", "SWEET"); 
    Cordova.exec("NativeControls.createTabBar"); 
    //var options = "top"; 
    var options = "bottom"; 

    navigator.notification.alert(window.requestFileSystem); 

    window.onorientationchange = function() { 
     var orientation = window.orientation; 

     switch(orientation) { 
      case 0: 

       Cordova.exec("NativeControls.showTabBar", options); 
       Cordova.exec("NativeControls.showToolBar"); 


       /* Add a descriptive message on "Handling iPhone or iPod touch Orientation Events" */ 
       document.getElementById("currentOrientation").innerHTML="Now in portrait orientation (Home button on the bottom)."; 
       break; 

      case 90: 

       Cordova.exec("NativeControls.showTabBar", options); 
       Cordova.exec("NativeControls.showToolBar"); 

       document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the left (Home button to the right)."; 
       break; 

      case -90: 

       Cordova.exec("NativeControls.showTabBar", options); 
       Cordova.exec("NativeControls.showToolBar"); 

       document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the right (Home button to the left)."; 
       break; 

      default: 

       Cordova.exec("NativeControls.showTabBar", options); 
       Cordova.exec("NativeControls.showToolBar"); 

       document.getElementById("currentOrientation").innerHTML="Now the orientation must be -180. default: case: "; 
       break;   
     }//end switch 
    }//end window.orientationchange 

    //var taboptions = {"onSelect":onTabBarItemSelect}; 
    var moreNum = 1; 

    Cordova.exec("NativeControls.showTabBar", options); 

    var onSelect = {"onSelect":onTabBarItem}; 
    //onSelect = "hahaha"; 

    Cordova.exec("NativeControls.createTabBarItem", "test", "test", "tabButton:Recents", onSelect); 
    Cordova.exec("NativeControls.createTabBarItem", "test2", "test2", "tabButton:Search", onSelect); 
    Cordova.exec("NaviteControls.createTabBarItem", "test3", null, "tabButton:More", onSelect); 

    Cordova.exec("NativeControls.showTabBarItems", "test", "test2", "test3"); 
    Cordova.exec("NativeControls.selectTabBarItem", "test"); 

    //Cordova.exec("NativeControls.tabBarItemSelected", "1"); 

    /*nativeControls = window.plugins.nativeControls; 
    navigator.notification.alert(nativeControls); 
    nativeControls.createTabBar(); 

    // Books tab 
    nativeControls.createTabBarItem(
    "books", 
    "Books", 
    "tabButton:Recents", 
    "", 
    {"onSelect": function() { 
     books(); 
    }} 
); 

    // Stats tab 
    nativeControls.createTabBarItem(
    "finished", 
    "Finished", 
    "tabButton:Recents", 
    "", 
    {"onSelect": function() { 
     finished(); 
    }} 
); 

    // About tab 
    nativeControls.createTabBarItem(
    "about", 
    "About", 
    "tabButton:Recents", 
    "", 
    {"onSelect": function() { 
     about(); 
    }} 
); 

    // Compile the TabBar 
    nativeControls.showTabBar(); 
    nativeControls.showTabBarItems("books", "finished", "about"); 
    nativeControls.selectTabBarItem("books");*/ 
} 


function orientationChange() { 
    var nativeControls = window.plugins.nativeControls; 
    nativeControls.resizeTabBar(); 
} 

function books() { 


} 

function about() { 

} 

function finished(){ 

} 

참고 : navigator.notification.alert(window.requestFileSystem);는 경고를 표시하지 않습니다이

여기 내 html 파일입니다.

+0

가능성, 경고 때문에 위의 Cordova.exec의 문제로 표시되지 않습니다. 플러그인에 추가하기 전에 파일 API http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html#File을 사용하여 기본 파일 시스템 앱을 다운로드 해보세요. –

+0

폴 감사합니다,하지만 그 작동하지 않았다. 코드를 다음과 같이 변경했습니다. navigator.notification.alert ("hi kaan", null, "hey", "SWEET"); navigator.notification.alert (window.requestFileSystem); Cordova.exec ("NativeControls.createTabBar"); 아직 운이 없습니다. – c0d3Junk13

+0

그래서 나는 네비게이터 객체 자체에 경보를 울리려고했고 경고를받지 못했습니다. 그러나 내가 작동했을 때 navigator.toString()에 경고했다. Navigator.LocalFileSystem.toString()을 시도해도 작동하지 않습니다. 나머지 탐색 객체가 초기화 될 때 LocalFileSystem 객체가 초기화되지 않는 방법을 모르겠습니다. 어떤 아이디어? – c0d3Junk13

답변

0

경고를 성공적으로 보았을 때 어제 1.4.1을 사용하고있었습니다. 1.5.0으로 전환하면 경고가 null로 인쇄됩니다. 이것은 cordova-1.5.0에서 추가 된 네임 스페이스가 필요하기 때문입니다.

은 그러나, 1.5.0에서 API sample은 여전히 ​​나를 위해 작동 :

function gotFS(fileSystem) { 
    navigator.notification.alert(window.requestFileSystem); 
    myFS = fileSystem; 
    console.log(fileSystem.name); 
    console.log(fileSystem.root.name); 
    $('#file-system-text').html("File System: <strong>" + fileSystem.name + "</strong> " + 
      "Root: <strong>" + fileSystem.root.name + "</strong>"); 
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, createGotFileEntry, writeFail); 
} 
function createFile() { // button onclick function 
    if (myFS) { 
     gotFS(myFS); 
    } else { 
     navigator.notification.alert(window.requestFileSystem); 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, failFS); 

    } 
} 
+0

폴 감사합니다! 당신도 문서 onload 함수를 게시 할 수 있습니까? 그래서 나는 모든 것이 만들어지는 것을 볼 수 있습니다. – c0d3Junk13

+0

Btw, 코르도바 1.6.0이 오늘 발표되었습니다. 그래서 나는 그걸 바꿔 다시 테스트 해 보았습니다. window.requestFileSystem 유형의 함수가 작동하지 않기 때문에 잘못된 점을 설정했을 수도 있습니다. 나는 결과를 게시 할 것이다. – c0d3Junk13

+0

전체 앱은 http://code.google.com/a/eclipselabs.org/p/mobile-web-development-with-phonegap/source/browse/tags/r1.2/com.mds.apg/에 있습니다. resources/phonegap/Sample/파일 API는 http://code.google.com/a/eclipselabs.org/p/mobile-web-development-with-phonegap/source/browse/tags/r1.2/에서 구현됩니다. com.mds.apg/resources/phonegap/Sample/apis/file.js –