在iPhone中加载网页并检查历史记录 [英] Loading a web page in iphone and check history

查看:49
本文介绍了在iPhone中加载网页并检查历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伪代码

pageUrl = "http://www.google.com"

if(pageUrl == never Downloaded)
  Download pageUrl
else
{
   Display Saved Version
   Mean while download pageUrl
   When done display new version

}

如何在目标C中为UIWebview做类似的事情?另外,在这种情况下保存网页的最佳方法是什么?PList,SQLite吗?

How can I do something like this in objective C for a UIWebview? Also what's the best way to save web pages for this scenario? PList, SQLite?

推荐答案

Plist是解决问题的最佳方法.与SQLite数据库相比,iPhone/Objective-c可以非常快速地访问plist文件.

Plist is the best way to solve your problem. iPhone/Objective-c can access very quickly to plist file as compare to SQLite Database.

让我给您一些示例代码.

Let me give you some sample code.

请参阅-一段时间后编辑.

See - edit After some time.

  • 创建项目的步骤&连接网络视图
    • 创建新项目->基于视图的应用程序.
    • 给名字起"yourProjName"(由您决定)
    • 打开"yourProjNameViewController.xib"
    • 拖动&放下UIWebView
    • 打开"yourProjNameViewController.h"文件
    • 放置变量 IBOutlet UIWebView * wView;
    • 在界面构建器中连接

    将属性列表文件添加到项目中的步骤

    Steps for adding Property list file to your project

    • 在项目树下扩展资源组
    • 右键单击资源"->添加->新文件
    • 选择模板类别-osx->资源
    • 选择属性列表文件
    • 提供文件名"LoadedURL.plist"
    • 将根类型"更改为数组"
    • 保存"LoadedURL.plist"文件

    现在将以下代码放置到"yourProjNameViewController.m"文件中.

    Now place following code to "yourProjNameViewController.m" file.

    #import "yourProjNameViewController.h"
    #define documentsDirectory_Statement NSString *documentsDirectory; \
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); \
    documentsDirectory = [paths objectAtIndex:0];
    
    
    @implementation WebViewLoadViewController
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // your url to load
        NSString *strToLoad=@"http://www.mail.yahoo.com";
    
    
        // file management code
        // copy file to documents directory
        documentsDirectory_Statement;
        NSFileManager *fm=[NSFileManager defaultManager];
        if(![fm fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]]){
            [fm copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"LoadedURL" ofType:@"plist"] 
                        toPath:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]
                         error:nil];
        }
    
        // array from doc-dir file
        NSMutableArray *ar=[NSMutableArray arrayWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]];
    
        // check weather file has url data or not.
        BOOL fileLocallyAvailable=NO;
        NSString *strLocalFileName=nil;
        NSUInteger indexOfObject=0;
        if([ar count]>0){
            for (NSDictionary *d in ar) {
                if([[d valueForKey:@"URL"] isEqualToString:strToLoad]){
                    fileLocallyAvailable=YES;
                    strLocalFileName=[d valueForKey:@"FileName"];
                    break;
                }
                indexOfObject++;
            }
        }
        if(fileLocallyAvailable){
            NSDictionary *d=[ar objectAtIndex:indexOfObject];
            strLocalFileName=[d valueForKey:@"FileName"];
        } else {
            NSMutableDictionary *d=[NSMutableDictionary dictionary];
            [d setValue:strToLoad forKey:@"URL"];
    
            NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strToLoad]];
    
            [str writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%02i.htm",[ar count]]] 
                  atomically:YES
                    encoding:NSUTF8StringEncoding error:nil];
    
            strLocalFileName=[NSString stringWithFormat:@"%02i.htm",[ar count]];
            [d setValue:[NSString stringWithFormat:@"%02i.htm",[ar count]] forKey:@"FileName"];
            [ar addObject:d];
    
            [ar writeToFile:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]
                 atomically:YES];
        }
        NSURL *u=[[NSURL alloc] initFileURLWithPath:[documentsDirectory stringByAppendingPathComponent:strLocalFileName]];
        NSURLRequest *re=[NSURLRequest requestWithURL:u];
        [wView loadRequest:re];
        [u release];
    }
    

    `

    这篇关于在iPhone中加载网页并检查历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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