zoukankan      html  css  js  c++  java
  • xcode 4.6 使用NSURLConnection 获取网页内容(iOS6.1,纯手工编码,无xib,无storyboard)

    环境 iOS   6.1, xcode 4.6

    一、创建新项目

     

    1、打开 xcode,File --> New --> Project... -->Empty Application

    2、项目名称 NSURLConnectionDemo,下面所有选项全部不选,完成创建。

    二、创建视图控制器

     

    3、File-->New-->File-->Objective-C class

    4、创建UIViewController的子类,命名 TestViewController

    三、创建视图控制器实例

     

    5、在 AppDelegate.m中引入 

    #import "TestViewController.h"

    6、添加TestViewController到窗体

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

        self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];

        TestViewController *rootController = [[TestViewControlleralloc]init];

        self.window.rootViewController = rootController;

        self.window.backgroundColor = [UIColorwhiteColor];

        [self.windowmakeKeyAndVisible];

        [rootController release];

        returnYES;

    }

    7、在TestViewController.h 中添加两个变量

     

    @interface TestViewController : UIViewController{

        NSMutableData * pageData ;

        UIWebView *webView;

    }

    8、在TestViewController.m 中添加按钮和 UIWebView

     

    - (void)viewDidLoad

    {

        [superviewDidLoad];

    // Do any additional setup after loading the view.

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        [self.view addSubview:button];

        [button setTitle:@"start"forState:UIControlStateNormal];

        [button setFrame:CGRectMake(10,20, 300, 40)];

        [button addTarget:selfaction:@selector(openUrl) forControlEvents:UIControlEventTouchUpInside];

        

         webView = [[UIWebViewalloc] initWithFrame:CGRectMake(0, 50, 320, 400)];

        [webViewsetUserInteractionEnabled:NO];

        [webViewsetBackgroundColor:[UIColorclearColor]];

        [webView setOpaque:NO];

        

        

        [self.view addSubview:webView];

    }

     

    9、实现按钮点击的事件

     

    -(void)openUrl

    {

     

        NSURL *url = [NSURLURLWithString:@"http://www.baidu.com"];

        NSMutableURLRequest *request = [[NSMutableURLRequestalloc ]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheDatatimeoutInterval:60];

        pageData = [[NSMutableData alloc]init];

        [request setHTTPMethod:@"GET"];

        [request addValue:@"text/html"forHTTPHeaderField:@"Content-Type"];

        NSURLConnection *conn = [[NSURLConnectionalloc]initWithRequest:request delegate:self];

        [request release];

        [conn release];

    }

    10、实现 NSURLConnection 代理的两个方法

     

    - (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data {

    [pageData appendData:data];

    }

     

    - (void)connectionDidFinishLoading:(NSURLConnection *)aConn {

        NSString *results = [[NSStringalloc] initWithData:pageDataencoding:NSUTF8StringEncoding];

    [webViewloadHTMLString:results baseURL:nil];

        

    }


    <END>

  • 相关阅读:
    Node.js安装及环境配置之Windows篇
    盘点.NET JIT在Release下由循环体优化所产生的不确定性Bug
    开源!一款功能强大的高性能二进制序列化器Bssom.Net
    开源 , KoobooJson一款高性能且轻量的JSON框架
    通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
    .Net Web开发技术栈
    web安全:通俗易懂,以实例讲述破解网站的原理及如何进行防护!如何让网站变得更安全。
    .Net高级进阶,教你如何构建企业模型数据拦截层,动态控制字段验证
    .Net 如何模拟会话级别的信号量,对http接口调用频率进行限制(有demo)
    .Net高级进阶,在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码?
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3086140.html
Copyright © 2011-2022 走看看