如何使用AFNetworking3.0同步plist数据到web服务器?
How to sync plist data to web server using AFNetworking3.0?
当网络连接不可用时,我正在使用 plist 在本地保存我的数据,当网络可用时,我想将我本地保存的数据同步到网络服务器。
我正在使用此代码。但是我不知道数据是否保存在服务器中。
(IBAction)manualSync:(id)sender {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example_url"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://plist_name"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
}
我正在将我的 api 分配给我想要保存 plist 数据的 URL,并将我的 plist 分配给已经保存我的数据的文件路径。谁能解决这个问题。
我认为您使用正常 API 调用存储的 pList 文件到服务器。 因为当您读取 plist 文件时,您将在 NSDictionary 中存储数据并将其发送到服务器进行存储。
要将 plist 值发送到服务器:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *strWebService = [NSString stringWithFormat:@"YOUR_BASE_URL"];
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"YOUR_PLIST_NAME" ofType:@"plist"];
NSDictionary *dicData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
[manager POST:strWebService parameters:dicData constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData)
{
} progress:^(NSProgress * _Nonnull uploadProgress)
{
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSLog(@"%@",responseObject)
;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
当网络连接不可用时,我正在使用 plist 在本地保存我的数据,当网络可用时,我想将我本地保存的数据同步到网络服务器。
我正在使用此代码。但是我不知道数据是否保存在服务器中。
(IBAction)manualSync:(id)sender {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example_url"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://plist_name"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"Success: %@ %@", response, responseObject);
}
}];
[uploadTask resume];
}
我正在将我的 api 分配给我想要保存 plist 数据的 URL,并将我的 plist 分配给已经保存我的数据的文件路径。谁能解决这个问题。
我认为您使用正常 API 调用存储的 pList 文件到服务器。 因为当您读取 plist 文件时,您将在 NSDictionary 中存储数据并将其发送到服务器进行存储。
要将 plist 值发送到服务器:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *strWebService = [NSString stringWithFormat:@"YOUR_BASE_URL"];
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"YOUR_PLIST_NAME" ofType:@"plist"];
NSDictionary *dicData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
[manager POST:strWebService parameters:dicData constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData)
{
} progress:^(NSProgress * _Nonnull uploadProgress)
{
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSLog(@"%@",responseObject)
;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];