Загрузка файла plist - PullRequest
       3

Загрузка файла plist

0 голосов
/ 15 октября 2018

Я новичок в Objective-C.Я пытаюсь загрузить мой (пустой) список на мой сервер для теста.Вот мой objective-c код:

-(void) writeOnce {

    NSString *one = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"one.plist"];
    NSString *two = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"two.plist"];

    [@"{}" writeToFile:one atomically:YES encoding:NSUTF8StringEncoding error:nil];
    [@"{}" writeToFile:two atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

Затем на viewDidLoad() я звоню [self writeOnce]; После этого.Я загружаю свой файл, используя это:

NSString * plistPath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"one.plist"];
[self uploadLog:plistPath];

My uploadLog() void:

   - (void) uploadLog :(NSString *) filePath
{
    NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
    NSString *urlString =[NSString stringWithFormat:@"server.com/upload.php"];
    NSError *error;

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPMethod:@"POST"];


//  NSData *postData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error]; // error
    NSData *postData =[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    [request setHTTPBody:postData];


    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(response,error);
    }];

    [postDataTask resume];
}

Но.Получаем это:

Тема 3: EXC_BAD_ACCESS (код = 1, адрес = 0x0)

upload.php код:

<?php
$target_path = "./uploads/";  
$target_path = $target_path . basename( $_FILES['file']['name']);  
   if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {  
  echo "The file ".  basename( $_FILES['file']['name'])." has been    uploaded";  
} else {  
    echo "There was an error uploading the file, please try again!";  
} ?>

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...