IOS平台:PhoneGap添加新类(javaScript与webView交互)----功能扩展

开发环境:xcode4.2和SDK5;

PhoneGap版本:PhoneGap-1.3.0;PhoneGap-1.3.0.js

1.首先打开xcode建立基于PhoneGap的项目(名称Demo),之后将www目录添加到工程。完成之后在模拟器中测试是否成功,成功继续;

2.在xcode界面左侧列表,删除PhoneGapLib.framework,之后选中项目,点击鼠标邮件选中"Add Files to "Demo""添加新文件到phoneGap项目中。

添加文件时注意选中以下选项:



3.新文件位置:Decuments—PhoneGapLib-Classes,将Classes中的文件全部选中,添加到项目。之后需要将向你会看到已经加入项目的文件列表,需要注意的是:有新加入的文件中有一个文件夹JSON(其中有两个文件:JSONKit.h  JSONKit.m),选中两个文件把他拖到外面,并且保证与项目JSON文件夹平行。

4.现在运行程序,将与步骤1一样,成功。

5.向项目添加新的类:



但是新的类基于PGPlugin类创建,类名为Config。

完成之后你会看见两个文件Config.h,Config.m.

6.Config.h 的内容

//  Config.h

//  Demo

//  Created by macos on 12-1-6.

//  Copyright 2012 __MyCompanyName__. All rights reserved.

#import <Foundation/Foundation.h>

#import "PGPlugin.h"

#import <stdio.h>

#import <stdlib.h>

@interface PGConfig : PGPlugin {

    NSString *filePath;

    NSMutableDictionary *dictionary;

    NSMutableArray *array; 

    NSDictionary *server_ip;

    NSDictionary *server_port;

    NSDictionary *font_size_zoom;

    NSDictionary *connect_protocol;

    NSDictionary *welcome_page;

    NSDictionary *index_page;

    NSDictionary *cache_time;

}

@property (nonatomic, retain) NSDictionary *server_ip;

@property (nonatomic, retain) NSDictionary *server_port;

@property (nonatomic, retain) NSDictionary *font_size_zoom;

@property (nonatomic, retain) NSDictionary *connect_protocol;

@property (nonatomic, retain) NSDictionary *welcome_page;

@property (nonatomic, retain) NSDictionary *index_page;

@property (nonatomic, retain) NSDictionary *cache_time;

@property (nonatomic, retain) NSString *filePath;

@property (nonatomic, retain) NSMutableDictionary *dictionary;

@property (nonatomic, retain) NSMutableArray *array;

- (void)set:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;

- (void)get:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options;

@end

6.Config.m 的内容

#import "Config.h"

@implementation PGConfig

@synthesize server_ip;

@synthesize server_port;

@synthesize font_size_zoom;

@synthesize connect_protocol;

@synthesize welcome_page;

@synthesize index_page;

@synthesize cache_time;

@synthesize filePath;

@synthesize dictionary;

@synthesize array;


- (id)init {

    self = [super init];

    if (self) {

    }

    return self;

}



- (NSString *)get_filename:(NSString *)string {

    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

            stringByAppendingPathComponent:string];

}



- (void)flush:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {

    if (self.server_ip != nil && self.server_port != nil && self.font_size_zoom != nil && self.connect_protocol != nil 

        && self.welcome_page != nil && self.index_page != nil && self.cache_time != nil) {

        NSArray *myArray = [NSArray arrayWithObjects:self.server_ip, self.server_port, self.font_size_zoom, self.connect_protocol,

                            self.welcome_page, self.index_page, self.cache_time, nil];

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

        

        self.filePath = [self get_filename:@"Config.plist"];

        [myArray writeToFile:self.filePath  atomically:YES];

    }

}



- (void)set:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {

    NSString *key = [arguments objectAtIndex:1];

    NSString *value = [arguments objectAtIndex:2];

      

    if ([key isEqualToString:@"server_ip"]) {

        self.server_ip = [NSDictionary dictionaryWithObject:value forKey:key];

    } else if ([key isEqualToString:@"server_port"]){

        self.server_port = [NSDictionary dictionaryWithObject:value forKey:key];

    } else if ([key isEqualToString:@"font_size_zoom"]) {

        self.font_size_zoom = [NSDictionary dictionaryWithObject:value forKey:key];

    } else if ([key isEqualToString:@"connect_protocol"]) {

        self.connect_protocol = [NSDictionary dictionaryWithObject:value forKey:key];

    } else if ([key isEqualToString:@"welcome_page"]) {

        self.welcome_page = [NSDictionary dictionaryWithObject:value forKey:key];

    } else if ([key isEqualToString:@"index_page"]) {

        self.index_page = [NSDictionary dictionaryWithObject:value forKey:key];

    } if ([key isEqualToString:@"cache_time"]) {

        self.cache_time = [NSDictionary dictionaryWithObject:value forKey:key];

    } 

}



