你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios23- 文件下載(同步和異步)

ios23- 文件下載(同步和異步)

編輯:IOS開發綜合

1.第一步:創建一個單例視圖


#import <UIKit/UIKit.h>

 

@interface ios23_downViewController : UIViewController<NSURLConnectionDelegate,NSURLConnectionDataDelegate>{

    NSMutableData *connectionData;

   
}

-(IBAction)tongbu;

-(IBAction)yibu;

@property (nonatomic,retain) NSMutableData *connectionData;

@end

2
//

//  ios23_downViewController.m

//  ios23-down

//

//  Created by  on 13-6-17.

//  Copyright 2013年 __MyCompanyName__. All rights reserved.

//

 

#import "ios23_downViewController.h"

 

@implementation ios23_downViewController

@synthesize connectionData;

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}

 

#pragma mark - View lifecycle

-(IBAction)tongbu{

    NSLog(@"同步");

    NSError *err;

    //定義url

    NSString *url=@"http://172.22.65.38/new/1.doc";

    //構建NSURL

    NSURL *fileUrl=[NSURL URLWithString:url];

    //構建nsurlrequest

    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:fileUrl];

    //建立連接

    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

    if (data.length>0) {

        NSString *savePath=[[NSHomeDirectory()stringByAppendingPathComponent:@"documents"]stringByAppendingPathComponent:@"test.zip"];

        //當數據寫入的時候

        if ([data writeToFile:savePath atomically:YES]) {

            NSLog(@"保存成功");

        }else{

            NSLog(@"保存失敗");

        }

    }

               
          
}

-(IBAction)yibu{

    NSLog(@"異步");

    NSError *err;

    //定義url

    NSString *url=@"http://172.22.65.38/new/1.doc";

    //構建NSURL

    NSURL *fileUrl=[NSURL URLWithString:url];

    //構建nsurlrequest

    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:fileUrl];

    //建立連接

    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

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

    //初始化connectionData;

    connectionData=[[NSMutableData alloc]init ];

   
}

//接受數據

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

    //獲取服務器傳遞的數據

    [connectionData appendData:data];

}

//接收數據成功

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

    if (connectionData.length>0) {

        NSString *savePath=[[NSHomeDirectory()stringByAppendingPathComponent:@"documents"]stringByAppendingPathComponent:@"test.zip"];

        //當數據寫入的時候

        if ([connectionData writeToFile:savePath atomically:YES]) {

            NSLog(@"保存成功");

        }else{

            NSLog(@"保存失敗");

        }

    }}

//接收數據失敗

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

 

- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

 

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

}

 

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

}

 

- (void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

}

 

- (void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

 

@end

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved