Roger Jin
Roger Jin

Reputation: 563

Possible to launch index.html from iOS phonegap plugin

First let me describe my scenario: i have a phonegap app which is just a simple view with a input text to config remote server address and a launcher btn, by clicking on the btn, it launches the configured hosted app and display into the native webview.

However, i need the ability to go back to the local-based config view if user want to re-config the remote app address, so i add a btn inside the hosted app. I cannot simply reload file:///blarblar/index.html through JavaScript because of webviews'(both android and ios) access restriction to local resource.

In order to work out this, i have tried to do the actual reload through native plugin in android like below:

 @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;
}

this does work fine in android. Then I try to work out a same version to iOS, unfortunately i am very limited in iOS native development, by searching lots of resources in the Web, come out the below code

#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

it actually doesn't work, i think the reason is the code is calling back JavaScript for actual reload, this won't work out the restriction to access local resource. what i need is loading the index.html directly within the plugin . I cannot figure out how to do this in iOS

So my question is: is it possible to load index.html from iOS plugin and any clue how to do that?

Upvotes: 0

Views: 666

Answers (1)

Roger Jin
Roger Jin

Reputation: 563

Okay, finally find how to reset the UIWebView to the local index.html in objective-c

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];

Upvotes: 1

Related Questions