你好,歡迎來到IOS教程網

 Ios教程網 >> IOS基礎知識 >> IOS基礎知識 >> iOS基礎8:自定義MyData/自定義SQLite用於網絡判斷,版本判斷,圖片緩存處理,下載或者上傳的GET或POST請求,加密手段,.數據解析

iOS基礎8:自定義MyData/自定義SQLite用於網絡判斷,版本判斷,圖片緩存處理,下載或者上傳的GET或POST請求,加密手段,.數據解析

編輯:IOS基礎知識
镔哥,這篇文章是用自己的工程項目來的,主要是代碼為主,但是會有注釋,讓大家能更好理解,讓大家做工程更多一個選擇,這其實就是自己自定義SQLite,不多說,見代碼例子

MyDAta的.h文件

/*

MyData這一個基本上每個工程都能用到的Demo,他包含了很多內容:

1.iPhone機網絡判斷;

2.iPhone機軟件版本判斷(主要是看是否是大於IOS7);

3.圖片緩存處理;

4.下載或者上傳的GET或POST請求;

5.加密手段;

6.數據解析;

*/

//*****************************************************************************************

/*

宏定義SQL語句:

創建菜單數據存儲表:

*/

#define MAPPINGDATA @"create table if not exists mapping(name text,title text)"//刷圖記錄表1

//******************************

#define SHAREDATA @"create table if not exists sharedata(name text,title text)"//分享記錄表2

//******************************

#define JOBDATA @"create table if not exists job(name text,title text,integral text,taskId text)"//威客記錄表3

//******************************

#define BRUSH @"create table if not exists brush(name text,taskid text,jifen text,url text)"//刷單記錄表4

//********************


#define LEAFLETSDATA @"create table if not exists leaft(name text,taskid text,jifen text,url text)"//傳單記錄5



/*

給表中添加數據:

*/

#define TJSJMAPPINGDATA @"insert into mapping(name,title) values(?,?)"//添加數據於刷圖記錄表1

//********************************

#define TJSJSHAREDATA @"insert into sharedata(name,title) values(?,?)"//添加數據於分享記錄表2

//********************************

#define TJSJJOBDATA @"insert into job(name,title,integral,taskId) values(?,?,?,?)"//添加數據於訂單威客記錄表3

//********************************

#define TJSJBRUSH @"insert into brush(name,taskid,jifen,url) values(?,?,?,?)"//添加數據於刷單記錄表4


#define TJLEAFLETSDATA @"insert into leaft(name,taskid,jifen,keyword,url) values(?,?,?,?,?)"//添加傳單與表中5



/*

讀取表中數據:

*/

#define DQSJMAPPINGDATA @"select *from mapping"//讀取刷圖記錄表1

#define DQSJSHAREDATA @"select *from sharedata"//讀取分享記錄表2

#define DQSJJOBDATA @"select *from job"//讀取訂單威客記錄表3

#define DQSJBRUSH @"select *from brush"//讀取訂單表中刷單記錄表4

#define DQLEAFLETSDATA @"select *from leaft"//讀取傳單表數據5


/*

刪除表中所有數據:

*/

#define SCSJMAPPINGDATA @"delete from mapping"//刪除食物表中數據1

#define SCSJSHAREDATA @"delete from sharedata"//刪除訂單表中數據2

#define SCSJJOBDATA @"delete from job"//刪除訂單表中數據3

#define SCSJBRUSH @"delete from brush"//刪除訂單表中數據4

#define SCSJLEAFLETSDATA @"delete from leaft"//刪除傳單中得數據5


//**********************************************************************************************


#import

#import

@protocol MyDataDelegate //定義一個協議

/*

參數:

datas ->返回數據;

ressNum ->下載完成度;0-1;

str ->訪問地址;

*/

@optional

-(void)sendData:(NSMutableData *)datas andProgress:(float)ressNum andFilg:(NSString *)str andIndexPath:(NSIndexPath *)indexPath andError:(BOOL)error;

-(void)sendData:(NSMutableData *)datas andFilg:(NSString *)str andError:(BOOL)error;

-(void)sendData:(NSMutableData *)datas andProgress:(float)ressNum andFilg:(NSString *)str andError:(BOOL)error;

-(void)sendData:(NSMutableData *)datas andFilg:(NSString *)str andIndexPath:(NSIndexPath *)indexPath andError:(BOOL)error;


@end


@interface MyData :NSObject

/***********************************************************************

本工程變量定義:

************************************/

{

NSMutableData *receiveData;//用來記錄數據data

NSString *urlstr;//用來記錄網址

NSTimer *timer;//用來定時,設置緩沖時間

NSURLConnection *connection;//用來控制下載暫停

long long AllLength;//獲取文件總長

}

@property(nonatomic,assign)iddelegate;

