【问题标题】:Possible to launch index.html from iOS phonegap plugin可以从 iOS phonegap 插件启动 index.html
【发布时间】:2014-06-23 07:41:03
【问题描述】:

首先让我描述一下我的场景:我有一个 phonegap 应用程序,它只是一个简单的视图,带有用于配置远程服务器地址和启动器 btn 的输入文本,通过单击 btn,它启动配置的托管应用程序并显示到本机网络视图。

但是,如果用户想要重新配置远程应用程序地址,我需要能够返回基于本地的配置视图,因此我在托管应用程序中添加了一个 btn。由于 webviews(android 和 ios)对本地资源的访问限制,我不能简单地通过 JavaScript 重新加载 file:///blarblar/index.html。

为了解决这个问题,我尝试通过 android 中的本机插件进行实际重新加载,如下所示:

 @Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    final CallbackContext ctxt = callbackContext;

    if (action.equals(ACTION_RELOAD)) {
        final Mobility act = (Mobility)cordova.getActivity();
        act.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                act.reloadMainView(); //--> this is calling super.loadUrl(Config.getStartUrl()); in MainActivity class
                ctxt.success("reloaded");
            }
        });

        return true;
    }

    callbackContext.error("Invalid action" + action + args);
    return false;
}

这在 android 中运行良好。然后我尝试为iOS开发一个相同的版本,不幸的是我在iOS原生开发方面非常有限,通过在Web中搜索大量资源,得出以下代码

#import "UiWrapper.h"
#import "../Classes/AppDelegate.h"
#import <Cordova/CDV.h>

@implementation UiWrapper

- (void) reload:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;

    AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
    CDVViewController* topview = appDelegate.viewController;

    NSLog(@"test");
    [topview.webView stringByEvaluatingJavaScriptFromString:@"location='file:///var/mobile/Applications/A70D02B2-ED48-4918-9CF5-DA3953111636/SM%20Mobile%20Client.app/www/index.html'"];


    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"reloaded"];

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end

它实际上不起作用,我认为原因是代码正在回调 JavaScript 以进行实际重新加载,这不会解决访问本地资源的限制。我需要的是直接在插件中加载 index.html。我不知道如何在 iOS 中做到这一点

所以我的问题是:是否可以从 iOS 插件加载 index.html 以及如何做到这一点?

【问题讨论】:

  • 好的,想办法在纯objective-c中重新加载index.html

标签: ios cordova plugins


【解决方案1】:

好了,终于在objective-c中找到如何将UIWebView重置为本地index.html

AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
CDVViewController* topview = appDelegate.viewController;

NSString* path = [[NSBundle mainBundle] pathForResource:@"www/index" ofType:@"html"];
NSURL* url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL: url];

[topview.webView loadRequest:request];

【讨论】:

  • 你做了插件吗,@RogerJin?
  • 是的,你可以试试github.com/jinyangzhen/uiwrapper。这是半年前做的。我不太确定它是否适用于最新的科尔多瓦版本。但它应该很容易做到。 :-)
  • 谢谢@RogerJib。你的插件不适合我。原因是它试图找到 Mobility。 final Mobility act = (Mobility)cordova.getActivity(); 我该如何解决?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
相关资源
最近更新 更多