GCDAsyncSocket "didReadDataWithTag" Never called with NSOperation subclass - iphone

In a "getMyFile" method of some XIB file.
I am creating a object of class "A"(subclass of NSOperation) and adding it to a "myFileQueue"(object of NSOperationQueue).
myFileQueue.MaxConcurrentOperationCount = 1;
Problem : didReadDataWithTag" delegate NEVER called in any case.
#import "GCDAsyncSocket.h"
//and all other required classes are imported correctly
//Class : A
#interface A : NSOperation
{
{
GCDAsyncSocket* socket;
dispatch_queue_t dQueue;
BOOL isWorkDone;
}
}
#implementation A {
-main(){
#autoreleasepool {
isWorkDone = NO;
dQueue = dispatch_queue_create(#"MyDQueue", NULL);
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dQueue];
//NOTE: Even after setting delegateQueue: dispatch_get_main_queue()
//didReadDataWithTag delegate never called
NSError *err = nil;
if (![socket connectToHost:#"192.168.1.142" onPort:12345 error:&err]) // Asynchronous!
{
// If there was an error, it's likely something like "already connected" or "no delegate set"
NSLog(#"I goofed: %#", err);
}
do{
[NSThread sleep:0.2];
}while(!isWorkDone)
}
}
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
NSString* myString= #"testing";
NSData* data=[myString dataUsingEncoding: [NSString defaultCStringEncoding] ];
[socket writeData:data withTimeout:-1 tag:1];
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
[socket readDataToData:[GCDAsyncSocket ZeroData] withTimeout:-1 tag:TAG_RESPONSE_HEADER];
}
- (void)socket:(GCDAsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag
{
NSLog(#"didReadDataWithTag called...."); //this method never called in any case
isWorkDone = YES;
}
}

Related

NSOperationQueue and sending delegate to main thread

Hi i have my class Sensors where i have gps,gyroscope,accelerometer and i want o send data in delegate from nsoperationqueue:
#protocol SensorsDelegate <NSObject>
#optional
- (void)motionManagerDidAccelerateData:(CMAccelerometerData *)accelerometerData;
#end
- (void)startAccelerometr
{
if (motionManager.accelerometerAvailable) {
self.motionManager.accelerometerUpdateInterval = 1.0/10.0;
[self.motionManager startAccelerometerUpdatesToQueue:self.operationQueue
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (error) {
[motionManager stopAccelerometerUpdates];
}
else {
[self.delegate motionManagerDidAccelerateData:accelerometerData];
}
}];
}
}
How to send data to delegate which is in main thread ?? Or send acceleremoter data ?
Have you tried using
[self.delegate performSelectorOnMainThread:#selector(motionManagerDidAccelerateData:) withObject:accelerometerData waitUntilDone:NO];
Put onMainThread: method in the same class and change your code like this
- (void)startAccelerometr
{
if (motionManager.accelerometerAvailable) {
self.motionManager.accelerometerUpdateInterval = 1.0/10.0;
[self.motionManager startAccelerometerUpdatesToQueue:self.operationQueue
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (error) {
[motionManager stopAccelerometerUpdates];
}
else {
[self performSelectorOnMainThread:#selector(onMainThread:)
withObject:accelerometerData waitUntilDone:NO];
}
}];
}
}
- (void)onMainThread:(id)accelerometerData{
[self.delegate motionManagerDidAccelerateData:accelerometerData];
}
You can try:
Assuming that delegate is a property of type NSObject<SensorDelegate>* assigned at Sensor init method:
[self.delegate performSelectorOnMainThread:#selector(motionManagerDidAccelerateData:) withObject:accelerometerData waitUntilDone:NO];
Hope it helps.
Change the queue you are sending the accelerometer updates to the main queue.
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {

help with singleton's

I am trying to create a singleton User class in my app, here's the code:
#import "User.h"
#import "Login.h"
#import "SFHFKeychainUtils.h"
// Constants
static NSString* const kDBUserCurrentUserIDDefaultsKey = #"kDBUserCurrentUserIDDefaultsKey";
// Current User singleton
static User* currentUser = nil;
#implementation User
#synthesize username = _username;
#synthesize password = _password;
#synthesize delegate = _delegate;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
+ (NSString*)primaryKeyProperty {
return #"username";
}
+ (User*)currentUser {
if (nil == currentUser) {
id username = [[NSUserDefaults standardUserDefaults] objectForKey:#"kApplicationUserNameKey"];
if (!username) {
currentUser = [self new];
} else{
NSLog(#"CURRENT USER");
return self;
}
[currentUser retain];
}
return currentUser;
}
+ (void)setCurrentUser:(User*)user {
[user retain];
[currentUser release];
currentUser = user;
}
/**
* Implementation of a RESTful login pattern. We construct an object loader addressed to
* the /login resource path and POST the credentials. The target of the object loader is
* set so that the login response gets mapped back into this object, populating the
* properties according to the mappings declared in elementToPropertyMappings.
*/
- (void)loginWithUsername:(NSString*)username andPassword:(NSString*)password delegate:(NSObject<DBUserAuthenticationDelegate>*)delegate {
_delegate = delegate;
//[RKObjectManager sharedManager].client.username = username;
//[RKObjectManager sharedManager].client.password = password;
self.username = username;
self.password = password;
RKObjectMapping * userMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:#"LoginViewController"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/account/verify.json" objectMapping:userMapping delegate:self];
}
/**
* Implementation of a RESTful logout pattern. We POST an object loader to
* the /logout resource path. This destroys the remote session
*/
- (void)logout/*:(NSObject<DBUserAuthenticationDelegate>*)delegate */{
NSError * error = nil;
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:#"kApplicationUserNameKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
[SFHFKeychainUtils deleteItemForUsername:self.username andServiceName:#"convore" error:&error];
NSLog(#"LOGGING OUT");
if ([self.delegate respondsToSelector:#selector(userDidLogout:)]) {
[self.delegate userDidLogout:self];
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"DBUserDidLogoutNotification" object:nil];
}
- (void)loginWasSuccessful {
// Upon login, we become the current user
[User setCurrentUser:self];
NSError * error = nil;
// Persist the username for recovery later
[[NSUserDefaults standardUserDefaults] setValue:self.username forKey:#"kApplicationUserNameKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
[SFHFKeychainUtils storeUsername:self.username andPassword:self.password forServiceName:#"convore" updateExisting:TRUE error:&error];
// Inform the delegate
if ([self.delegate respondsToSelector:#selector(userDidLogin:)]) {
[self.delegate userDidLogin:self];
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"DBUserDidLoginNotification" object:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
NSLog(#"Loaded payload: %#", [response bodyAsString]);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object
{
if ([objectLoader wasSentToResourcePath:#"/account/verify.json"]) {
Login * login = (Login *) object;
if ([login.username length] > 0)
[self loginWasSuccessful];
}
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError*)error {
if ([objectLoader wasSentToResourcePath:#"/account/verify.json"]) {
NSLog(#"Encountered an error: %#", error);
// Login failed
if ([self.delegate respondsToSelector:#selector(user:didFailLoginWithError:)]) {
[self.delegate user:self didFailLoginWithError:error];
}
}
}
- (BOOL)isLoggedIn {
return self.username != nil;
//return self.singleAccessToken != nil;
}
- (void)dealloc {
_delegate = nil;
[_password release];
[_passwordConfirmation release];
[super dealloc];
}
#end
The issue is that whenever I tried to access currentUser it always breaks down. I first called the loginWithUsernameandPassword and then tried calling the currentUser, but when I call the currentUser on logout, it gives me an error:
calling this:
if ([[User currentUser] isLoggedIn])
gives me:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[User isLoggedIn]: unrecognized selector sent to class 0x1a671c'
seems that currentUser is nil, why is this?
Quick Singleton 101 (I wish I had this when I started, lol. Everyone just pointed me to the docs which didn't help much). The name of the singleton is going to be "Singleton"
//Singleton.h
#import <Foundation/Foundation.h>
#interface SingletonManager : NSObject
{
NSDictionary* randomDictionary; //just using a dictionary for demonstrative purposes. You can make this a string or whatever you want.
}
+ (Singleton*)sharedSingleton;
#property (nonatomic, retain) NSDictionary *randomDictionary;
#end
And now the .m
//Singleton.m
#import "Singleton.h"
static Singleton *sharedSingleton = nil;
#implementation Singleton
#synthesize randomDictionary;
#pragma mark Singleton Method
+ (Singleton*)sharedSingleton
{
#synchronized(self)
{
if(sharedSingleton == nil)
{
sharedSingleton = [[super allocWithZone:NULL] init];
}
}
return sharedSingleton;
}
#end
And to set/get, first import the singleton in whatever class you need: #import "Singleton.h", then grab the singleton with Singleton *singletonManager = [Singleton sharedSingleton]; and then you can do whatever you need to as necessary. i.e. to get the description of the NSDictionary you would call [[singletonManager randomDictionary] description];
Now this is using ARC, so if you are not you'd just have to make sure you manage your memory correctly. Enjoy.
You need to get the singleton object before you can call a method on it.
if ( [[User currentUser] isLoggedIn] ) {
// Magic happens here
}
You aren't coding your singleton properly.
+ (User *) currentUser {
#synchronized (self) {
if (currentUser == nil) {
currentUser = [[self alloc] init];
}
return currentUser;
}
}
The answer is really a combo of the two answers from XCodeDev and Matthieu Cormier. You need to "protect" your init the way the code sample says so new versions of the object are not created. Otherwise, its not a real singleton. More info on Singleton pattern.
Also, just because its a singleton doesn't mean you can access it with just class methods after you initialize it. You still need to get the instance you initialized, otherwise you cannot do operations that require certain values only in the instance.

Why cant I pass this variable from one class to another

I am stuck and need some help understanding why this is not working.
I want to be able to download the HTML of a page and then format it to show correctly, the code inside the second class (spriing) will download and display the HTML in a UITextView if it is placed inside the ViewController, however this is breaking the MVC right?
So could anyone tell me why I am getting the out of scope error on the mStringData variable?
My classes are below:
I have one class which is a view controller;
//Class for the download and processing of data from website
#import "FirstViewController.h"
#implementation FirstViewController
// The designated initializer. Override to perform setup that is required before the view is loaded.
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
// if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// // Custom initialization
//}
// return self;
//}
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
spriing = [Spriing new];
[spriing downloadData:#"http://www.spriing.co.uk/services/"];
SpriingTxt.text = spriing.mStringData;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[mRecData release];
[mStringData release];
}
And a separate class;
#import "Spriing.h"
#implementation Spriing
#synthesize mStringData;
#synthesize mRecData;
- (void)downloadData: (NSString*) URL{
mBaseURL = URL;
// Create the request.
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:mBaseURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
mCon=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (mCon)
{
// create var to store data
mRecData = [[NSMutableData data] retain];
}
else
{
// Inform the user that the connection failed.
}
}
//If the connection is reset
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//reset the data length
[mRecData setLength:0];
}
//Obtaining new data
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//Add any newly recieved data to the currently stored data
[mRecData appendData:data];
}
//If something went wrong
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//Release the connection
[mCon release];
//Release the data
[mRecData release];
//Alert the user
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Error!"
message:#"No internet connection!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alert show];
[alert release];
}
//When its done
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//NSLog(#"finished");
// Once this method is invoked, "responseData" contains the complete result
self.mStringData = [[[NSString alloc] initWithData:mRecData encoding:NSUTF8StringEncoding] retain];
//NSLog(#"%#", mStringData);
self.mStringData = [self processData:mStringData];
//NSLog(#"%#", mStringData);
//SpriingTxt.text = mStringData;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//mStringData = nil;
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
{
[mBaseURL autorelease];
mBaseURL = [[request URL] retain];
return request;
}
-(NSString*) processData: (NSString*) string
{
NSMutableString *html = [NSMutableString stringWithCapacity:[string length]];
NSScanner *scanner = [NSScanner scannerWithString:string];
NSString *tempText = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:#"<" intoString:&tempText];
if (tempText != nil)
[html appendString:tempText];
[scanner scanUpToString:#">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation] + 1];
tempText = nil;
}
return html;
}
- (void) dealloc
{
[super dealloc];
//[mStringData release];
}
#end
You are starting an asynchronous request for a URL which will take some time. Although it returns immediately, it doesn't imply that the data has been download. NSURLRequest's delegate will be notified when the data has finished downloading. It is not until then that there is data in mStringData which is probably nil prior to being assigned the downloaded data. So when you do SpriingTxt.text = spriing.mStringData; immediately after an asynchronous request without the data being downloaded, SpriingTxt.text is assigned nil.
To resolve this, you can either make a synchronous request which will block until the data has been downloaded which is generally a bad idea or you can message via delegates or notifications to your view controller when the data of your asynchronous request has been downloaded.
To implement the delegate
Delegates are implemented using protocols. You will create a delegate property in the delegating object which would be Spriing as it will let the delegate know when the string has been downloaded and the view controller will be its delegate as it wants to know when the data is available so that it can update its view. Delegates are usually not retained as most times it is the object that creates them that becomes its delegate. So retaining the delegate would create a retain cycle in such instances. There are lots of tutorials about creating the delegates. A rough implementation would be,
in Spriing.h
#protocol SpriinDelegate;
#interface Spriing:... {
id<SpriingDelegate> delegate;
...
}
#property (nonatomic, assign) id<SpriingDelegate> delegate;
...
#end
#protocol SpriingDelegate
- (void)spriing:(Spriing*)aSpriing didFinishDownloadingString:(NSString*)aString;
#end
in Spriing.m
#implementation Spriing
#synthesize delegate;
...
//When its done
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
...
self.mStringData = [self processData:mStringData];
if ( self.delegate && [self.delegate respondsToSelector:#selector(spriing:didFinishDownloadingString:)]) {
[self.delegate spriing:self didFinishDownloadingString:self.mStringData];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
...
#end
in the view controller,
- (void)viewDidLoad {
[super viewDidLoad];
spriing = [Spriing new];
spriing.delegate = self;
[spriing downloadData:#"http://www.spriing.co.uk/services/"];
}
- (void)spriing:(Spriing*)aSpriing didFinishDownloadingString:(NSString*)aString {
SpriingText.text = aString;
}
...

Google Analytics tracking doesn't work for me on ios and my GANTrackerDelegate never gets called

I've implemented google analytics in my ios app but nothing ever gets logged to analytics. I've been running it for a month, so I know there's something that's not working.
I added a GANTrackerDelegate to trace why my dispatch calls are failing, but it never gets called. I know dispatch is getting called. Does anyone know? Here's my class.
#import <Foundation/Foundation.h>
#import "GANTracker.h"
#interface trackerDelegate : NSObject<GANTrackerDelegate> {
}
#end
#import "trackerDelegate.h"
#implementation trackerDelegate
#pragma mark GANTrackerDelegate
- (void)trackerDispatchDidComplete:(GANTracker *)tracker
eventsDispatched:(NSUInteger)eventsDispatched
eventsFailedDispatch:(NSUInteger)eventsFailedDispatch {
NSLog(#"events dispatched: %d, events failed: %d", eventsDispatched, eventsFailedDispatch);
}
#end
Here's my tracker class. Note, I can trace all of these things getting called, and I know that dispatch is called repeatedly and returns 'YES', but the delegate routine never gets called. It might be a coding thing, but I'm trying to see if the dispatch calls are failing or not. The id I'm using is valid because it works for Android.
tracker.h
#interface Tracker : NSObject {
}
+ (void) startTracking;
+ (void) endTracking;
+ (void) dispatch;
+ (void) trackPageView : (NSString *) pageId;
+ (void) trackEvent : (NSString *) categoryId
: (NSString *) actionID
: (NSString *) labelID
: (int) tvalue;
#end
tracker.m
#import "Tracker.h"
#import "trackerDelegate.h"
#implementation Tracker
static BOOL trackingOn = false;
static BOOL dirty = false;
trackerDelegate *tg = nil;
+ (void) startTracking
{
if (trackingOn){
return;
}
#try{
if (!tg)
{
tg = [[trackerDelegate alloc] init];
}
[[GANTracker sharedTracker] startTrackerWithAccountID:#"UA-VALID-GOOGLEID"
dispatchPeriod:-1
delegate:tg];
trackingOn = true;
}
#catch (NSException*)
{
trackingOn = false;
}
}
+ (void) endTracking
{
#try{
if (trackingOn)
{
[[GANTracker sharedTracker] stopTracker];
}
trackingOn = false;
}
#catch (NSException *){
trackingOn = false;
}
}
+ (void) dispatch
{
if (!dirty){
return;
}
[self startTracking];
#try
{
if (![[GANTracker sharedTracker] dispatch]) {
trackingOn = false;
NSLog(#"Google anaytics dispatch failed");
return;
}
dirty = false;
}
#catch (NSException *){
trackingOn = false;
}
}
+ (void) trackPageView : (NSString *) pageId
{
[self startTracking];
#try{
if (trackingOn){
NSError *error;
if (![[GANTracker sharedTracker] trackPageview:pageId
withError:&error]) {
trackingOn = false;
NSLog(#"Google anaytics track pageview failed");
return;
}
dirty = true;
}
}
#catch (NSException *){
trackingOn = false;
}
}
+ (void) trackEvent : (NSString *) categoryId
: (NSString *) actionID
: (NSString *) labelID
: (int) tvalue
{
[self startTracking];
#try
{
if (trackingOn){
NSError *error;
if (![[GANTracker sharedTracker] trackEvent:categoryId
action:actionID
label:labelID
value:tvalue
withError:&error])
{
trackingOn = false;
NSLog(#"Google anaytics track event failed");
return;
}
dirty = true;
}
}
#catch (NSException *){
trackingOn = false;
}
}
#end
Muchas Gracias!!
First of all you should initiate the Google Analytics account using the Web-property ID given to you at the time of creating an account in Google Analytics site.
Then you must add the following code to start the tracking and set the dispatch time. Then only you could view the analytics count in your account in Google Analytics online account. Here is the initiating code for that.
[[GANTracker sharedTracker]startTrackerWithAccountID:#"UA-xxxxxx-yy"
dispatchPeriod:10
delegate:nil];
You should also make sure, you have included the following code in the dealloc in the AppDelegate.
[GANTracker sharedTracker] stopTracker];
Hope this will work fine. Thanks.

iPhone read image from socket

I am working in a little app for iphone base on ideas used to make an Android app.
To test, obviously i use the simulator, but the simulator don't have support for built-in camera. The Android idea to test this consist in use a WebCamBroadcaster Java app in the desktop to capture frames from built-in webcam and pass it through socket. Then in the app you just read the bytes and convert to image.
Well i was trying to do the same thing with iPhone Simulator. Searching in the web a found a class to work with asynchronous sockets (cocoaasyncsocket). But i can't make it work.
The Java App send the frames like this:
socket = ss.accept();
BufferedImage image = videoCapture.getNextImage();
if (image != null) {
OutputStream out = socket.getOutputStream();
if (RAW) {
image.getWritableTile(0, 0).getDataElements(0, 0, w$
image.releaseWritableTile(0, 0);
DataOutputStream dout = new DataOutputStream(new Bu$
out));
for (int i = 0; i < data.length; i++) {
dout.writeInt(data[i]);
}
dout.close();
} else {
ImageIO.write(image, "JPEG", out);
}
}
The Android version of this use C code to implement de socket reading proccess like this:
long read_count, total_read = 0;
while (total_read < readBufSize)
{
read_count = read(sockd, &readBuf[total_read], readBufSize);
if (read_count <= 0 || errno != 0)
{
char buffer[100];
sprintf(buffer, "socket read errorno = %d", errno);
LOGV(buffer);
break;
}
total_read += read_count;
}
// If we read all of the data we expected, we will load the frame from the p$
if (total_read == readBufSize){
frame = loadPixels(readBuf, width, height);}
Where readBufsize = width*height*sizeof(int);
readBuf = (char*)malloc(readBufSize);
So i try to implement the same for iPhone but i have an error in the connection (errno = 2).. Then i find cocoaasyncsocket and i try to use but i have an unknown error and nothing is read:
#import <Foundation/Foundation.h>
#import "AsyncSocket.h"
#interface Captura : NSObject {
NSString *ipserver;
UInt16 port;
NSError *errPtr;
AsyncSocket *socket;
NSMutableData *socketData;
}
#property (nonatomic,retain) NSString *ipserver;
#property (retain) AsyncSocket *socket;
#property (retain) NSError *errPtr;
//will contain de data read from socket
#property (retain) NSMutableData *socketData;
-(id)initWithIp:(NSString*)ip puerto:(UInt16)p;
-(BOOL)open;
-(void)close;
-(void)beginRead;
- (UIImage*)getImage;
#end
and the implementation
#import "Captura.h"
#implementation Captura
#synthesize ipserver;
#synthesize socket;
#synthesize errPtr;
#synthesize socketData;
-(id)initWithIp:(NSString*)ip puerto:(UInt16)p{
if (self = [super init]) {
ipserver = ip;
port = p;
socket = [[AsyncSocket alloc] initWithDelegate:self];
socketData = [[NSMutableData alloc] init];
}
return self;
}
//Connect
-(BOOL)open{
return [socket connectToHost:ipserver onPort:port error:&errPtr];
}
-(void)beginRead{
NSLog(#"Begin Read");
NSUInteger offset = [socketData length];
[socket readDataWithTimeout:1
tag:0];
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(#"Conectado al servidor");
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSLog(#"Data leida %u",[data length]);
[socketData appendData:data];
[self beginRead];
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock{
[socketData release];
[ipserver release];
[socket release];
NSLog(#"MutableData length %u", [socketData length]);
NSLog(#"Socket Desconectado");
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{
NSLog(#"OcurriĆ³ un error desconectando.... %#",err);
}
- (UIImage*)getImage{
NSData *data;
[socketData getBytes:data length:320*480*sizeof(int)];
NSLog(#"Data obtenida %#",[data length]);
if ([socketData length]>320*480*sizeof(int)) {
[socketData replaceBytesInRange:NSMakeRange(0,320*480*sizeof(int)) withBytes:NULL length:0];
}
if (data!=nil && [data length]) {
UIImage *img = [[UIImage alloc] initWithData:data];
[data release];
return img;
}
[data release];
return nil;
}
#end
Well this code connect to the server and initialize the reading process and then close up.. socket is disconnect and the app is close.
i can't test de getImage method yet...
Some idea?
Thanks in advance...
I think you need a call to -beginRead in -onSocket:didConnectToHost:port: