Phonegap 3.0 iOS7 ApplicationPreferences 插件

Posted

技术标签:

【中文标题】Phonegap 3.0 iOS7 ApplicationPreferences 插件【英文标题】:Phonegap 3.0 iOS7 ApplicationPreferences Plugin 【发布时间】:2013-10-05 01:53:44 【问题描述】:

有没有人能够让 ApplicationPreferences 插件 (https://github.com/phonegap/phonegap-plugins/tree/master/ios/ApplicationPreferences) 与 phonegap 的新插件 api 一起使用?我以旧方式安装插件并不断收到以下错误:

ERROR: Method 'getSetting:' not defined in Plugin 'applicationPreferences'
    我检查以确保包含 .js 文件 我检查以确保包含 .h 和 .m 文件

【问题讨论】:

【参考方案1】:

我也想在 Phonegap 3/iOS7 中使用这个插件,所以我更新了它。您可以下载我的 Phonegap 3 测试项目,其中包含更新的插件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

使用示例

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

【讨论】:

【参考方案2】:

我能够让它工作。 (谢谢,Dpa99c!)如果您遇到困难,请记住您仍然需要像“旧”(Cordova/PhoneGap 2.*)版本一样进行设置。也就是说,您需要将 Settings.bundle 添加到您的项目并进行相应的编辑。并且,您需要添加:

<feature name="applicationPreferences">
    <param name="ios-package" value="applicationPreferences" />
</feature>

到您的 config.xml。

这是除了将上面显示的文件放在正确的位置之外。

【讨论】:

以上是关于Phonegap 3.0 iOS7 ApplicationPreferences 插件的主要内容,如果未能解决你的问题,请参考以下文章

iOS 7 状态栏重叠 UI - Cordova 3.0 需要解决方案

Phonegap / Cordova:jQuery 发布到外部服务器不起作用

phonegap/backbonejs 应用程序的 ios7 样式

PhoneGap/Cordova + iOS7 + XCode 5

iOS7 的错误显示带有 phonegap 和 Intel App Framework 的软键盘

从PhoneGap Camera API返回时如何隐藏iOS7状态栏?