2016年6月7日 星期二

OSC for Swift

since: 2016/06/07
update: 2016/06/07
reference:
1. marsman12019/buzzOSC

A. 新增專案:
    1. Xcode > File > New > Project...

    2. iOS > Application > Single View Application > Next

    3. options for project:
        Product Name: Hello_OSC
        Organization Identifier: tw.blogspot.app4iot
        Language: Swift
        Device: iPhone
         > Next

    4. > 選擇專案目錄存放位置
        > 勾選: Create Git repository on Mac
        > Create

-----------------------------------------------------------------------------------------------

B. OSC Library
    1. 從 marsman12019/buzzOSC 下載 buzzOSC-master.zip,
        解壓縮後將整個 F53OSC 目錄複製到 Hello_OSC 專案目錄裡.
        (先新增一個 library 資料夾)

    2. 在專案裡, 新增二個階層群組: ResourcesF53OSC
        > F53OSC 滑鼠右鍵 > Add Files to "Hello_OSC" ...

    3. 選取專案的 F53OSC 目錄下的所有檔案目錄
        > Add

    4. Create Bridging Header

     5. add Header to Bridge to Swift
         > 點選 F53OSC 目錄下的 Hello_OSC-Bridging-Header.h 檔案
         > 新增內容:
//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "F53OSC.h"


-----------------------------------------------------------------------------------------------

C. 新增 OSC 接收功能:

    1. 開啟 ViewController.swift 檔案, 修改如下:
//
//  ViewController.swift
//  Hello_OSC
//
//  Created by LanliChen on 2016/6/7.
//  Copyright © 2016年 Lanli. All rights reserved.
//


import UIKit

//class ViewController: UIViewController {
//@update for F53OSC ############

class ViewController: UIViewController, F53OSCPacketDestination {

    //@add for F53OSC ############
    var oscServer: F53OSCServer!
    var myAddressPattern = ""
    var myArguments = [AnyObject]()
   
    //@add for F53OSC ############
    func takeMessage(message: F53OSCMessage!) {
       
        print("Received '\(message)'")
       
        //        let addressPattern = message.addressPattern
        //        let arguments = message.arguments

        myAddressPattern = message.addressPattern
        myArguments = message.arguments
        print("myAddressPattern = " + myAddressPattern)
        print(myArguments) // print(myArguments[0])
    }
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        //@add for F53OSC ############
        print("Starting OSC Server")
        oscServer = F53OSCServer.init()
        oscServer.port = 3030
        oscServer.delegate = self
        if oscServer.startListening() {
            print("Listening for messages on port \(oscServer.port)")
        } else {
            print("Error: Server was unable to start listening on port \(oscServer.port)")
        }
       
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


2. 修正錯誤:
    編譯時出現以下錯誤:
    .... iOS/Hello_OSC/library/F53OSC/GCDAsyncSocket.m:7928:75:
  
'kCFStreamNetworkServiceTypeVoIP' is deprecated: first deprecated in iOS 9.0 - use PushKit for VoIP control purposes


   修正方式:
   開啟: .... iOS/Hello_OSC/library/F53OSC/GCDAsyncSocket.m 檔案, 修改如下:

#import "GCDAsyncSocket.h"

//@add #############################################
#import <PushKit/PushKit.h>


- (BOOL)enableBackgroundingOnSocketWithCaveat:(BOOL)caveat
{
    if (![self createReadAndWriteStream])
    {
        // Error occured creating streams (perhaps socket isn't open)
        return NO;
    }
   
    BOOL r1, r2;
   
    LogVerbose(@"Enabling backgrouding on socket");
   
    //r1 = CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
    //r2 = CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
    //@update ###############################################################################
    r1 = CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, PKPushTypeVoIP);
    r2 = CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, PKPushTypeVoIP);

   
    if (!r1 || !r2)
    {
        return NO;
    }
   
    if (!caveat)
    {
        if (![self openStreams])
        {
            return NO;
        }
    }
   
    return YES;
}


-----------------------------------------------------------------------------------------------

D. 編譯並執行:


沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。