应用加载时将AppDelegete变量传递给Index.js

时间:2019-05-23 08:40:55

标签: ios objective-c cordova phonegap

我已经在iOS Cordova Project上实现了URL方案。

我可以在AppDelegete.m中使用此功能进行网址查询:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url 
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {

    NSLog(@"url recieved: %@", url);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);
    NSLog(@"url path: %@", [url path]);

    NSLog(@"host: %@", [url host]);
    NSLog(@"path components: %@", [url pathComponents]);
    NSLog(@"parameterString: %@", [url parameterString]);
    NSLog(@"fragment: %@", [url fragment]);

    sdQuery     = [url query];

    NSLog(@"sdQuery: %@", sdQuery);

    return YES;
}

我在AppDelegete.h中定义了sdQuery变量:

@public
NSObject *sdQuery;

我想将sdQuery变量传递给index.js,并在App加载时使用该变量。

我可以在index.js中调用AppDelegete.m函数:

if (sdQuery){
    NSLog(@"The file contained yept: %@",sdQuery);
    NSString * jsString = [NSString stringWithFormat:@"sendUrlSchemeQuery(%@);",sdQuery];
    [self.viewController.webViewEngine evaluateJavaScript:jsString completionHandler:nil];
}

index.js中,我可以获得变量:

function sendUrlSchemeQuery(sdQuery){
    console.log('heyyo ' + sdQuery)
}

如果我在sdQuery中使用onDeviceReady变量,则变量为undefined

index.js:

var app = {
    // Application Constructor
initialize: function () {
    this.bindEvents();
},
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function () {
    document.addEventListener('push-notification', function(event) {
                              var notification = event.notification;
                              });
    document.addEventListener('resume', this.onResume, false);
    document.addEventListener("offline", this.onOffline, false);
    document.addEventListener("deviceready", this.onDeviceReady, false);
    document.addEventListener("online", this.onOnline, false);
},
load: function () {

},
onOnline: function () {
    if (!online) {
        //console.log("connection ready!");
        var l_dnLoader = document.getElementById('content');
        l_strURL = l_defaultURL;
        l_dnLoader.src = l_strURL;
        online = true;
    }
},
onOffline: function () {
    online = false;
    //console.log("lost connection");
    l_strURL = "offline.html";
    var l_dnLoader = document.getElementById('content');
    l_dnLoader.src = l_strURL;
    count = 0;
},
onResume: function () {
    if (g_appWindow) {
        //g_appWindow.currectLocationFocus = false;
        g_appWindow.f_onResume();
    }
},
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function () {
    initPushwoosh();
    console.log('testVar: ' + testVar);
    var l_dnLoader = document.getElementById('content');
    var l_strURL = l_defaultURL; //

    // when the document has loaded, delete the indicator and show the iframe
    l_dnLoader.onload = function () {
        l_dnLoader.style.display = '';
        //console.log('Developers beware: the application runs in iframe. You can access its window object through g_appWindow');
        g_appWindow = l_dnLoader.contentWindow;
        g_appWindow.pwDeviceToken = pwDeviceToken;
        //Cookies.set('DEVICEID', pwDeviceToken, { expires: 7, path: '/ords', domain: 'apps.simplydeliver.com' });

        g_appWindow.openInappBrowser = function (str, callback) {
            var inAppBrowserRef = cordova.InAppBrowser.open(str, '_blank', 'location=no,toolbar=no,zoom=no');

            inAppBrowserRef.addEventListener('loadstart', function (event) {
                                             g_appWindow.f_registerDevice(pwDeviceToken);
                                             });

            inAppBrowserRef.addEventListener('exit', function (event) {
                                             //ios status bar show wrongly after quiting the inappbrowser
                                             StatusBar.hide();
                                             StatusBar.show();
                                             StatusBar.overlaysWebView(false);
                                             });

        };
        g_appWindow.navigator.globalization = navigator.globalization;
        g_appWindow.platformId  = cordova.platformId;
        g_appWindow.geolocation = navigator.geolocation;
        g_appWindow.hardwareId  = hardwareId;
        g_appWindow.alert = function (txt, title) {
            navigator.notification.alert(txt, null, title, "Close");
        }
        g_appWindow.bgGeo = window.BackgroundGeolocation;
        g_appWindow.camera = navigator.camera;

        g_appWindow.Camera = Camera;
        g_appWindow.v_latglobal = window.v_latglobal;
        g_appWindow.v_longlobal = window.v_longlobal;

        // check simcard info to find mobile number
        g_appWindow.simCard = window.plugins.sim;
        //window.plugins.sim.hasReadPermission(successCallbackSimHasRead, errorCallbackSimHasRead);

    };

},
receivedEvent: function () {}
};

在加载应用程序时,如何在sdQuery onDeviceReady中使用index.js变量?

0 个答案:

没有答案