2016-10-12 98 views
1

我一直在試圖找出一種方法來獲取本地網頁內容,以在本地腳本中加載和設置webview html內容。 我已經成功地在模擬器上工作,但它似乎無法在設備上工作。我一直在搜索,大多數人都說我需要找到我的應用程序的「文檔」目錄中的我的html文件,但據我所見,當我部署應用程序時,無法獲取我需要的文件應用程序。任何人都可以闡明如何獲取位於應用程序的文檔文件夾中的文件,或在運行時將其複製到應用程序中。謝謝如何在本地腳本中將文件加載到webview中

var createViewModel = require("./main-view-model").createViewModel; 
var orientationModule = require("nativescript-screen-orientation"); 
var fs = require("file-system"); 

var webViewInterfaceModule = require('nativescript-webview-interface'); 
var oWebViewInterface; 
var page; 

function pageLoaded(){ 
    setupWebViewInterface(page); 
    // orientationModule.setCurrentOrientation("landscape", function(){  
    //  //console.log("landscape orientation set"); 
    // }); 
} 

// Initializes plugin with a webView 
function setupWebViewInterface(page){ 
    var webView = page.getViewById('webView'); 
    var documents = fs.knownFolders; 

    oWebViewInterface = new webViewInterfaceModule.WebViewInterface(webView, documents.currentApp().path + '/htllo/index.html'); 
} 

function navigatingTo(args) { 
    page = args.object; 
    page.bindingContext = createViewModel(); 
} 

exports.pageLoad  = pageLoaded; 
exports.navigatingTo = navigatingTo; 
+0

要訪問Android設備'你應該使用本地代碼Documents'文件夾。在這裏你會找到一個例子 - https://github.com/tsonevn/ReadFilesFromDevice/blob/master/app/main-page.ts#L21,怎麼做。關於你的問題,你應該在'getExternalStoragePublicDirectory'方法中設置'DIRECTORY_DOCUMENTS'而不是'DIRECTORY_DOWNLOADS'。有關更多信息,可以在Android中訪問哪些目錄,可以在這裏查看文章 - https://developer.android.com/reference/android/os/Environment.html。 –

+0

謝謝你的回答。它不完全是我想要的。基本上。我的客戶需要一個應用程序,我可以登錄並在本機應用程序組件中註冊。然後從那裏被轉發到一個'家'頁面,這是一個網頁窗口,我加載了一個位於本地的index.html。所以,當我把該文件夾中的應用程序目錄中的HTML目錄在模擬器中工作正常,但不是在iOS設備上。我發現另一個人說他有同樣的問題,直到他開始從文檔目錄加載文件而不是應用程序目錄。 – TheMan68

+0

它的文件結構等問題沒有問題,不想弄亂本機代碼,我只需要知道如何1.當我編譯到設備或移動它們時,將我的html文件夾部署到ios和android上的Documents目錄從應用程序目錄到第一個應用程序加載的文檔目錄。非常感謝你的hlep – TheMan68

回答

2

以下是如何從本地文件添加WebView src的示例。在這個例子中已經顯示了這兩種情況都帶有nativescript-webview-interface插件,沒有它。 ~/將路徑返回到您的app文件夾。

主page.xml

<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" loaded="onLoaded"> 
    <GridLayout rows="150 *" columns="*"> 
     <WebView row="0" col="0" id="WebViewId"/> 
     <WebView row="1" col="0" id="WebViewId2"/> 
    </GridLayout> 
</Page> 

主page.ts

import { EventData } from 'data/observable'; 
import { Page } from 'ui/page'; 
import { HelloWorldModel } from './main-view-model'; 
var webViewInterfaceModule = require("nativescript-webview-interface"); 
import {WebView} from "ui/web-view"; 

var oWebViewInterface; 
export function onLoaded(args: EventData) { 
    let page = <Page>args.object; 
    var webView:WebView = <WebView>page.getViewById('WebViewId'); 
    oWebViewInterface = new webViewInterfaceModule.WebViewInterface(webView, '~/index.html'); 
    var SecondWebView:WebView = <WebView>page.getViewById('WebViewId2'); 
    SecondWebView.src="~/secondpage.html" 
    page.bindingContext = new HelloWorldModel(); 
} 
+0

哇。感謝您對指向您的應用目錄的'〜/'的評論。我在Linux上很自然地認爲它指向了主目錄。我試過這個,但仍然沒有在設備上工作,因爲它指出了以下鏈接:https://whoischris.com/nativescript-js/。似乎有一個開放的問題,直接從應用程序目錄加載蒼蠅,他指出,一旦他將目錄轉移到文檔目錄,它解決了加載問題。但是當我用應用程序部署文件時,我不知道如何將這些文件放入Documents dir中。到目前爲止,沒有辦法將文件移動到該目錄 – TheMan68

+0

謝謝你的幫助 – TheMan68

相關問題