- (void)get:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {

    NSMutableArray *saveDataArray = [[NSMutableArray alloc] init];

    if([[NSFileManager defaultManager] fileExistsAtPath:self.filePath]) {

        saveDataArray = [NSMutableArray arrayWithContentsOfFile:self.filePath];  

    } else {

        saveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"]];

    }

    

    NSString *string = [[NSString alloc] init];

    NSString *key = [arguments objectAtIndex:1];

    

    for (NSDictionary *dic in saveDataArray) {

        NSString *getKey = [[dic allKeys] objectAtIndex:0];

        if ([getKey isEqualToString:key]) {

            string = [dic objectForKey:key];

            NSLog(@"key:%@ string:%@", key, string);

        }

    }

    

//    NSString *str = [NSString stringWithFormat:@"document.getElementById('%@').value='%s'", key, [string cStringUsingEncoding:NSASCIIStringEncoding]];    

//    [self.webView stringByEvaluatingJavaScriptFromString:str];

    NSString *callbackId = [arguments objectAtIndex:0];

    PluginResult *result = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString:string];

    NSString *jsString = [result toSuccessCallbackString:callbackId];

    NSLog(@"JSSTRING:%@", jsString);

    [self writeJavascript:jsString];

//    [self.webView stringByEvaluatingJavaScriptFromString:jsString];

}



- (void) dealloc {

    [super dealloc];

    

    self.server_ip = nil;

    self.server_port = nil;

    self.font_size_zoom = nil;

    self.connect_protocol = nil;

    self.welcome_page = nil;

    self.index_page = nil;

    self.cache_time = nil;

    self.filePath = nil;

    self.dictionary = nil;

    self.array = nil;

}

@end

7.在项目中PhoneGap.plist文件Plujins添加一个键值对:
KEY为com.phonegap.config
VALUE为PGConfig
8.修改index.html页面,修改后内容为:

<!DOCTYPE html>

<html>

    <head>

        <title>Capture Photo</title>

        <meta http-equiv="Content-Type" contect="text";charset=gb_2312>

        <script type="text/javascript" charset="utf-8" src="Mcube.js"></script>

        <script type="text/javascript" charset="utf-8">  

            document.addEventListener("deviceready",onDeviceReady,false);        

         

    function onDeviceReady() { } 

            

            function SetConfig(){

                navigator.config.set("server_ip", document.getElementById("server_ip").value);

                navigator.config.set("server_port", document.getElementById("server_port").value);

                navigator.config.set("font_size_zoom", document.getElementById("font_size_zoom").value);

                navigator.config.set("connect_protocol", document.getElementById("connect_protocol").value);

                navigator.config.set("welcome_page", document.getElementById("welcome_page").value);

                navigator.config.set("index_page", document.getElementById("index_page").value);

                navigator.config.set("cache_time", document.getElementById("cache_time").value);

                navigator.config.flush();

            }

            

            function onGetIpSuccess(messge) { document.getElementById("server_ip").value = messge; }

            function onGetPoSuccess(messge) { document.getElementById("server_port").value = messge; }

            function onGetSiSuccess(messge) { document.getElementById("font_size_zoom").value = messge; }

            function onGetPrSuccess(messge) { document.getElementById("connect_protocol").value = messge; }

            function onGetWeSuccess(messge) { document.getElementById("welcome_page").value = messge; }

            function onGetInSuccess(messge) { document.getElementById("index_page").value = messge; }

            function onGetCaSuccess(messge) { document.getElementById("cache_time").value = messge; }

            

            function GetConfig(){ 

                navigator.config.get(onGetIpSuccess, "server_ip");

                navigator.config.get(onGetPoSuccess, "server_port");

                navigator.config.get(onGetSiSuccess, "font_size_zoom");

                navigator.config.get(onGetPrSuccess, "connect_protocol");

                navigator.config.get(onGetWeSuccess, "welcome_page");

                navigator.config.get(onGetInSuccess, "index_page");

                navigator.config.get(onGetCaSuccess, "cache_time");

            }      

        </script>

    </head>

    <body>

        <form>

            <table>

                <tr><td>server_ip:</td><td><input type="text" id="server_ip" ></td></tr>

                <tr><td>server_port:</td><td><input type="text" id="server_port" ></td></tr>

                <tr><td>font_size_zoom:</td><td><input type="text" id="font_size_zoom"></td></tr>

                <tr><td>connect_protocol:</td><td><input type="text" id="connect_protocol" ></td></tr> 

                <tr><td>welcome_page:</td><td><input type="text" id="welcome_page" ></td></tr>

                <tr><td>index_page:</td><td><input type="text" id="index_page" ></td></tr>    

                <tr><td>cache_time:</td><td><input type="text" id="cache_time" ></td></tr>

                <tr><td><input type="button" value="SETCONFIG" name="Set" onclick="SetConfig()"/></td></tr>

                <tr><td><input type="button" value="GETCONFIG" name="Get" onclick="GetConfig()"/></td></tr>

            </table>

        </form>

    </body>

</html>

9.运行可以设置,得到ip, port, size等值

你可能感兴趣的:(JavaScript,ios,server,function,平台,PhoneGap)