IOS開發之檔案上傳,ios檔案上傳
IOS開發之檔案上傳
在行動裝置 App開發 檔案形式上傳是必不可少的,最近把IOS這塊檔案上傳檔案代碼簡單的整理一下,如果大家有需要安卓這邊的代碼,本人也可以分享給大家!QQ群:74432915 歡迎大家一起探討
首先本demo採用網上開源架構 AFNetworking 源碼:http://download.csdn.net/detail/wangliang198901/7809439
將整個架構匯入IOS建立立的工程中
在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
{
//如果可重用的view的tag不等於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];
}
//擷取伺服器響應成功時激發的代碼塊
success:^(AFHTTPRequestOperation *operation,id responseObject)
{
//當使用HTTP響應解析器時,伺服器響應資料被封裝在NSData中
// 此處將NSData轉換成NSString、並使用UIAlertView顯示登入結果
[[[UIAlertViewalloc]initWithTitle:@"上傳結果" message:
[[NSStringalloc]initWithData:responseObjectencoding:
NSUTF8StringEncoding]delegate:self
cancelButtonTitle:@"確定"otherButtonTitles:nil]
show];
}
//擷取伺服器響應失敗時激發的代碼塊
failure:^(AFHTTPRequestOperation *operation,NSError *error)
{
NSLog(@"擷取伺服器響應出錯!");
}];
}
@end
源碼下載: http://download.csdn.net/detail/wangliang198901/7813361 註:本文章屬於個人原創 請尊重個人勞動成果,謝謝!
object-c 相關 目前在開發ios 相關軟體 由於客戶要 把檔案上傳到ftp 伺服器 上傳完畢後改名
?
ios開發人員怎上傳渠道?
ios開發人員上傳應用渠道,首先想到的就是 app store ,App Store上傳流程分為幾個步驟,分別為上傳應用—確定價格—應用程式評等—上傳檔案—等待審核—應用上架等;在國內還有一些助手類市場渠道可提供上傳,分別為:Itools、91助手、同步推、pp助手、快用助手、海馬助手等。
IOS產品開發人員在推廣上相對於安卓,操作空間會小一些,推廣效果與產品的定位、受眾的群體、產品的品質等關係巨大。
更多APP推廣相關知識可查看 酷傳推廣手冊!