@property(nonatomic,assign)NSIndexPath *indexPath;




/***********************************************************************

本工程固定比用方法定義:

************************************/

/*

判斷系統是否是ios7;

*/

+(BOOL)isIos7;

+(BOOL)isIos6;

/*

圖片緩存處理;

*/

+(NSString *)huoPathString:(NSString *)string;



+(NSString *)picturePath;//獲取圖片存儲地址

+(BOOL)addpicture:(UIImage *)image andAddress:(NSString *)path;//將圖片存入緩存文件夾

+(BOOL)judgeIsImage:(NSString *)path;//判斷圖片是否在文件夾中存在

+(UIImage *)obtainImage:(NSString *)path;//從文件夾中獲取圖片

+(BOOL)RemoveArrPictures;//刪除所有圖片

+(float)folderSizeAtPath;

/*

md5加密;

*/

+(NSString *)md5:(NSString *)str;

/*

下載數據方法;

*/

-(void)downloadGET:(NSString *)urlStr;//開始下載

-(void)stopDownload;//下載暫停

/*

POST請求;

*/

-(void)postUrl:(NSString *)url andString:(NSString *)string;


//-(void)postUrl:(NSString *)url andString:(NSString*)

/*

網絡判斷;

*/

+(BOOL)determineNetworkStatus;

/*

XML和JSON解析;

*/

+(void)jsonFor:(NSMutableArray *)array toIn:(NSData *)data;

+(void)xmlFor:(NSMutableArray *)array toIn:(NSData *)data;



/***********************************************************************

本工程非固定比用方法定義:

************************************/

/*

上傳圖片文件

*/

+(NSString *)postRequestWithURL:(NSString *)url

postParems:(NSMutableDictionary *)postParems

postImage:(UIImage *)image

fileName:(NSString *)name;




//+(BOOL)uploadJPEGImage:(UIImage *)image toUrl:(NSString *)url asFilename:(NSString *)filename postParems:(NSMutableDictionary *)postParems;



/*

數據庫;

*/

//存數據

+(NSString *)filePath;


+(BOOL)cunMappingDataOfName:(NSString *)name andString:(NSString *)title;

+(BOOL)cunShareDataOfName:(NSString *)name andString:(NSString *)title;

+(BOOL)cunJobDateOfName:(NSString *)name andString:(NSString *)title andTTString:(NSString *)text andtaskId:(NSString *)taskId;

+(BOOL)cunBrushName:(NSString *)name andtaskId:(NSString *)taskId andJifen:(NSString *)jifen andUrl:(NSString *)urlStr;

+(BOOL)cunLeafName:(NSString *)name andtaskId:(NSString *)taskId andJifen:(NSString *)jifen andkeyWord:(NSString *)keyWord andUrl:(NSString *)urlStr;



//獲取數據

+(NSMutableArray *)quMappingData;

+(NSMutableArray *)quShareData;

+(NSMutableArray *)quJobData;

+(NSMutableArray *)quBrushData;

+(NSMutableArray *)quLeafData;


//刪除數據

+(BOOL)shanMappingData;

+(BOOL)shanShareData;

+(BOOL)shanJobData;

+(BOOL)shanBrushData;

+(BOOL)shanLeafDat



//==================================.m文件===========================

MyData .m文件

/*

MyData這一個基本上每個工程都能用到的Demo,他包含了很多內容:

1.iPhone機網絡判斷;

2.iPhone機軟件版本判斷(主要是看是否是大於IOS7);

3.圖片緩存處理;

4.下載或者上傳的GET或POST請求;

5.加密手段;

6.數據解析;

*/

#import "MyData.h"

#import

#import "Reachability.h"

#import "JSON.h"

#import "GDataXMLNode.h"

@implementation MyData

static sqlite3 *db=nil;//聲明一個sqlite3數據庫



static NSString *const FORM_FLE_INPUT = @"uploadFile";//聲明一個未上傳數據常量

//版本判斷******************************************************************************

NSUInteger DeviceSystemMajorVersion();

NSUInteger DeviceSystemMajorVersion()

{

static NSUInteger _deviceSystemMajorVersion = -1;

//GCD單列用(得到唯一的版本號)

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_deviceSystemMajorVersion = [[[[[UIDevicecurrentDevice] systemVersion]componentsSeparatedByString:@"."]objectAtIndex:0]intValue];

});

return _deviceSystemMajorVersion;

}

//是否大於等於ios7

+(BOOL)isIos7

{

if ((DeviceSystemMajorVersion() >=7)) {

return YES;

}else{

return NO;

}

}

//是否大於等於ios6

+(BOOL)isIos6

{

if ((DeviceSystemMajorVersion() >=6)) {

return YES;

}else{

return NO;

}

}

