IOS上传文件开发

简介:

IOS上传文件开发



    在移动应用开发  文件形式上传是不可缺少的,近期把IOS这块文件上传文件代码简单的整理一下。假设大家有须要安卓这边的代码,本人也能够分享给大家!

QQ群:74432915  欢迎大家一起探讨


首先本demo採用网上开源框架 AFNetworking  源代码:http://download.csdn.net/detail/wangliang198901/7809439

将整个框架导入IOS新建立的project中

FKAppDelegate.h声明 例如以下:
     

#import <UIKit/UIKit.h>

#import "AFHTTPRequestOperationManager.h"


@interface FKAppDelegate :UIResponder <UIApplicationDelegate>


@property (strong,nonatomic)UIWindow *window;

@property (strong,nonatomic)AFHTTPRequestOperationManager* manager;

@end


然后在 FKAppDelegate.m文件初始化

#import "FKAppDelegate.h"


@implementation FKAppDelegate


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

{

self.manager = [AFHTTPRequestOperationManagermanager];

self.manager.responseSerializer = [[AFHTTPResponseSerializeralloc]init];

    return YES;

}



然后在自定义ViewController主要做例如以下操作

#import "FKViewController.h"

#import "FKAppDelegate.h"

@interface FKViewController ()

{

FKAppDelegate* appDelegate;

NSArray* images;

}

@end

@implementation FKViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

appDelegate = [UIApplicationsharedApplication].delegate;

self.picker.dataSource = self;

self.picker.delegate = self;

// 使用简化语法创建NSArray集合

images =@[@"logo",@"java" , @"android"];

}

// UIPickerViewDataSource中定义的方法。该方法返回值决定该控件包括多少列

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

//返回1表明该控件仅仅包括1

return1;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView

numberOfRowsInComponent:(NSInteger)component

{

returnimages.count;

}

#define kImageTag 1

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:

(NSInteger)row forComponent:(NSInteger)component

reusingView:(UIView *)view

{


//假设可重用的viewtag不等于kImageTag,表明该view已经不存在,须要又一次创建

if(view.tag !=kImageTag)

{

view = [[UIViewalloc]init];

// 为该UIView设置tag属性

view.tag =kImageTag;

//设置不同意用户交互

view.userInteractionEnabled =NO;

UIImageView* iv = [[UIImageViewalloc]initWithImage:

[UIImageimageNamed:[imagesobjectAtIndex:row]]];

iv.frame =CGRectMake(0 ,0  , 48 ,48);

iv.contentMode =UIViewContentModeScaleAspectFit;

[viewaddSubview:iv];

}

return view;

}

// UIPickerViewDelegate中定义的方法,该方法的返回值决定列表项的高度

- (CGFloat)pickerView:(UIPickerView *)pickerView

rowHeightForComponent:(NSInteger)component

{

return48;

}

// UIPickerViewDelegate中定义的方法,该方法的返回值决定列表项的宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView

widthForComponent:(NSInteger)component

{

return48;

}

- (IBAction)upload:(id)sender

{

//获取用户选中的行

NSInteger selectedRow = [self.pickerselectedRowInComponent:0];

//获取用户选中的文件名称

NSString* fileName = [imagesobjectAtIndex:selectedRow];

//依据用户选中的文件名称确定须要上传的文件

NSURL *filePath = [[NSBundlemainBundle]URLForResource:fileName

withExtension:@"png"];

NSDictionary *parameters =@{@"name":@"额外的请求參数"};

// 使用AFHTTPRequestOperationManager发送POST请求

[appDelegate.manager

POST:@"http://192.168.1.88:8888/AFNetworkingServer/upload"

parameters:parameters

//使用代码块来封装要上传的文件数据

constructingBodyWithBlock:^(id<AFMultipartFormData> formData)

{

[formDataappendPartWithFileURL:filePath //指定上传的文件

name:@"file" //指定上传文件相应的请求參数名

//指定上传文件的原始文件名称

fileName:[NSStringstringWithFormat:@"%@.png" ,fileName]

//指定上传文件的MIME类型

mimeType:@"image/png"

error:nil];

}

//获取server响应成功时激发的代码块

success:^(AFHTTPRequestOperation *operation,id responseObject)

{

//当使用HTTP响应解析器时,server响应数据被封装在NSData

// 此处将NSData转换成NSString、并使用UIAlertView显示登录结果

[[[UIAlertViewalloc]initWithTitle:@"上传结果" message:

  [[NSStringalloc]initWithData:responseObjectencoding:

NSUTF8StringEncoding]delegate:self

cancelButtonTitle:@"确定"otherButtonTitles:nil]

show];

}

//获取server响应失败时激发的代码块

failure:^(AFHTTPRequestOperation *operation,NSError *error)

{

NSLog(@"获取server响应出错!

");

}];

}

@end



源代码下载: http://download.csdn.net/detail/wangliang198901/7813361

                   注:本文章属于个人原创  请尊重个人劳动成果,谢谢。







版权声明:本文博主原创文章,博客,未经同意,不得转载。




本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4758607.html,如需转载请自行联系原作者


相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
85 3
|
1月前
|
移动开发 前端开发 数据安全/隐私保护
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
iOS发布证书.p12文件无密码解决办法及导出带密码的新.p12文件方法
29 0
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
39 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
91 2
|
2月前
|
Linux 数据安全/隐私保护 iOS开发
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
|
2月前
|
Web App开发 Go iOS开发
【IOS】教你如何在手机端轻松安装 ipa 文件 -(安装器已失效 21.10)|社区征文
【IOS】教你如何在手机端轻松安装 ipa 文件 -(安装器已失效 21.10)|社区征文
|
7天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
11 0
|
22天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
1月前
|
移动开发 监控 小程序
mPaaS常见问题之uniapp ios端云打包的配置config文件如何解决
mPaaS(移动平台即服务,Mobile Platform as a Service)是阿里巴巴集团提供的一套移动开发解决方案,它包含了一系列移动开发、测试、监控和运营的工具和服务。以下是mPaaS常见问题的汇总,旨在帮助开发者和企业用户解决在使用mPaaS产品过程中遇到的各种挑战
26 0