Convert yyyy-MM-ddTHH:mm:ss.SSSZ format to default timezone

– (NSDate *) localDateStringForISODateTimeString:(NSString *) anISOString

{
NSDateFormatter *isoDateFormatter = [[NSDateFormatter alloc] init];
[isoDateFormatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”];
[isoDateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@”UTC”]];

NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@”UTC”]];

NSDate *date = [isoDateFormatter dateFromString:anISOString];
return [dateFormatter dateFromString:[userFormatter stringFromDate:date]];
}

Advertisements

NSURLConnection Usage

– (void)sendRequestToServer:(NSURL *)url httpBody:(NSString *)methodCallString
{

    NSMutableURLRequest *xmlrpcRequest =[NSMutableURLRequest requestWithURL:url
                                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                            timeoutInterval:60.0];
    [xmlrpcRequest setHTTPMethod:@”POST”];
    [xmlrpcRequest setValue:@”text/xml” forHTTPHeaderField:@”Content-Type”];
   
    // set the HTTPBody with the method call string as bytes
    const char *utfString = [methodCallString UTF8String];
   
    NSString *utfStringLenString = [NSString stringWithFormat:@”%u”, strlen(utfString)];
   
    [xmlrpcRequest setHTTPBody:[NSData dataWithBytes: utfString length:strlen(utfString)]];
   
    // Set the Content-Length header
    [xmlrpcRequest setValue:utfStringLenString forHTTPHeaderField:@”Content-Length”];
   
    // create the connection with the request and start loading the data
    if (!isConnectionBusy) {
        isConnectionBusy = YES;
        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:xmlrpcRequest delegate:self];
       
        if (theConnection) {
            responseData = [[NSMutableData data] retain];
        } else {
            NSLog(@”Connection Failed!”);
        }
    }
}

– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if (responseData != NULL)
        [responseData setLength:0];
}

– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
    //    NSString *responseString = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease];
}

– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    isConnectionBusy = NO;

    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [responseData release];
       
    // inform the user
    NSLog(@”Connection failed! Error – %@ %@”,
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

– (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    isConnectionBusy = NO;
   
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

    [connection release];
    connection = nil;
    [responseData release];

    [self parseData:responseString];
    [responseString release];
    // release the connection, and the data object
}

-(NSURLRequest *)connection:(NSURLConnection*) connection
            willSendRequest:(NSURLRequest*)    request
           redirectResponse:(NSURLResponse*) redirectResponse
{
    //NSLog(@”connection willSendRequest redirectResponse”);

    NSURLRequest *newRequest = request;
    if (redirectResponse)
    {
        newRequest=nil;
    }
    else
    {
    }
    return newRequest;
}

– (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}

Blogged with the Flock Browser

Escape code for % sign in NSString

The escape code for a percent sign is “%%”, so your code would look like this

[NSString stringWithFormat:@”%d%%”, someDigit];

This is also true for NSLog() and printf() formats.

Cited from http://stackoverflow.com/questions/739682/how-to-add-percent-sign-to-nsstring.

Blogged with the Flock Browser

Gamekit + Wifi

GKSession object will automatically handle the discovery of peers over WiFi or Bluetooth, and find those that are available on either protocol.

The documentation is stating that unlike Bluetooth, for which GKPeerPickerController provides a UI to enable Bluetooth if it’s not already enabled, GKPeerPickerController does not provide a UI to enable wifi, or select the network the user wishes to be on, you will have to provide your own.

Following example will help you

http://developer.apple.com/iphone/library/samplecode/GKRocket/Introduction/Intro.html

Blogged with the Flock Browser

UIScrollView: paging horizontally, scrolling vertically?

Cited from: http://stackoverflow.com/questions/728014/prevent-diagonal-scrolling-in-uiscrollview

// RemorsefulScrollView.h

@interface RemorsefulScrollView : UIScrollView {
  CGPoint _originalPoint;
  BOOL _isHorizontalScroll, _isMultitouch;
  UIView *_currentChild;
}
@end

// RemorsefulScrollView.m

// the numbers from an example in Apple docs, may need to tune them
#define kThresholdX 12.0f
#define kThresholdY 4.0f

@implementation RemorsefulScrollView

– (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    self.delaysContentTouches = NO;
  }
  return self;
}

– (id)initWithCoder:(NSCoder *)coder {
  if (self = [super initWithCoder:coder]) {
    self.delaysContentTouches = NO;
  }
  return self;
}

– (UIView *)honestHitTest:(CGPoint)point withEvent:(UIEvent *)event {
  UIView *result = nil;
  for (UIView *child in self.subviews)
    if ([child pointInside:point withEvent:event])
      if ((result = [child hitTest:point withEvent:event]) != nil)
        break;
   
    if ([result isKindOfClass:[UITableView class]])
    {
        return result;
    }
   
    return nil;
}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event]; // always forward touchesBegan — there’s no way to forward it later
    if (_isHorizontalScroll) {
        return; // UIScrollView is in charge now
    }
    if ([touches count] == [[event touchesForView:self] count]) { // initial touch
        _originalPoint = [[touches anyObject] locationInView:self];
    _currentChild = [self honestHitTest:_originalPoint withEvent:event];
    _isMultitouch = NO;
    }
  _isMultitouch = _isMultitouch || ([[event touchesForView:self] count] > 1);
  [_currentChild touchesBegan:touches withEvent:event];
}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  if (!_isHorizontalScroll && !_isMultitouch) {
    CGPoint point = [[touches anyObject] locationInView:self];
    if (fabsf(_originalPoint.x – point.x) > kThresholdX && fabsf(_originalPoint.y – point.y) < kThresholdY) {
      _isHorizontalScroll = YES;
      [_currentChild touchesCancelled:[event touchesForView:self] withEvent:event];
    }
  }
  if (_isHorizontalScroll) {
    [super touchesMoved:touches withEvent:event]; // UIScrollView only kicks in on horizontal scroll
    }
  else {
    [_currentChild touchesMoved:touches withEvent:event];
    }
}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  if (_isHorizontalScroll) {
    [super touchesEnded:touches withEvent:event];
        _isHorizontalScroll = NO;
    }
    else {
    [super touchesEnded:touches withEvent:event];
    [_currentChild touchesEnded:touches withEvent:event];
    }
}

– (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  [super touchesCancelled:touches withEvent:event];
  if (!_isHorizontalScroll)
    {
    [_currentChild touchesCancelled:touches withEvent:event];
        _isHorizontalScroll = NO;
    }
}

@end

Blogged with the Flock Browser

Avoiding WiFi disconnections

Avoiding WiFi disconnections

Any iPhone application that requires a WiFi connection must set UIRequiresPersistentWiFi to <true/>, otherwise the iPhone will abruptly disconnect the WiFi connection after 30 minutes of use. No warning, no error: 30 minutes and you’ll lose WiFi without this flag.

Blogged with the Flock Browser

UITableView selection not working on 3.0 Beta

Looks like it is a problem with disabling scrolling on a UITableView:

tableView.scrollEnabled=NO; // Causes selection problems

tableView.scrollEnabled=YES; // Appears to fix it.

http://forums.macrumors.com/showthread.php?t=470266

Blogged with the Flock Browser