PowenKo, unity Tutorial, iPhone Objective-C NativeCode

January 3, 2012   ·   0 Comments

Screen Shot 2012-01-02 at 10.57.30 PM

sample code: NativeCodeExample

http://unity3d.com/support/resources/example-projects/iphone-examples

 

 

unity c#

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Bonjour {

	/* Interface to native implementation */
	
	[DllImport ("__Internal")]
	private static extern void _StartLookup (string service, string domain);

	[DllImport ("__Internal")]
	private static extern string _GetLookupStatus ();
	
	[DllImport ("__Internal")]
	private static extern int _GetServiceCount ();
	
	[DllImport ("__Internal")]
	private static extern void _Stop ();
	
	[DllImport ("__Internal")]
	private static extern string _GetServiceName (int serviceNumber);
	
	/* Public interface for use inside C# / JS code */
	
	// Starts lookup for some bonjour registered service inside specified domain
	public static void StartLookup(string service, string domain)
	{
		// Call plugin only when running on real device
		if (Application.platform != RuntimePlatform.OSXEditor)
			_StartLookup(service, domain);
	}
	
	// Stops lookup current lookup
	public static void StopLookup()
	{
		// Call plugin only when running on real device
		if (Application.platform != RuntimePlatform.OSXEditor)
			_Stop();
	}
	
	// Returns current lookup status
	public static string GetLookupStatus()
	{
		// Call plugin only when running on real device
		if (Application.platform != RuntimePlatform.OSXEditor)
		{
			return _GetLookupStatus();
		}
		// Return mockup values for code running inside Editor
		else
		{
			return "Done";
		}
	}
	
	// Returns list of looked up service hosts
	public static string[] GetServiceNames()
	{
		// Call plugin only when running on real device
		if (Application.platform != RuntimePlatform.OSXEditor)
		{
			string[] res = new string[_GetServiceCount()];
			for(int i = 0; i < res.Length; i++)
			{
				res[i] = _GetServiceName(i);
			}
			
			return res;
		}
		// Return mockup values for code running inside Editor
		else
		{
			string[] res = { "hostname1", "hostname2", "hostname3", "hostname4" };
			return res;
		}
	}
}

iphone

BonjourClientImpl.mm



#import "BonjourClientImpl.h"

@implementation NetServiceBrowserDelegate


- (id)init
{
    self = [super init];
    services = [[NSMutableArray alloc] init];
    searching = NO;
	status = @"Initializing";
    return self;
}



- (void)dealloc
{
    [services release];
    [super dealloc];	
}



// Sent when browsing begins
- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser
{
    searching = YES;
	status = @"Searching";
	
	[services removeAllObjects];
}



// Sent when browsing stops
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser
{
    searching = NO;
	status = @"Done";
}



// Sent if browsing fails
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
			 didNotSearch:(NSDictionary *)errorDict
{
    searching = NO;
	NSString * msg = @"Failed.";	
	status = [msg stringByAppendingString:[[errorDict objectForKey:NSNetServicesErrorCode] stringValue]];
}



// Sent when a service appears
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
		   didFindService:(NSNetService *)aNetService
			   moreComing:(BOOL)moreComing
{
    [services addObject:aNetService];
}



// Sent when a service disappears
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
		 didRemoveService:(NSNetService *)aNetService
			   moreComing:(BOOL)moreComing
{
    [services removeObject:aNetService];	
}


- (int)getCount
{
	return [services count];
}

- (NSNetService *)getService:(int)serviceNo
{
	return [services objectAtIndex:serviceNo];
}

- (NSString *)getStatus
{
	return status;
}

@end

static NetServiceBrowserDelegate* delegateObject = nil;
static NSNetServiceBrowser *serviceBrowser = nil;

// Converts C style string to NSString
NSString* CreateNSString (const char* string)
{
	if (string)
		return [NSString stringWithUTF8String: string];
	else
		return [NSString stringWithUTF8String: ""];
}

// Helper method to create C string copy
char* MakeStringCopy (const char* string)
{
	if (string == NULL)
		return NULL;
	
	char* res = (char*)malloc(strlen(string) + 1);
	strcpy(res, string);
	return res;
}

// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {

	void _StartLookup (const char* service, const char* domain)
	{
		if (delegateObject == nil)
			delegateObject = [[NetServiceBrowserDelegate alloc] init];
		
		
		if (serviceBrowser == nil)
			serviceBrowser = [[NSNetServiceBrowser alloc] init];
		
		[serviceBrowser setDelegate:delegateObject];
		
		// Call "searchForServicesOfType" and pass NSStrings as parameters. By default mono
		// marshals all .Net strings as UTF-8 C style strings.
		[serviceBrowser searchForServicesOfType: CreateNSString(service) inDomain: CreateNSString(domain)];
	}
	
	const char* _GetLookupStatus ()
	{
		// By default mono string marshaler creates .Net string for returned UTF-8 C string 
		// and calls free for returned value, thus returned strings should be allocated on heap
		return MakeStringCopy([[delegateObject getStatus] UTF8String]);
	}
	
	int _GetServiceCount ()
	{
		return [delegateObject getCount];
	}
	
	const char* _GetServiceName (int serviceNumber)
	{
		// By default mono string marshaler creates .Net string for returned UTF-8 C string 
		// and calls free for returned value, thus returned strings should be allocated on heap
		return MakeStringCopy([[[delegateObject getService:serviceNumber] name] UTF8String]);
	}
	
	void _Stop()
	{
		[serviceBrowser stop];
	}
	
}



#import <Foundation/Foundation.h>



@interface NetServiceBrowserDelegate : NSObject
{
    // Keeps track of available services
    NSMutableArray *services;
	
	// Keeps track of search status
	NSString* status; 
    BOOL searching;
}



// NSNetServiceBrowser delegate methods for service browsing
- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser;
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser;
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
			 didNotSearch:(NSDictionary *)errorDict;
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
		   didFindService:(NSNetService *)aNetService
			   moreComing:(BOOL)moreComing;
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser
		 didRemoveService:(NSNetService *)aNetService
			   moreComing:(BOOL)moreComing;

// Other methods

- (int)getCount;
- (NSNetService *)getService:(int)serviceNo;
- (NSString *)getStatus;

@end


shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon