""

Archive for September, 2012

I was setting up facebook-phonegap plugin with my phonegap project on xcode. After everything was setup accordingly, i ran the project and many funny things happened after that. Following are the issues that happened:

1) On first attempt run, my xcode stuck on “attaching to myapp” part. I waited and waited and yeah that, but it was stuck.
2) On second attempt i stopped the process and ran the project again. This time app opened up on simulator.

Now i thought it was one time minor issue with simulator or xcode. So i began working on project. After doing my stuff i ran my project again and same above things happened. And this was happening every cycle. I couldn’t understand why xcode build and run process was getting stuck at “attaching to myapp” on first attempt and runs on second attempt after stopping the process. After all this i found third issue:

3) On Application menu of simulator there were two launcher icon for the app, one having image icon(default phonegap icon) and other was blank. When i clicked launcher with image then app closed very quickly after opening, and when i clicked launcher without image then app opened up. This was very strange and brought questions on my mind.

I spent hours and yeah lotsa hours to fix it. I searched stackoverflow for this problem and solution. After lotsa digging around it, i found the answer on one of the forum topic and it did save my life 😛
Solution is:

“Go to Project Navigator -> Select your project name(on top) -> In right pane, select project name under targets -> Select Build Phases Tab -> Expand ‘Copy Bundle Resources'”

Now if you find “Info.plist” within the list then congratulation its your lucky day, Just delete it from list, then clean the project(optional) and run it.

I really hope it works for you too.

Gracias.

This way or that way. When working on web service based application, we make a request to a service and expect a response. So this time i have something to share on this. Most common way to make a request to service is using POST. How would you send an image to service ? In my case i was using php based web service and client is iphone application. We generally send image by writing data to http message, where we mainly mention header and body of message. Our image data goes with the message body of http request. This is how normal http message
is composed in objective-c.

NSString *boundary = @"---------------------------14737809831466499882746641449";
        
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header
            
NSMutableData *body = [NSMutableData data];
    
// the body of the post
UIImage *contactImage = self.originalImage;
NSData *imageData = UIImagePNGRepresentation(contactImage);
            
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now append the image
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\";filename=\"picture.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
            
// We now need to tell the receiver what content type we have
            
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// adding the body we've created to the request
[request setHTTPBody:body];

That is straight-forward and there is nothing very different with code. Now suppose you have to send some other information data along with image using same above process, so for first and few attempt i tried to write other data to message body too but i failed. I was missing something and couldn’t get it to work. So at that time i had only one solution which was working for me. The idea was to send my other data like strings and all in query string and image data via post message. So what i did was made an query string with my name balue pairs and hit this new url via post message and post message body had my image data. Ofcourse i should have been sending everything in post body but somehow it wasn’t working since i was writing all data in some wrong way as i could send only image at that time. But definitely i found the correct way and i made it right, but i’l come to that part later. For now i want to show where exactly my problem came and where i found the solution. Now my requirement was to send json object/string representation and image to server via post. Good way to do this was again writing both json and image to post message body but i wasn’t able to write both so i decided to send json string representation with query string and image data on post message body. It was definitely working. This is how i was making my json string for my array of objects and creating query string url.

