IOS学习十一:ios开发之网络编程

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 13:23 点击:
这边我就以最简单的例子来引入几个常用的API中的类。
[cpp] 
// 
//  NLViewController.m 
//  NetWorkTest 
// 
//  Created by Nono on 12-5-16. 
//  Copyright (c) 2012年 NonoWithLilith. All rights reserved. 
// 
#import "NLViewController.h" 
 
@interface NLViewController () 
 
@end 
 
@implementation NLViewController 
@synthesize label = _label; 
@synthesize data = _data; 
@synthesize connection = _connection; 
- (void)dealloc{ 
    [self.label release]; 
    [self.data release]; 
    [super dealloc]; 

- (void)viewDidLoad 

    [super viewDidLoad]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 400)]; 
    self.label = label; 
    label.textAlignment = UITextAlignmentCenter; 
    [label setNumberOfLines:0]; 
    label.lineBreakMode = UILineBreakModeWordWrap;  
    self.label.text = @"正在在请求数据"; 
    [self.view addSubview:label]; 
    [label release]; 
    //step 1:请求地址 
    NSString *urlString = @"www.2cto.com"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    //step 2:实例化一个request 
    NSURLRequest *requrst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; 
    //step 3:创建链接 
    self.connection = [[NSURLConnection alloc] initWithRequest:requrst delegate:self]; 
    if ( self.connection) { 
        NSLog(@"链接成功"); 
    }else { 
        NSLog(@"链接失败"); 
    } 
     
    [url release]; 
    [urlString release]; 
    [requrst release]; 
    // Do any additional setup after loading the view, typically from a nib. 

 
- (void)viewDidUnload 

    self.label = nil; 
    self.data = nil; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
#pragma mark- 
#pragma NSUrlConnectionDelegate methods 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

    //接受一个服务端回话,再次一般初始化接受数据的对象 
    
    NSLog(@"返回数据类型:%@",[response textEncodingName]);  
    NSMutableData *d = [[NSMutableData alloc] init]; 
     self.data = d; 
    [d release]; 

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

    //接受返回数据,这个方法可能会被调用多次,因此将多次返回数据加起来 
 &n

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Lexical or Preprocessor Issue 'xxx.h
      • ios学习笔记(二)xcode 4.3.2下实现基本交互
      • ios版本的helloworld
      • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
      • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
      • IOS类似iphone通讯录TableView的完整demo【附源码】
      • UITableView一些方法
      • [iPhone中级]iPhone团购信息客户端的开发 (二)
      • 如何为Iphone应用创建启动界面
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1