//圖片處理*******************************************************************************

+(NSString *)picturePath

{

//保存在沙箱

NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];

//沙箱路徑追加文件名

path=[path stringByAppendingPathComponent:@"圖片緩存"];

NSFileManager *fm=[NSFileManagerdefaultManager];//文件管理器(處理文件分配、保存,讀取)

//判讀文件路徑是否存在

if ([fm fileExistsAtPath:path]==NO)

{

//就創建文件路徑

[fm createDirectoryAtPath:pathwithIntermediateDirectories:YESattributes:nilerror:nil];

}

return path;

}

+(NSString *)picturesName:(NSString *)path//利用圖片的url生成圖片的名稱:

{

if ([path hasSuffix:@".bmp"]||[pathhasSuffix:@".jpg"]||[pathhasSuffix:@".tiff"]||[pathhasSuffix:@".gif"]||[path hasSuffix:@".pcx"]||[pathhasSuffix:@".png"]||[pathhasSuffix:@".jpeg"])

{

NSMutableString *str=[NSMutableStringstringWithString:path];

for (int i=path.length-1; i>=0; i--)

{

if ([str characterAtIndex:i]=='/') {

[str deleteCharactersInRange:NSMakeRange(i,1)];

}

}

return str;

}

else

{

NSMutableString *str=[NSMutableStringstringWithString:[path stringByAppendingString:@".png"]];

for (int i=path.length-1; i>=0; i--)

{

if ([str characterAtIndex:i]=='/') {

[str deleteCharactersInRange:NSMakeRange(i,1)];

}

}

return str;

}

}

+(BOOL)addpicture:(UIImage *)image andAddress:(NSString *)path

{

NSFileManager *file=[NSFileManagerdefaultManager];//定義文件

NSString *pp=[MyDatapicturePath];

NSString *lujing=[ppstringByAppendingPathComponent:[MyDatapicturesName:path]];//創建文件路徑

NSData *data=UIImagePNGRepresentation(image);//將圖片轉換為NSData格式

BOOL flay=[filecreateFileAtPath:lujing contents:dataattributes:nil];//創建文件

return flay;

}

+(BOOL)judgeIsImage:(NSString *)path

{

NSString *str=[MyDatapicturesName:path];

NSString *str_1=[MyDatapicturePath];

NSString *string=[str_1stringByAppendingPathComponent:str];

NSFileManager *manager=[NSFileManagerdefaultManager];

if ([manager fileExistsAtPath:string]==YES)

{

return YES;

}

else

{

return NO;

}

}

+(UIImage *)obtainImage:(NSString *)path

{

NSFileManager *file=[NSFileManagerdefaultManager];//定義文件

NSString *pp=[MyDatapicturePath];

NSString *lujing=[ppstringByAppendingPathComponent:[MyDatapicturesName:path]];//創建文件路徑

NSData *data=[file contentsAtPath:lujing];

UIImage *image=[UIImageimageWithData:data];

return image;

}

+(NSString *)huoPathString:(NSString *)string

{

NSString *pp=[MyDatapicturePath];

return [ppstringByAppendingPathComponent:[MyDatapicturesName:string]];//創建文件路徑

}


+(BOOL)RemoveArrPictures

{

NSError *error=nil;

NSString *path=[MyDatapicturePath];

NSFileManager *fileManager=[NSFileManagerdefaultManager];

[fileManager removeItemAtPath:path error:&error];

return YES;

}

//md5加密***************************************************************

+(NSString *)md5:(NSString *)str

{

const char *cStr = [strUTF8String];

unsigned char result[32];

CC_MD5( cStr, strlen(cStr), result );

return [NSStringstringWithFormat:

@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",

result[0], result[1], result[2], result[3],

result[4], result[5], result[6], result[7],

result[8], result[9], result[10], result[11],

result[12], result[13], result[14], result[15]

];

}



//下載數據*********************************************************

-(void)downloadGET:(NSString *)urlStr

{

NSURL *url=[NSURLURLWithString:urlStr];

NSURLRequest *request=[NSURLRequestrequestWithURL:url];

connection=[NSURLConnectionconnectionWithRequest:request delegate:self];

// timer=[NSTimer scheduledTimerWithTimeInterval:40.0 target:self selector:@selector(TerminationOfTheThread:) userInfo:nil repeats:NO];

}

-(void)stopDownload

{

[connection cancel];//取消下載

}


-(void)TerminationOfTheThread:(NSTimer *)timer1

{

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andIndexPath:andError:)]) {

[self.delegatesendData:nilandProgress:0andFilg:@"訪問超時"andIndexPath:nilandError:YES];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andError:)]) {

[self.delegatesendData:nilandProgress:0andFilg:@"訪問超時"andError:YES];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andFilg:andIndexPath:andError:)]) {

[self.delegatesendData:nilandFilg:@"訪問超時"andIndexPath:self.indexPathandError:YES];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andFilg:andError:)]) {

[self.delegatesendData:nilandFilg:@"訪問超時"andError:YES];

}

