Resolved Upload an audio file to a server using Swift

Discussion in 'iOS Programming' started by HoboFlo, Mar 23, 2015.

  1. HoboFlo, Mar 23, 2015
    Last edited: Mar 23, 2015

    HoboFlo macrumors newbie

    Joined:
    Jan 9, 2014
    #1
    Very new to iOs development and Swift here!

    I've been asked by a company I intern for to make an iOs app where the user records a few sentences-worth of audio, is able to listen back to said audio, and either re-record it or submit it. If they choose to submit it, the audio file is emailed to the company.

    To accomplish this I've been using XCode 6.2 and Swift, using an iOs Simulator for testing.

    So far I've accomplished everything but the "submit" part. I want to be able to temporarily upload the audio file to the company website server, and then send that file to the email address as an attachment using PHPMailer (please correct me if there's a better way). The problem is I can't seem to figure out how to upload the file.

    In summary:

    Step 1:
    After recording, the audio file is saved on my simulated phone with the path:

    file:///Users/macpro/Library/Developer/CoreSimulator/Devices/AEAF2794-1DBF-4C6C-B5D1-C7B039370D59/data/Containers/Data/Application/6CB9C44B-4961-446F-847A-C8F76FEB11B4/Documents/audiorecording.m4a

    Step 2:
    Use Swift to upload the audio file to the company's server (need help with this)

    Step 3:
    Use a PHPMailer script (something like below) to automatically email that file to a member of the company.
    PHP:
    <?php

    if (!empty($_POST)) {
        
        require 
    '/PHPMailer/PHPMailerAutoload.php';
        
    $mail = new PHPMailer;

        
    $mail->isSMTP();
        
    $mail->Host 'MY MAIL HOST';
        
    $mail->SMTPAuth true;
        
    $mail->Username 'MY USERNAME';
        
    $mail->Password 'MY PASSWORD';
        
    $mail->SMTPSecure 'ssl';
        
    $mail->SMTPKeepAlive true;
        
    $mail->Port 465;

        
    $mail->From 'FROM ADDRESS';
        
    $mail->FromName 'FROM NAME';
        
    $mail->addAddress("example@email.com");
        
    $mail->isHTML(true);
        
    $body =     'Here is your file';
        
    $mail->AddAttachment(/uploads/audiorecording.m4a);

        
    $mail->Subject 'Uploaded audio';
        
    $mail->Body    $body;
        
    $mail->send();
        
    }

    So can anyone just point me in the right direction for uploading an audio file to a server using Swift? From research, it looks like I should be using a library such as Alamofire or SwiftHTTP, but I'm yet to find a tutorial that explains sending an audio file (they all seem to be for images). Let me know if you have any advice!

    Thanks.
     
  2. HoboFlo thread starter macrumors newbie

    Joined:
    Jan 9, 2014
    #2
    For anyone curious, I've managed to figure it out.

    First I followed the procedure in this tutorial for uploading images, making sure it worked.

    I then basically modified the code to refer to audio rather than images.

    For instance, I changed the file path in myImageUploadRequest() to:

    Code:
    var fileURL = NSURL(fileURLWithPath: audiofilePath)
    As well as changed the destination file and mime type:
    Code:
    let filename = "file.mp3"
    let mimetype = "audio/mpeg"
    (note, I tested this with an mp3 instead of an m4a as in my original post)

    My PHP file looks like this:
    PHP:
    <?php
    $target_dir 
    "./uploads";

    if(!
    file_exists($target_dir))
    {
    mkdir($target_dir0777true);
    }

    $target_dir $target_dir "/" basename($_FILES["file"]["name"]);

    if (
    move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) {
    echo 
    json_encode([
    "Message" => "The file "basename$_FILES["file"]["name"]). " has been uploaded.",
    "Status" => "OK"
    ]);

    } else {

    echo 
    json_encode([
    "Message" => "Sorry, there was an error uploading your file.",
    "Status" => "Error"
    ]);
    After adjusting my php.ini to allow files larger than 2M, the upload works perfectly.

    I suppose this is probably pretty intuitive, but hopefully this will help other newcommers, since there aren't any audio upload tutorials for Swift that I've found, only image upload.
     
  3. Vamer macrumors newbie

    Vamer

    Joined:
    Jun 15, 2015
    #3
    Hey HoboFlo, can you tell me wich exactly Code you have put in to the "myImageUploadRequest()" Part or wich code have you delete in xcode??. Because i get en Error, wehn i just put your example "
    var fileURL = NSURL(fileURLWithPath: audiofilePath)"
    in Xcode. Thank you for your Time.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    You both upload and email the file. Seems redundant. Why not use MFMailComposeViewController to email it directly?
     
  5. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #5
    Maybe because MFMailComposerViewController opens up an email window, from which the user will have to hit "send". Although, barring any other reason for his app to store the email address than to send an email, he just might want to do that.
     

Share This Page