Objective C: Detect start of bouncing effect for UIScrollView

For detect bouncing of UIScrollView (or UITableView ...) using UIScrollViewDelegate:
static BOOL _draggingView = NO;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
 _draggingView = YES;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
 _draggingView = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 NSInteger pullingDetectFrom = 50;
 if (self.contentOffset.y < -pullingDetectFrom) {
  _draggingView = NO;
  NSLog(@"Pull Down");
 } else if (self.contentSize.height <= self.frame.size.height && self.contentOffset.y > pullingDetectFrom) {
  _draggingView = NO;
  NSLog(@"Pull Up");
 } else if (self.contentSize.height > self.frame.size.height && 
       self.contentSize.height-self.frame.size.height-self.contentOffset.y < -pullingDetectFrom) {
  _draggingView = NO;
  NSLog(@"Pull Up");
 }
}

Objective C: Sort array of objects

If we have array of some objects:
@interface SimpleObject : NSObject {
 NSInteger key;
 NSString *name;
        . . .
}
and we need to sort this array by "name" property of SimpleObject. Use NSSortDescriptor:
// declare array with SimpleObject instances
self.simpleObjectsArray = . . .

NSSortDescriptor *sortDescriptor =
        [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
NSArray *sortDescriptors = 
        [NSArray arrayWithObject:sortDescriptor];
// array contained all instances sorted by "name" field
NSArray *sortedArray = 
        [self.simpleObjectsArray sortedArrayUsingDescriptors:sortDescriptors];

Objective C: Bring back user to your application during/after he End the Call

Sometime you need come back to application after phone call initiated by your own application.
Exist two options (each from them not 100% that do you want), but if no choice..

  1. After starting phone call you getting local notification alert with two option
    • Cancel (stay on call screen)
    • Return (back to application during your call)
    NSURL *phoneSchemaURL = [NSURL URLWithString:@"tel://123456789"];
     
    [[UIApplication sharedApplication] openURL:phoneSchemaURL];
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (localNotification != nil) {
     NSDate *notificationDate = [[NSDate alloc] initWithTimeIntervalSinceNow: 3];
      
     localNotification.fireDate = notificationDate;
     [notificationDate release];
      
     localNotification.alertBody = @"Stay in app";
     localNotification.alertAction = @"Return";
      
     localNotification.soundName = UILocalNotificationDefaultSoundName;
      
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }
    [localNotification release];
    
  2. After ending phone call your application retrieve focus.
    NSURL *phoneSchemaURL = [NSURL URLWithString:@"tel://123456789"];
     
     NSString *osVersion = [[UIDevice currentDevice] systemVersion];
     
    if([osVersion compare: @"3.1" options: NSNumericSearch] >= NSOrderedSame ) {
     UIWebView *webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
     //or if you just want to call and back to application
     //UIWebView *webview = [[UIWebView alloc] init];
    
     [webview loadRequest:[NSURLRequest requestWithURL:phoneSchemaURL]];
      
     //if you just want to call and back to application remark code below, but don't forget release webview
     [self.view addSubview:webview];
     [webview release];
    } else {
     // On 3.0 and below, dial as usual
     [[UIApplication sharedApplication] openURL: phoneSchemaURL];          
    }
    

Objective C: Basic HTTP Authorization


  1.  First version of "Authorization":
    NSString *loginString = [NSString stringWithFormat:@"%@:%@", [self userName], [self password]];
    NSString *encodedLoginData = [Base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];  
    NSString *base64LoginData = [NSString stringWithFormat:@"Basic %@",encodedLoginData];
    
    NSURL *url=[NSURL URLWithString:@"http://my_url.com"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                    timeoutInterval:10.0];
    
    [request setHTTPMethod:@"POST"];
    
    [request setValue:base64LoginData forHTTPHeaderField:@"Authorization"];
    
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
  2. Second version of "Authorization":
    NSURL *URL = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL
                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:10.0];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    NSURLConnection Delegates
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
        if ([challenge previousFailureCount] == 0) {
            NSLog(@"received authentication challenge");
            NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER"
                                                                        password:@"PASSWORD"
                                                                     persistence:NSURLCredentialPersistenceForSession];
            NSLog(@"credential created");
            [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
            NSLog(@"responded to authentication challenge");    
        }
        else {
            NSLog(@"previous authentication failure");
        }
    }