[connection cancel];//停止下載

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{//將要下載時執行

receiveData=[[NSMutableDataalloc]initWithCapacity:0];

AllLength=[responseexpectedContentLength];

urlstr=[[response URL]absoluteString];

}

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

{//正在下載時執行

[receiveData appendData:data];

long long currentDataLength=[receiveDatalength];

float proportion=(float)currentDataLength/(float)AllLength;

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andIndexPath:andError:)]) {

[self.delegatesendData:receiveDataandProgress:proportion andFilg:urlstrandIndexPath:self.indexPathandError:NO];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andError:)]) {

[self.delegatesendData:receiveDataandProgress:proportion andFilg:urlstrandError:NO];

}

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{//下載完成時執行

NSLog(@"3333333333333333");

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andIndexPath:andError:)]) {

[self.delegatesendData:receiveDataandProgress:1andFilg:urlstrandIndexPath:nilandError:NO];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andError:)]) {

[self.delegatesendData:receiveDataandProgress:1andFilg:urlstrandError:NO];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andFilg:andIndexPath:andError:)]) {

[self.delegatesendData:receiveDataandFilg:urlstrandIndexPath:self.indexPathandError:NO];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andFilg:andError:)]) {

[self.delegatesendData:receiveDataandFilg:urlstrandError:NO];

}

}

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

{//錯誤時!!!!!!!!!!!!!

NSLog(@"444444444444444");

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andIndexPath:andError:)]) {

[self.delegatesendData:nilandProgress:0andFilg:@"訪問超時"andIndexPath:nilandError:YES];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andProgress:andFilg:andError:)]) {

[self.delegatesendData:nilandProgress:0andFilg:@"訪問超時"andError:YES];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andFilg:andIndexPath:andError:)]) {

[self.delegatesendData:nilandFilg:@"訪問超時"andIndexPath:self.indexPathandError:YES];

}

if ([self.delegaterespondsToSelector:@selector(sendData:andFilg:andError:)]) {

[self.delegatesendData:nilandFilg:@"訪問超時"andError:YES];

}

}

//Post









//網絡判斷*******************************************************************************

+(BOOL)determineNetworkStatus

{

BOOL flay;

Reachability *r=[ReachabilityreachabilityForInternetConnection];

switch ([rcurrentReachabilityStatus]) {

caseNotReachable:

flay=NO;

break;

caseReachableViaWiFi:

flay=YES;

break;

caseReachableViaWWAN:

flay=YES;

break;

default:

break;

}

if (!flay) {

UIAlertView *alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:@"沒有檢測到網絡"delegate:selfcancelButtonTitle:@"知道了~"otherButtonTitles:nil];

[alert show];

}

return flay;

}




//POST請求****************************************************************************

-(void)postUrl:(NSString *)url andString:(NSString *)string

