Skip to main content

Posts

Showing posts with the label objective-c

Best way to use RestKit in an iPhone Application

I am writing an iPhone application and I have finally decided to use RestKit as the framework for connecting to REST Services. The way I am thinking of building is to have the Controllers in my application be completely agnostic to RestKit. For eg. If I had a login screen, in the usual RestKit scenario (based on example programs as well as few blog entries created by the RestKit developers) you will have the controller implement the RKRequestDelegate protocol and use the RKClient to call the service in the Controller passing self ( the controller) as the delegate. I would like to hide that from the User developing the Controllers and views. What I am thinking of is the following. I will have a LoginService which will login the user. There will be protocol LoginServiceDelegate which has two methods for success and failure. And the Controller can implement the LoginServiceDelegate and call the login Method in LoginService and get a success or failure callback. However to do this,

Key-Value-Observation — Looking for a more elegant solution to respond to value changes

I've run into a frustrating feature of KVO: all notifications are funneled through a single method ( observeValueForKeyPath:.... ), requiring a bunch of IF statements if the object is observing numerous properties. The ideal solution would be to pass a method as an argument to the method that establishes the observing in the first place, but it seems this isn't possible. Does a solution exist to this problem? I initially considered using the keyPath argument ( addObserver:forKeyPath:options:context: ) to call a method via NSSelectorFromString , but then I came across the post KVO Dispatcher pattern with Method as context and the article it linked to which offers a different solution in order to pass arguments along as well (although I haven't gotten that working yet). I know a lot of people have come up against this issue. Has a standard way of handling it emerged?

returning UIImage from block

I have the following code: - (UIImage *) getPublisherLogo { //check the cache if the logo already exists NSString * imageUrl = [NSString stringWithFormat:@"%@/%@&image_type=icon", self.baseUrl, self.imageUrl_]; ASIHTTPRequest * imageRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imageUrl]]; [imageRequest setTimeOutSeconds:30.0]; [imageRequest setDownloadCache:[ASIDownloadCache sharedCache]]; [imageRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; [imageRequest setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy]; [imageRequest setCompletionBlock:^(void){ UIImage *img = [UIImage imageWithData:[imageRequest responseData] ]; if (img){ return img; } }]; [imageRequest setFailedBlock:^(void){ NSLog(@"Error in pulling image for publisher %@", [[imageReq

Adding box2d header causes a torrent of compiler errors

I am experiencing either an extremely weird error or a temporary loss of brain function. I have a box2d / cocos project running in XCode. This all works fine but my code is currently a proof of concept "hack". Stage one of cleaning up the code is creating classes for various objects. Now, if I create a brand new Objective C class (inherited off NSObject) as below it all compiles fine. As soon as I attempt to import the "Box2D.h" I receive a gazillion errors originating from the box2d library saying it cannot be built. 201 to be precise and it indicates no issues with the class itself. Can anyone shed any light? I have already tried a clean and rebuild as well as restarting XCode. My super complicated class definition #import "Box2D.h" @interface test : NSObject @end An example compiler error thrown Expected '=', ',', ';', 'asm' or '__attribute__' before 'b2Fixture' in /Users/..../libs/Box2D/Dynamics

What does the origin control do in interface builder?

In interface builder there is a control in the struts and springs inspector that is labeled origin. What does this do and why does changing it for one object change it for ALL objects? As far as I can tell it doesn't have any real effect on the frame rectangle origin as the name implies. Let me explain: Selecting a UILabel and changing the origin to be at the top right as in the photo above puts the frame origin at the point (280,11). However, in code, when you actually ask the frame for it's origin it is given as (211,11) which corresponds to the top left corner of the frame. Therefore, changing the frame origin in interface builder appears to do absolutely nothing! What is going on here?!

Is a "CFRelease” needed if I am using CGImageSourceCreateWithData from a (__bridge)?

My question is very simple, I would like to know if the method "CGImageSourceCreateWithData" creates a new object copying the data I provide so that that I have to release it when I don't need it anymore, or if it just creates a reference to the data I already have so that if I release it I will lose this data (and possibly have bad access errors). The issue is related to using (__bridge CFDataRef) as the source data. Which let's me use Core Foundation objects as toll-free in ARC mode. Consider the following function (or method, not sure how it's called): - (void)saveImageWithData:(NSData*)jpeg andDictionary:(NSDictionary*)dicRef andName:(NSString*)name { [self setCapturedImageName:name]; CGImageSourceRef source ; // Notice here how I use __bridge source = CGImageSourceCreateWithData((__bridge CFDataRef)jpeg, NULL); CFStringRef UTI = CGImageSourceGetType(source); NSMutableData *dest_data = [NSMutableData data]; // And here I use

CGContextSetShadowWithColor not working

This code should be showing a shadow, but it isn't: CGContextRef context = UIGraphicsGetCurrentContext(); //Border CGMutablePathRef outerPath = createRoundedRectForRect(self.bounds, MENU_BUTTON_OUTER_RADIUS); CGContextSetFillColorWithColor(context, [[UIColor colorWithWhite:0 alpha:0.18] CGColor]); CGContextAddPath(context, outerPath); CGContextFillPath(context); //Button UIColor *buttonColor; if (self.type == JMenuButtonTypeBlack) { buttonColor = [UIColor colorWithWhite:0 alpha:1.0]; } else if (self.type == JMenuButtonTypeWhite) { buttonColor = [UIColor colorWithWhite:0.72 alpha:1.0]; } CGRect insideRect = rectForRectWithInset(self.bounds, 3); CGMutablePathRef innerPath = createRoundedRectForRect(insideRect, MENU_BUTTON_INNER_RADIUS); CGPoint gradientTop = CGPointMake(0, insideRect.origin.y); CGPoint gradientBottom = CGPointMake(0, insideRect.origin.y + insideRect.size.height); //Base color CGContextSaveGState(context); CGContextSetFillColorWithColor(context, [buttonColor

insertSubview:belowSubview: in UITableViews

I have a UITableView with UITableViewCell s that are swipable. When a cell is swiped, I want a view to be visible (revealed) underneath that cell. Here's the code that I have: UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; _cellBack = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)]; _cellBack.backgroundColor = [UIColor whiteColor]; [self.tableView insertSubview:_cellBack belowSubview:cell]; for (int i = 0; i < [self.tableView subviews].count; i++) { UIView *v = [[self.tableView subviews] objectAtIndex:i]; if ([v isEqual:_cellBack]) { NSLog(@"cellBack %d", i); } if ([v isEqual:cell]) { NSLog(@"cell %d", i); } } In the for loop, I check to see if the views' indexes are as I expect, and indeed they are; _cellBack has an index that is one less than cell 's index. When I replace the insertSubview:belowSubview: call wit

Preloading a large image

I have an image with the dimensions of 5534 × 3803, and size of 2.4mb. The UIView references notes that: "In iOS 3.0 and later, views are no longer restricted to this maximum size but are still limited by the amount of memory they consume." When the image loads, it lags for half a second, then slides in. The image sits in the UIImageView at 1024x704, but can be scaled up to 4x that size for the purpose of my app. Are you able to preload the image in the AppDelegate? Or is there another way of working around having such a large image? Thanks EDIT: The scaling is done via UIPinchGestureRecognizer, and scales up and done (scale x4 - x1) based on the image's center point. There is no panning of the image when zoomed in.

Xcode 4.2.1 issue : Universal app runs on iPad but not on iphone

I am right now working on one universal application where I am stuck to one point. I was able to successfully launch my application both on iPhone and iPad but since 2-3 days, I am not able to launch my application on neither iPhone nor iPhone simulator but it runs smoothly on iPad/ipad simulator. Also when I run my app choosing iPhone device it shows message like Running the App on iPhone simulator but i am not able to see any effect on my simulator. I am using Xcode 4.2.1 and I guess the issue is pertains to the same. Kindly help me out to resolve the issue. Thank you.

Read plist from URL into NSMutableDictionary

I am trying to do read a PLIST from URL to a NSMutableDictionary. Code before autos = [[NSMutableArray alloc] init]; [autos addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Auto 1", @"name", @"Auto.png", @"image", @"Klassiker", @"description" , nil]]; This works. But now I want to use a PLIST. And I am trying this, this way: autos = [[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://dl.dropbox.com/u/9358444/auto.plist"]]]; Thanks. PLIST http://dl.dropbox.com/u/9358444/auto.plist

Using RestAPI calls with MGTwitterEngine+Oauth

I am using MGTwitterEngine+Oauth for twitter integration in iPhone. I need to post status and status with media on twitter.When searching I came to know that it can be possible with REST API. https://dev.twitter.com/docs/api But I did not find any links which can help how to use this api after I get authenticated with twitter. So what I did I tried using Oauth but it requires manually entering the pin which is not accepted in my case. So what I did it. I fetch information from mgtwitter engine methods:- - (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username { NSLog(@"Inside authenticatedWithUsername"); NSLog(@"%@",[[_engine tokenOFAuth] key]); NSLog(@"%@",[[_engine tokenOFAuth] pin]); NSLog(@"%@",[[_engine tokenOFAuth] secret]); [_engine getUserInformationFor:username]; } tokenOFAuth is the instance of OAToken. - (void)userInfoReceived:(NSArray *)userInfo

moving view well by UIPanGestureRecognizer but sometimes goes jumpy

i am moving views by this code, it is moving well but sometimes they are very jumpy, any idea about this ? [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations]; [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]]; CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view]; if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { firstX = [[sender view] center].x; firstY = [[sender view] center].y; } translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); [[sender view] setCenter:translatedPoint]; if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) { CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x); CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:.3