ITEEDU

3.iPhone书架中加载本地网页

(1)利用page= [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"iphone/baobaoweiyang"];函数来进行逐个判断和读取。

(2)将各个书籍网页的首页地址写入.plist文件中,然后读取.plist文件,并将读取处理封装成一个DM类。

本文上述采用的是方法(1),下边来讲述方法(2):

DM.h

#import <Foundation/Foundation.h>

@interface DM : NSObject
{
        NSString *csurl;
	NSArray *tbc;
	NSInteger netref;
    
}

@property (nonatomic, retain) NSString *csurl;
@property (nonatomic, retain) NSArray *tbc;


+ (DM *)getInstance;

- (NSString *)getTabBarViewURL:(NSInteger)index;
- (NSInteger)getTabBarItemNumber;

@end

DM.m:

//  bookself
//
//  Created by 丽英 潘 on 11-10-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DM.h"

static DM *dmInstance;

@implementation DM

@synthesize csurl;
@synthesize tbc;


+ (DM *)getInstance{
	if(dmInstance == nil){
		dmInstance = [[DM alloc] init];
	}
	return dmInstance;
}

- (id)init{
	if(self = [super init]){
	
	
		NSString *confPath  = [[NSBundle mainBundle] pathForResource:@"pagelist" ofType:@"plist"];

		NSData *dt = [[NSData alloc] initWithContentsOfFile:confPath];
		NSString *err = nil;
		NSPropertyListFormat *fmt = nil;
		NSDictionary *dict = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:dt mutabilityOption:NSPropertyListImmutable format:fmt errorDescription:&err];
		[dt release];
		if(err != nil){
			[err release];
		}else{
			csurl = (NSString *)[dict objectForKey:@"book"];
			self.tbc = (NSArray *)[dict objectForKey:@"list"];
		}
	}
	return self;
}



- (NSString *)getTabBarViewURL:(NSInteger)index{
	NSString *ret = nil;
	if(index > -1){
		NSInteger tsize = [self.tbc count];
		if(index < tsize){
			NSDictionary *dict = (NSDictionary *)[tbc objectAtIndex:index];
			ret = (NSString *)[dict objectForKey:@"book"];
            NSLog(@"ret1=%@",ret);
			if(ret != nil){
				if ([ret hasPrefix:@"locbundle://"]) {
					NSString *str = [ret substringFromIndex:[@"locbundle://" length]];
					//NSLog(@"sub url is %@",str);
					NSBundle *mainBundle = [NSBundle mainBundle];
					NSArray *pcs = [str pathComponents];
					if(pcs != nil){
						// NSInteger len = [pcs count];
						NSString *fp = [str lastPathComponent];
						NSRange range = [fp rangeOfString:@"."];
						NSString *fn = nil;
						NSString *fe = [str pathExtension];
						if(range.location != NSNotFound) {
							fn = [fp substringToIndex:range.location];
						}
						range = [str rangeOfString:fn];
						NSString *p = nil;
						if(range.location != NSNotFound){
							p = [str substringToIndex:range.location];
						}
						if(p != nil && fn != nil && fe != nil){
							NSInteger pl = [p length];
							if(pl > 1){
								if([p hasSuffix:@"/"]){
									p = [p substringToIndex:(pl - 1)];
								}
							}
                            NSString *page = [[NSBundle mainBundle] pathForResource:fn ofType:fe inDirectory:p];
                            NSLog(@"page=%@",page);
							ret = [mainBundle pathForResource:fn ofType:fe inDirectory:p];
                            //NSURL *url = [NSURL encryptedFileURLWithPath:indexPath];
                            NSLog(@"fn=%@",fn);
                            NSLog(@"fe=%@",fe);
                            NSLog(@"p=%@",p);

                            NSLog(@"ret2=%@",ret);
						}else if(fn != nil && fe != nil){
							ret = [mainBundle pathForResource:fn ofType:fe];
                            NSLog(@"ret3=%@",ret);
						}
						if (ret != nil) {
							NSURL *u = [NSURL fileURLWithPath:ret];
							ret = [u absoluteString];
						}
					}
				}
			}
		}
	}
	return ret;
}

- (NSInteger)getTabBarItemNumber{
	return [self.tbc count];
}

- (void)dealloc{
	[csurl release];
	[tbc release];
	[super dealloc];
}

@end