【问题标题】:When loading an external URL in a Webview/UIWebView, is it possible to load a local JS file?在 Webview/UIWebView 中加载外部 URL 时,是否可以加载本地 JS 文件?
【发布时间】:2013-03-14 19:42:54
【问题描述】:
在尝试解决我在 Phonegap 上遇到 iOS 问题的this question 时,我意识到我需要加载两个不同的cordova.js 文件,具体取决于它是 iOS 应用程序还是 Android 应用程序。一种解决方案是测试服务器端并链接到不同的cordova.js 文件,但我试图弄清楚是否可以将特定于平台的cordova.js 文件与每个应用程序捆绑在一起。
有没有可能在 Android 上做这样的事情:
super.loadUrl("file:///android_asset/phonegap-2.5.0.js, 15000);
在 iOS 中有类似的东西吗?
【问题讨论】:
标签:
javascript
android
ios
cordova
webview
【解决方案1】:
您可能应该只点击不同的端点,或者为每个平台使用不同的参数并让服务器处理它。
也就是说你可以这样做:
- (void)downloadHTMLAndUpdateCordova
{
NSURL *url = [NSURL URLWithString:@"http://www.someapi.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSHTTPURLResponse* response = nil;
NSError* error = nil;
NSData *htmlData = [NSURLConnection sendSynchronousRequest:self.request returningResponse:&response error:&error];
if (error == nil) {
NSString *htmlString = [NSString stringWithUTF8String:[htmlData bytes]];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"someCordovaFile.js" withString:@"someCordovaFileiOS.js"];
[self.webView loadHTMLString:htmlString baseURL:@"http://www.someapi.com"];
}
}