{

// NSLog(@"4444444444444");

NSMutableData *postData=[[NSMutableDataalloc]init];

[postData appendData:[stringdataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheDatatimeoutInterval:20.0];

[request setHTTPMethod:@"POST"];

[request setHTTPBody:postData];

connection=[NSURLConnectionconnectionWithRequest:request delegate:self];

NSLog(@"========%@",request);

// timer=[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(TerminationOfTheThread:) userInfo:nil repeats:NO];

}


//JSON和XML解析***********************************************************************

+(void)jsonFor:(NSMutableArray *)array toIn:(NSData *)data

{

[array removeAllObjects];

//方法一:(系統提供方法)

NSArray *arr=[NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];

[array setArray:arr];

//方法二:(第三方提供方法“JSON.h”)

NSString *str=[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];

[array setArray:[str JSONValue]];

}

+(void)xmlFor:(NSMutableArray *)array toIn:(NSData *)data

{

[array removeAllObjects];

NSError *error=nil;

GDataXMLDocument *document=[[GDataXMLDocumentalloc]initWithData:dataoptions:0 error:&error];

GDataXMLElement *element=[document rootElement];

NSArray *eArray=[element elementsForName:@"student"];

for (GDataXMLElement *ein eArray) {

NSMutableDictionary *dic=[[NSMutableDictionaryalloc]init];

GDataXMLElement *subE1=[[e elementsForName:@"name"] lastObject];

[dic setObject:[subE1 stringValue] forKey:@"name"];

GDataXMLElement *subE2=[[e elementsForName:@"age"] lastObject];

[dic setObject:[subE2 stringValue] forKey:@"age"];

GDataXMLElement *subE3=[[e elementsForName:@"sex"] lastObject];

[dic setObject:[subE3 stringValue] forKey:@"sex"];

[array addObject:dic];

}

}

//單個文件的大小********************************************************

+(long long)fileSizeAtPath:(NSString*) filePath{

NSFileManager* manager = [NSFileManagerdefaultManager];

if ([manager fileExistsAtPath:filePath]){

return [[managerattributesOfItemAtPath:filePath error:nil]fileSize];

}

return 0;

}

+(float)folderSizeAtPath

{

NSFileManager* manager = [NSFileManagerdefaultManager];

if (![manager fileExistsAtPath:[MyData picturePath]])

{

return 0;

}

NSEnumerator *childFilesEnumerator = [[managersubpathsAtPath:[MyDatapicturePath]] objectEnumerator];

NSString* fileName;

long long folderSize =0;

while ((fileName = [childFilesEnumerator nextObject]) != nil){

NSString* fileAbsolutePath = [[MyDatapicturePath] stringByAppendingPathComponent:fileName];

folderSize += [MyData fileSizeAtPath:fileAbsolutePath];

}

return folderSize/(1024.0*1024.0);

}



//+(BOOL)uploadJPEGImage:(UIImage *)image toUrl:(NSString *)url asFilename:(NSString *)filename postParems:(NSMutableDictionary *)postParems

//{

// NSString *TWITTERFON_FORM_BOUNDARY = @"0xKhTmLbOuNdArY";

//

// //分界線 --AaB03x

// NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];

// //結束符 AaB03x--

// NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];

//

//

//

//

//

//

//

// //取得圖形數據

// NSData *imageData=UIImageJPEGRepresentation(image, 0.1);

//

// //試用post上傳到服務器上

// NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];

// [request setURL:[NSURL URLWithString:url]];

// [request setHTTPMethod:@"POST"];

//

// //設置content type

// NSString *boundary=[NSString stringWithFormat:@"----------------------------%ld",random()];

//

// NSString *contentType=[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

// [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

//

// //設置body

// NSMutableData *body=[NSMutableData data];

//

//

// NSMutableString *bodyb=[[NSMutableString alloc]init];

// //參數的集合的所有key的集合

// NSArray *keys= [postParems allKeys];

//

// //遍歷keys

// for(int i=0;i<[keys count];i++)

// {

// //得到當前key

// NSString *key=[keys objectAtIndex:i];

//

// //添加分界線,換行

// [bodyb appendFormat:@"%@\r\n",MPboundary];

// //添加字段名稱,換2行

// [bodyb appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];

// //添加字段的值

// [bodyb appendFormat:@"%@\r\n",[postParems objectForKey:key]];

//

// NSLog(@"添加字段的值==%@",[postParems objectForKey:key]);

// }

//

// //聲明myRequestData,用來放入http body

//

//

// //將body字符串轉化為UTF8格式的二進制

//

//

//

//

// [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//

//

// [body appendData:[bodyb dataUsingEncoding:NSUTF8StringEncoding]];

//

//

// [body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data;name=\"imageFilename\";filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];

// [body appendData:[@"Content-Type:application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// [body appendData:imageData];

// [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//

//

//

//

// [request setHTTPBody:body];

//

// //連接到服務器

// NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//

// NSString *returnString=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];

// if ([returnString compare:@"YES"]==NSOrderedSame) {

// return YES;

// }else{

// return NO;

// }

//}






+(NSString *)postRequestWithURL:(NSString *)url

postParems:(NSMutableDictionary *)postParems

postImage:(UIImage *)image

fileName:(NSString *)name

{

NSString *TWITTERFON_FORM_BOUNDARY = @"0xKhTmLbOuNdArY";

//根據url初始化request

NSMutableURLRequest* request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:url]

cachePolicy:NSURLRequestReloadIgnoringLocalCacheData

timeoutInterval:10];

//分界線 --AaB03x

