How to retrieve ip address of iphone programmatically

Cited From: http://zachwaugh.com/2009/03/programmatically-retrieving-ip-address-of-iphone/

#include <ifaddrs.h>
#include <arpa/inet.h>

- (NSString *)getIPAddress
{
  NSString *address = @”error”;
  struct ifaddrs *interfaces = NULL;
  struct ifaddrs *temp_addr = NULL;
  int success = 0;

  // retrieve the current interfaces – returns 0 on success
  success = getifaddrs(&interfaces);
  if (success == 0)
  {
    // Loop through linked list of interfaces
    temp_addr = interfaces;
    while(temp_addr != NULL)
    {
      if(temp_addr->ifa_addr->sa_family == AF_INET)
      {
        // Check if interface is en0 which is the wifi connection on the iPhone
        if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@”en0″])
        {
          // Get NSString from C String
          address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
        }
      }

      temp_addr = temp_addr->ifa_next;
    }
  }

  // Free memory
  freeifaddrs(interfaces);

  return address;
}

Blogged with the Flock Browser

Customize back bar button item title

When developing navigation controller-based apps, it’s pretty common to want to customize the title of the back button that is displayed on the navigation bar. Usually the button title is set to the parent view controller’s title, but you can customize that.

All you need to do is add some code to the viewDidLoad method in the parent view controller:

- (void) viewDidLoad
{
    [super viewDidLoad];

    self.title = @”Title goes here”;
 
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@”Back”
                            style:UIBarButtonItemStyleBordered
                            target:nil
                            action:nil];

    self.navigationItem.backBarButtonItem = backButton;   // Affect child view controller’s back button.
    [backButton release];
 }

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

Decoding a UTF-8 NSString

http://www.codza.com/decoding-utf-8-nsstring

Blogged with the Flock Browser