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>

  • 相关阅读:
    安卓界面基本组件------计时器
    安卓界面组件----时间日期拾取器
    安卓界面组件----列表视图
    安卓组件------列表选择框
    Redis 开启远程访问
    收集的一个关于大批量插入数据的代码
    Server.MapPath和Request.PhysicalApplicationPath的异同
    C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编
    cocos2d-x3.2在xcode6.1下的 环境搭建
    STL源码剖析(适配器)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3086140.html
Copyright © 2011-2022 走看看