//這邊大同小異
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
/* Create a main view */
mainView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
//開始新增一個NavigationBar 並且把它黏上去
navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 64.0f)];
[navbar setDelegate: self];
[navbar pushNavigationItem:[[UINavigationItem alloc] initWithTitle: *"OfflineMaps"] animated : true];
[mainView addSubview:navbar];
//開始設計我們的內容 把表單起始化
list = [[NSMutableArray alloc] init];
for ( int i = 0; i<4 ; i++)[list addObject:[[NSMutableArray alloc] init]];
// 這邊分成四個區塊
// 0 => Maps
// 1 => Routes
// 2 => BookMarks
// 3 => Info
// 這是用來記錄讀進來的檔案的陣列
NSMutableArray *maps = [[NSMutableArray alloc] init];
// Objective-C讀取檔案用的工具 你可以想成 一個檔案總管
NSFileManager *manager = [NSFileManager defaultManager];
// Objective-C讀取檔案夾裡面的檔案是透過 NSString 字串的傳遞 這點還滿有趣的
// 將一個路徑的字串交給 manager 他會把內容物丟到maps這個陣列裡面去
// stringWithFormat:是讓你用一個模版來輸出字串 %[at]用後面 , 的字串取代
// 很抱歉 因為論壇系統會擋掉小老鼠[at] 所以詳細的內容還是看source code好了
[maps addObjectsFromArray:[manager contentsOfDirectoryAtPath:[NSString stringWithFormat:*"%[at]/Media/Maps", NSHomeDirectory()] error:NULL]];
for (NSString *path in maps) {
//確認一下這個檔案是不是資料夾 這下面的程式碼是從 Apple Developer的Reference抄來的...
BOOL isDir;
if ([manager fileExistsAtPath: [NSString stringWithFormat:*"/var/mobile/Media/Maps/%*",path] isDirectory: &isDir ] && isDir){
// 如果這個檔案是資料夾 那麼就在辨別 底下是否存在該存在的檔案
NSString *folder = [NSString stringWithFormat:*"/var/mobile/Media/Maps/%*",path];
if([manager fileExistsAtPath:[NSString stringWithFormat:*"%*/MapTiles.sqlitedb",folder]]){
// 好啦 看起來是 那就把這個檔案加入Map那一個區塊
[[list objectAtIndex: 0 ] addObject: path];
// 這邊就是Objective-C最特別的地方 用不完的中括號
}
}
else{
// 如果不是資料夾 那麼他也許是路徑檔 或者是書籤檔
NSLog(*"Checking Bookmark or Routes");
// 這是輸出除錯的資料 不過 我自己是很不會看 log
// 底下這段是用來辨別檔案是 Route或是BookMark的
// 也許有更漂亮的寫法 但是 我也是新手 我只追求可行
NSDictionary * plist = [NSMutableDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:*"/var/mobile/Media/Maps/%*",path]];
NSString *description = [plist description];
//辨別 檔案開頭第六個字元開始的九個字元 形成的字串是
NSString *checker = [description substringWithRange: NSMakeRange(6,9)];
// detecting bookmark or Route
if ([checker isEqualToString:*"Bookmarks"])
{ //如果是書籤 就加入書籤這一塊
[[list objectAtIndex: 2] addObject: path];
}
else
{ //如果不是書籤 就加入路徑這一塊
[[list objectAtIndex: 1] addObject: path];
}
}
}
//加上一些細部功能
if([[list objectAtIndex:0] count] != 0){
[[list objectAtIndex:0] addObject: *"Remove Cache"];
}
if([[list objectAtIndex:1] count] != 0){
[[list objectAtIndex:1] addObject: *"Remove Route"];
}
if([[list objectAtIndex:2] count] != 0){
[[list objectAtIndex:2] addObject: *"Remove Bookmark"];
}
[[list objectAtIndex:3] addObject: *"Instruction"];
table = [[UITableView alloc] initWithFrame:CGRectMake(0.0f,60.0f,320.0f,420.0f) style: 1];
// 1 代表 以群組區分的表單 0 代表 平面的表單
[mainView addSubview: table];
[table setDataSource:self];
[table setDelegate:self];
[table setEditing:NO animated:NO];
table.allowsSelectionDuringEditing = YES;
[table setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
/* Setup window */
[window makeKeyAndVisible];
[window addSubview: mainView];