Phonegap 3.0 iOS7 ApplicationPreferences插件 [英] Phonegap 3.0 iOS7 ApplicationPreferences Plugin

查看:200
本文介绍了Phonegap 3.0 iOS7 ApplicationPreferences插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能够获取ApplicationPreferences插件( https:// github) .com / phonegap / phonegap-plugins / tree / master / iOS / ApplicationPreferences )使用新的插件api for phonegap?我安装插件的旧方式,并继续得到以下错误:

Has anyone been able to get ApplicationPreferences plugin (https://github.com/phonegap/phonegap-plugins/tree/master/iOS/ApplicationPreferences) to work with the new plugin api for phonegap? I installed the plugin the old way and keep getting the following error:

ERROR: Method 'getSetting:' not defined in Plugin 'applicationPreferences'




  1. 我检查以确保包含.js文件

  2. 我检查以确保.h& .m文件


推荐答案

我想在Phonegap 3 / iOS7上使用这个插件,所以我更新了。您可以下载我的包含更新插件的Phonegap 3测试项目从这里

I wanted to use this plugin with Phonegap 3/iOS7 too, so I've updated it. You can download my Phonegap 3 test project containing the updated plugin from here.

以下是代码:

applicationPreferences.js

cordova.define("applicationPreferences", function(require, exports, module) {
    var exec = require('cordova/exec');

    var ApplicationPreferences = function() {};


    ApplicationPreferences.prototype.get = function(key, successFn, errorFn) {
        exec(successFn, errorFn, 'applicationPreferences', 'getSetting', [key]);
    }

        ApplicationPreferences.prototype.set = function(key,value, successFn, errorFn) {
            exec(successFn, errorFn, 'applicationPreferences', 'setSetting', [key,value]);
        }

    var applicationPreferences = new ApplicationPreferences();
    module.exports = applicationPreferences;
});

applicationPreferences.h

#import <Foundation/Foundation.h>

#import <Cordova/CDVPlugin.h>

@interface applicationPreferences : CDVPlugin
{

}

-   (void) getSetting:(CDVInvokedUrlCommand*)command;
-   (void) setSetting:(CDVInvokedUrlCommand*)command;
-   (NSString*) getSettingFromBundle:(NSString*)settingName;


@end

applicationPreferences.m

#import "applicationPreferences.h"


@implementation applicationPreferences



- (void)getSetting:(CDVInvokedUrlCommand*)command;
{
    NSString* jsString;


        NSString *settingsName = [command.arguments objectAtIndex:0];
        CDVPluginResult* result = nil;

        @try
        {
            //At the moment we only return strings
            //bool: true = 1, false=0
            NSString *returnVar = [[NSUserDefaults standardUserDefaults] stringForKey:settingsName];
            if(returnVar == nil)
            {
                returnVar = [self getSettingFromBundle:settingsName]; //Parsing Root.plist

                if (returnVar == nil)
                    @throw [NSException exceptionWithName:nil reason:@"Key not found" userInfo:nil];;
            }
            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:returnVar];
            jsString = [result toSuccessCallbackString:command.callbackId];
        }
        @catch (NSException * e)
        {
            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]];
            jsString = [result toErrorCallbackString:command.callbackId];
        }
        @finally
        {
            [self writeJavascript:jsString]; //Write back to JS
        }
}

- (void)setSetting:(CDVInvokedUrlCommand*)command;
{
    NSString* jsString;
    CDVPluginResult* result;

    NSString *settingsName = [command.arguments objectAtIndex:0];
    NSString *settingsValue = [command.arguments objectAtIndex:1];


    @try
    {
        [[NSUserDefaults standardUserDefaults] setValue:settingsValue forKey:settingsName];
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        jsString = [result toSuccessCallbackString:command.callbackId];

    }
    @catch (NSException * e)
    {
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT messageAsString:[e reason]];
        jsString = [result toErrorCallbackString:command.callbackId];
    }
    @finally
    {
        [self writeJavascript:jsString]; //Write back to JS
    }
}
/*
  Parsing the Root.plist for the key, because there is a bug/feature in Settings.bundle
  So if the user haven't entered the Settings for the app, the default values aren't accessible through NSUserDefaults.
*/


- (NSString*)getSettingFromBundle:(NSString*)settingsName
{
    NSString *pathStr = [[NSBundle mainBundle] bundlePath];
    NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"];
    NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"];

    NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
    NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];
    NSDictionary *prefItem;
    for (prefItem in prefSpecifierArray)
    {
        if ([[prefItem objectForKey:@"Key"] isEqualToString:settingsName])
            return [prefItem objectForKey:@"DefaultValue"];
    }
    return nil;

}
@end

/ strong>

Example usage

cordova.require("applicationPreferences").set("foo", "bar",
      function () {
        alert("Successfully set preference 'foo' with value 'bar'");
      },
      function (error) {
        alert("Failed to set preference 'foo' with value 'bar' - error:" + error);
      }
);

cordova.require("applicationPreferences").get("foo",
      function (value) {
        alert("Successful get of preference 'foo' with value '"+value+"'");
      },
      function (error) {
        alert("Failed to get value for preference 'foo' - error:" + error);
      }
);

这篇关于Phonegap 3.0 iOS7 ApplicationPreferences插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