NSString *MPboundary=[[NSStringalloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];

//結束符 AaB03x--

NSString *endMPboundary=[[NSStringalloc]initWithFormat:@"%@--",MPboundary];

NSData* data;

if(image){

//判斷圖片是不是png格式的文件

if (UIImagePNGRepresentation(image)) {

//返回為png圖像。

NSLog(@"返回png");

// data = UIImagePNGRepresentation(image);

data = UIImageJPEGRepresentation(image,0.1);

}else {

//返回為JPEG圖像。

NSLog(@"返回jpeg");

data = UIImageJPEGRepresentation(image,0.1);

}

}

NSLog(@"---------%d",data.length);

//http body的字符串

NSMutableString *body=[[NSMutableStringalloc]init];

//參數的集合的所有key的集合

NSArray *keys= [postParems allKeys];

//遍歷keys

for(int i=0;i<[keyscount];i++)

{

//得到當前key

NSString *key=[keys objectAtIndex:i];

//添加分界線,換行

[body appendFormat:@"%@\r\n",MPboundary];

//添加字段名稱,換2行

[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];

//添加字段的值

[body appendFormat:@"%@\r\n",[postParemsobjectForKey:key]];

NSLog(@"添加字段的值==%@",[postParemsobjectForKey:key]);

}

if(image){

////添加分界線,換行

[body appendFormat:@"%@\r\n",MPboundary];

//聲明pic字段,文件名為boris.png

[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",FORM_FLE_INPUT,name];

//聲明上傳文件的格式

[body appendFormat:@"Content-Type: image/jpg\r\n\r\n"];

}

//聲明結束符:--AaB03x--

NSString *end=[[NSStringalloc]initWithFormat:@"\r\n%@",endMPboundary];

//聲明myRequestData,用來放入http body

NSMutableData *myRequestData=[NSMutableDatadata];

//將body字符串轉化為UTF8格式的二進制

[myRequestData appendData:[bodydataUsingEncoding:NSUTF8StringEncoding]];

if(image){

//將image的data加入

[myRequestData appendData:data];

}

//加入結束符--AaB03x--

[myRequestData appendData:[enddataUsingEncoding:NSUTF8StringEncoding]];

//設置HTTPHeader中Content-Type的值

NSString *content=[[NSStringalloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];

//設置HTTPHeader

[request setValue:contentforHTTPHeaderField:@"Content-Type"];

//設置Content-Length

[request setValue:[NSStringstringWithFormat:@"%d", [myRequestDatalength]] forHTTPHeaderField:@"Content-Length"];

//設置http body

[request setHTTPBody:myRequestData];

//http method

[request setHTTPMethod:@"POST"];

NSHTTPURLResponse *urlResponese = nil;

NSError *error = [[NSErroralloc]init];

NSData* resultData = [NSURLConnectionsendSynchronousRequest:request returningResponse:&urlResponese error:&error];

NSString* result= [[NSStringalloc] initWithData:resultDataencoding:NSUTF8StringEncoding];

NSLog(@"%@",result);

NSLog(@"%d",[urlResponesestatusCode]);

if([urlResponese statusCode] >=200&&[urlResponese statusCode]<300){

NSLog(@"返回結果=====%@",result);

return result;

}

return nil;

}


//數據庫文件的路徑。一般在沙箱的Documents裡邊操作

+(NSString *)filePath

{

NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];

path=[path stringByAppendingPathComponent:@"shuJu.sqlite"];

return path;

}


//創建表單

+(BOOL)chuangJianTable:(NSString *)string

{

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if(!db)

{

NSLog(@"數據庫打開失敗!");

return NO;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db, [stringUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"創建成功!");

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}


+(BOOL)cunMappingDataOfName:(NSString *)name andString:(NSString *)title

{

[MyDatachuangJianTable:MAPPINGDATA];

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare(db,[TJSJMAPPINGDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

//給問號賦值1,2,3,表示第幾個問號

if (name.length>0) {

sqlite3_bind_text(stmt, 1, [name UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 1, [@"100" UTF8String], -1, nil);

}

if (title.length>0) {

sqlite3_bind_text(stmt, 2, [title UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 2, [@"你镔哥哥"UTF8String], -1,nil);

}

}

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"添加成功!");

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}



//傳單記錄

+(BOOL)cunLeafName:(NSString *)name andtaskId:(NSString *)taskId andJifen:(NSString *)jifen andkeyWord:(NSString *)keyWord andUrl:(NSString *)urlStr

{


[MyDatachuangJianTable:chuandanURL];

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

}

sqlite3_stmt *stmt=nil;

//添加數據到表

int result=sqlite3_prepare(db,[TJLEAFLETSDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

//給問號賦值1,2,3,表示第幾個問號

if (name.length>0) {

sqlite3_bind_text(stmt, 1, [name UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 1, [@"100" UTF8String], -1, nil);

}

if (taskId.length>0) {

sqlite3_bind_text(stmt, 2, [taskId UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 2, [@"優點尚"UTF8String], -1,nil);

}

if (jifen.length>0) {

sqlite3_bind_text(stmt, 3, [jifen UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 3, [@"100" UTF8String], -1, nil);

}

if (keyWord.length>0) {

sqlite3_bind_text(stmt, 4, [keyWord UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 4, [@"100" UTF8String], -1, nil);

}

if (urlStr.length>0) {

sqlite3_bind_text(stmt, 5, [urlStr UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 5, [@"100" UTF8String], -1, nil);

}

}

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!(執行完成)

{

NSLog(@"添加成功!");

}

sqlite3_finalize(stmt);

sqlite3_close(db);


return YES;


}


//刷單記錄

+(BOOL)cunBrushName:(NSString *)name andtaskId:(NSString *)taskId andJifen:(NSString *)jifen andUrl:(NSString *)urlStr

{

[MyDatachuangJianTable:BRUSH];

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

}

sqlite3_stmt *stmt=nil;

//添加數據到表

int result=sqlite3_prepare(db,[TJSJBRUSHUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

//給問號賦值1,2,3,表示第幾個問號

if (name.length>0) {

sqlite3_bind_text(stmt, 1, [name UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 1, [@"100" UTF8String], -1, nil);

}

if (taskId.length>0) {

sqlite3_bind_text(stmt, 2, [taskId UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 2, [@"你镔哥哥"UTF8String], -1,nil);

}

if (jifen.length>0) {

sqlite3_bind_text(stmt, 3, [jifen UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 3, [@"100" UTF8String], -1, nil);

}

if (urlStr.length>0) {

sqlite3_bind_text(stmt, 4, [urlStr UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 4, [@"100" UTF8String], -1, nil);

}

}

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!(執行完成)

{

NSLog(@"添加成功!");

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}



//存取分享(名字、標題)

+(BOOL)cunShareDataOfName:(NSString *)name andString:(NSString *)title

{

[MyDatachuangJianTable:SHAREDATA];

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

}

sqlite3_stmt *stmt=nil;

//添加數據到訂單表

int result=sqlite3_prepare(db,[TJSJSHAREDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)//

{

//給問號賦值1,2,3,表示第幾個問號

if (name.length>0) {

sqlite3_bind_text(stmt, 1, [name UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 1, [@"100" UTF8String], -1, nil);

}

if (title.length>0) {

sqlite3_bind_text(stmt, 2, [title UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 2, [@"你镔哥哥"UTF8String], -1,nil);

}

}

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!(執行完畢)

{

NSLog(@"添加成功!");

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}

+(BOOL)cunJobDateOfName:(NSString *)name andString:(NSString *)title andTTString:(NSString *)text andtaskId:(NSString *)taskId

{

[MyDatachuangJianTable:JOBDATA];

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare(db,[TJSJJOBDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

//給問號賦值1,2,3,表示第幾個問號

if (name.length>0) {

sqlite3_bind_text(stmt, 1, [name UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 1, [@"100" UTF8String], -1, nil);

}

if (title.length>0) {

sqlite3_bind_text(stmt, 2, [title UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 2, [@"你镔哥"UTF8String], -1,nil);

}

if (text.length>0) {

sqlite3_bind_text(stmt, 3, [text UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 3, [@"你镔哥"UTF8String], -1,nil);

}

if (taskId.length>0) {

sqlite3_bind_text(stmt, 4, [taskId UTF8String], -1,nil);

}else{

sqlite3_bind_text(stmt, 4, [@"你镔哥"UTF8String], -1,nil);

}

}

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"添加成功!");

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}

+(NSMutableArray *)quMappingData

{

NSMutableArray *arr=[[NSMutableArrayalloc]init];

//打開數據庫

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (db==nil)

{

NSLog(@"數據庫打開失敗");

return arr;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db, [DQSJMAPPINGDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

while (sqlite3_step(stmt)==SQLITE_ROW)

{

char *name=(char *)sqlite3_column_text(stmt,0);

char *title=(char *)sqlite3_column_text(stmt,1);

NSMutableDictionary *dic=[[NSMutableDictionaryalloc]init];

[dic setObject:[NSStringstringWithCString:name encoding:NSUTF8StringEncoding]forKey:@"name"];

[dic setObject:[NSStringstringWithCString:title encoding:NSUTF8StringEncoding]forKey:@"title"];

[arr addObject:dic];

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return arr;

}


+(NSMutableArray *)quBrushData

{

NSMutableArray *arr=[[NSMutableArrayalloc]init];

//打開數據庫

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (db==nil)

{

NSLog(@"數據庫打開失敗");

return arr;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db, [DQSJBRUSHUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

while (sqlite3_step(stmt)==SQLITE_ROW)

{

char *name=(char *)sqlite3_column_text(stmt,0);

char *taskid=(char *)sqlite3_column_text(stmt,1);

char *jifen=(char *)sqlite3_column_text(stmt,2);

char *url=(char *)sqlite3_column_text(stmt,3);

NSMutableDictionary *dic=[[NSMutableDictionaryalloc]init];

[dic setObject:[NSStringstringWithCString:name encoding:NSUTF8StringEncoding]forKey:@"name"];

[dic setObject:[NSStringstringWithCString:taskid encoding:NSUTF8StringEncoding]forKey:@"taskid"];

[dic setObject:[NSStringstringWithCString:jifen encoding:NSUTF8StringEncoding]forKey:@"jifen"];

[dic setObject:[NSStringstringWithCString:url encoding:NSUTF8StringEncoding]forKey:@"url"];

[arr addObject:dic];

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return arr;

}


+(NSMutableArray *)quShareData

{

NSMutableArray *arr=[[NSMutableArrayalloc]init];

//打開數據庫

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (db==nil)

{

NSLog(@"數據庫打開失敗");

return arr;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db, [DQSJSHAREDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

while (sqlite3_step(stmt)==SQLITE_ROW)

{

char *name=(char *)sqlite3_column_text(stmt,0);

char *title=(char *)sqlite3_column_text(stmt,1);

NSMutableDictionary *dic=[[NSMutableDictionaryalloc]init];

[dic setObject:[NSStringstringWithCString:name encoding:NSUTF8StringEncoding]forKey:@"name"];

[dic setObject:[NSStringstringWithCString:title encoding:NSUTF8StringEncoding]forKey:@"title"];

[arr addObject:dic];

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return arr;

}

+(NSMutableArray *)quJobData

{

NSMutableArray *arr=[[NSMutableArrayalloc]init];

//打開數據庫

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (db==nil)

{

NSLog(@"數據庫打開失敗");

return arr;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db, [DQLEAFLETSDATA UTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

while (sqlite3_step(stmt)==SQLITE_ROW)

{

char *name=(char *)sqlite3_column_text(stmt,0);

char *title=(char *)sqlite3_column_text(stmt,1);

char *text=(char *)sqlite3_column_text(stmt,2);

char *taskId=(char *)sqlite3_column_text(stmt,3);

NSMutableDictionary *dic=[[NSMutableDictionaryalloc]init];

[dic setObject:[NSStringstringWithCString:name encoding:NSUTF8StringEncoding]forKey:@"name"];

[dic setObject:[NSStringstringWithCString:title encoding:NSUTF8StringEncoding]forKey:@"title"];

[dic setObject:[NSStringstringWithCString:text encoding:NSUTF8StringEncoding]forKey:@"text"];

[dic setObject:[NSStringstringWithCString:taskId encoding:NSUTF8StringEncoding]forKey:@"taskId"];

[arr addObject:dic];

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return arr;

}



+(NSMutableArray *)quLeafData

{


NSMutableArray *arr=[[NSMutableArrayalloc]init];

//打開數據庫

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (db==nil)

{

NSLog(@"數據庫打開失敗");

return arr;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db, [DQLEAFLETSDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

//name,taskid,jifen,keyword,url

while (sqlite3_step(stmt)==SQLITE_ROW)

{

char *name=(char *)sqlite3_column_text(stmt,0);

char *taskid=(char *)sqlite3_column_text(stmt,1);

char *jifen=(char *)sqlite3_column_text(stmt,2);

char *url=(char *)sqlite3_column_text(stmt,3);

NSMutableDictionary *dic=[[NSMutableDictionaryalloc]init];

[dic setObject:[NSStringstringWithCString:name encoding:NSUTF8StringEncoding]forKey:@"name"];

[dic setObject:[NSStringstringWithCString:taskid encoding:NSUTF8StringEncoding]forKey:@"taskid"];

[dic setObject:[NSStringstringWithCString:jifen encoding:NSUTF8StringEncoding]forKey:@"jifen"];

[dic setObject:[NSStringstringWithCString:jifen encoding:NSUTF8StringEncoding]forKey:@"keyword"];

[dic setObject:[NSStringstringWithCString:jifen encoding:NSUTF8StringEncoding]forKey:@"taskOptCourse"];

[dic setObject:[NSStringstringWithCString:url encoding:NSUTF8StringEncoding]forKey:@"url"];

[arr addObject:dic];

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return arr;

}


+(BOOL)shanMappingData

{

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

return NO;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db,[SCSJMAPPINGDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"刪除成功!");

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}


+(BOOL)shanBrushData

{

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

return NO;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db,[SCSJMAPPINGDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"刪除成功!");

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}


+(BOOL)shanShareData

{

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

return NO;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db,[SCSJBRUSHUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"刪除成功!");

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}


+(BOOL)shanJobData

{

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

return NO;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db,[SCSJJOBDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"刪除成功!");

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;

}





+(BOOL)shanLeafData

{

sqlite3_open([[MyDatafilePath] UTF8String], &db);

if (!db)

{

NSLog(@"數據庫打開失敗!");

return NO;

}

sqlite3_stmt *stmt=nil;

int result=sqlite3_prepare_v2(db,[SCSJLEAFLETSDATAUTF8String], -1, &stmt,nil);

if (result==SQLITE_OK)

{

if (sqlite3_step(stmt)==SQLITE_DONE)//是程序數據庫運行一步!

{

NSLog(@"刪除成功!");

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return YES;


}









@end



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