NSString *jsonStr = [self.studentArray JSONRepresentation];
NSMutableString *urlString =[NSMutableString stringWithString:urlStr];
[urlString appendString:@"?student_details="];
[urlString appendString:[NSString stringWithFormat:@"%@", [jsonStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURL *url = [NSURL URLWithString:urlString];

I was hitting this url and sending image data via post, things were going fine until one big bang. If i was making json representation of 40 students inside students array and each student object has 5 members, so json representation comes out really long and big. Sending this long very long string to query string always failed. My nsurlconnection was always getting lost. Now i was left with only one option at that time, it was that i had to send both image and json on post request message body. So after many trials i finally had it working.

responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url 
                                                                cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                            timeoutInterval:60]; 
    
[request setHTTPMethod:@"POST"];
    
NSString *boundary = @"---------------------------14737809831466499882746641449";
        
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header
            
NSMutableData *body = [NSMutableData data];
    
// the body of the post
UIImage *contactImage = self.originalImage;
NSData *imageData = UIImagePNGRepresentation(contactImage);
            
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now append the image
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\";filename=\"picture.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
            
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"student_detail\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/json\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", jsonString] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
// adding the body we've created to the request
[request setHTTPBody:body];
        
//[connection start];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self ];
    
[request release];

So that was it. Keep one thing in mind that “name=student_detail” matches with your request variable in web service, so whatever request variable you have in web service, just put that name here. You can write other data to post request message body too now.
Hope this works for you too.

Gracias.

Display Object: Origins

When i started corona, i felt as3 programming days back again. It is so much like actionscript 3. Though corona is a framework which is very similar to flash api. So here i am again to talk what i have been doing. I won’t be giving any tutorials, just wanted to share some concepts of things that i came to know when falling in trouble. In corona everything that is drawn or you see in screen are display objects. So i was creating a image display object and just rendering on screen.

local background = display.newImage("purple.png");

It gave the output something like this.

Well seems fine. First width and height of image was 480 * 480. Second, in corona, cartesian was positive 4th quadrant too, i.e top left was the origin (0,0) of a stage. So what it seemed was that image of 480*480 dimension is being rendered at point (0,0) aka top left point of image, at origin of stage. But i don’t know, what made me to print the position coordinates of that image and so i did, i found that position coordinates of image was (240, 240). I was lke wow, my assumption turned out to be wrong. So if position coordinate was (240, 240) then it meant that image was being rendered at point (240, 240) but registration point or reference point of image was definetly at center of image, beacuse come on! look at the output above. Just print the coordinates aaargh.

background.x = 0;
background.y = 0;

Output is now this, since registration point is at center of image.

So now according to documentation

display.newImage( filename [, baseDirectory] [, left, top] ) return image object; if we don't provide [left, top] then the image will be centered about its local origin.

So if you want your registration point on top left of image, then you can provide [left, top] parameter while creating or you can call setReferencePoint later.

Thanks.

I learned new concept, so this post will be about something…lets move on. I have used (retain) @property for objects before, on many tutorial codes i have seen (copy) semantics used for NSMutableArray, NSString etc. I wanted to know the difference between (retain) and (copy) and when to use on what situation? I did search online forums, tutorials etc for informaion and found that:
(retain) retains the object before assignment and takes the ownership. If i assign object (setter) and change the state of object, it will reflect on same original object that was being assigned. But (copy) creates a new(another copy) of object, if i change state then it won’t reflect on first object that was assigned but to new(another copy) object. For example:

NSMutableString *name = @"Leonard";
//@property (retain, nonatomic) NSMutableString *characterName;
self.characterName = name;
[characterName setString:@"Sheldon"];

name object will have value “Sheldon”, since characterName is taking the ownership of the object by retaining it before assignment.

//@property (retain, nonatomic) NSMutableString *characterName;

name object will remain “Leonard”, since characterName has been assigned another copy upon assignment.

There is more but i’l come to problem that i faced. So i had an mutable array member of a my class, I used (copy) semantics for creating setter and getter, so as usual i assigned an mutable array to my member (synthesizer). Now later on i was adding object to my mutable array member, and program crashed. Yes program crashed, and it was very intersting find when i saw the nature of crash. Nature of crash was that i was adding object to NSArray not NSMutableArray, and as we know NSArray is immutable which cannot be modified hence a crash. So later i found that since i am using (copy) semantics so my implicit synthesizer makes a copy instead of retaining it but the real thing is that copy returns a immutable object, even if copy is being called on mutable. So the object whom i was thinking as mutable turned up as immutable and my program crashed.

Using (retain) makes it work if u want mutable, but there is a mutablecopy also, i haven’t tried that yet but you can write your own mutator, use mutablecopy and see if it return mutable or immutable. The implicit synthesizer will always use copy and assign immutable object.

Thanks.