Thursday, August 21, 2014

The new app are available in App Store

Left Brian's training: improve the game flow by click one button instead of two buttons to move to next question.

https://itunes.apple.com/us/app/left-brains-training/id477059883?mt=8

Sushi Book: fix bug that the new database is always available.

https://itunes.apple.com/us/app/sushi-book/id500375284?mt=8

SoundRec: new app to record the voice memo in the meeting, lecture, & etc.

https://itunes.apple.com/us/app/soundrec/id899045542?mt=8



Wednesday, April 3, 2013

Dismiss Numpad/keyboard when touching outside UITextField

There are two kinds of method to achieve it.

Method 1:

To add an UITapGestureRecogniser and assign it to the view, and then call the function to resign the keyboard.

in viewDidLoad:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
                                       initWithTarget:self
                                       action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];
-(void)dismissKeyboard {
       [aTextField resignFirstResponder];
}
Method 2:
to make your UIView as an instance of UIControl and then link its "Touch Up Inside" event to (IBAction)dismissKeyboard:(id)sender method. 
- (IBAction)dismissKeyboard:(id)sender {
    [aTextField resignFirstResponder];
}

Wednesday, September 12, 2012

Show progress while uploading data to Facebook

Facebook's SDK does not provide the delegate function to show the progress during uploading the data to Facebook, e.g. photo. Therefore, user has to create it by adding the following functions into FBRequest.h and FBRequest.m as well as your ViewController.

In FBRequest.h, add the below function under FBRequestDelegate protocol:

- (void)request:(FBRequest *)request didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;



In FBRequest.m, add the below function:

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
   
if ([_delegate respondsToSelector:@selector(request:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:)]) {
       
[_delegate request:self didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite];
   
} }




In your ViewController, add the below function to implement the delegate function:

- (void)request:(FBRequest *)request didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
   
NSLog(@"%d bytes out of %d sent.", totalBytesWritten, totalBytesExpectedToWrite); } 


Right now, your ViewController can receive the feedback from FBRequest when the data is being uploaded to Facebook.

Wednesday, August 1, 2012

Public, Private, and Protected Instance Variables

Classes can set instance variables as private, protected, and public. You use the compiler directives @private, @protected, and @public to declare the visiblity of instance variable.

The public directive allows any class access to the public variables.
The private directive ensures private variables are only visible to the class which declares it.
The protected directive ensures protected variables are only visible to the declaring class and its descendants.

Thursday, July 5, 2012

Alternative way to replace UIGetScreenImage()

CGImageRef screen = UIGetScreenImage();

The above code is one line of code to capture the screen, but Apple does not open the above API UIGetScreenImage() for the public app. i.e. fail to upload to Apple for approval. So the alternative way to capture screen and save it are listed as below.

- (void)captureAndSaveImage
{    
    
    // Capture screen here... and cut the appropriate size for saving and uploading
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   
    // crop the area you want 
    CGRect rect;
    rect = CGRectMake(0, 10, 300, 300);    // whatever you want
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
    UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    imageView.image = img; // show cropped image on the ImageView     
}

// this is option to alert the image saving status
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;
    
    // Unable to save the image  
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                           message:@"Unable to save image to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Dismiss" 
                                 otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                           message:@"Image saved to Photo Album." 
                                          delegate:self cancelButtonTitle:@"Ok" 
                                 otherButtonTitles:nil];
    [alert show];
    [alert release];
}


Tuesday, July 3, 2012

Base64 encode/decode method

//put it on the top of xxx.m
static char base64EncodingTable[64] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
- (void)testBase64
{

    NSString *testStr = @"000000";

    NSString *encodedStr = [self base64StringFromData:testStr];
    NSLog(@"%@", encodedStr);

    NSData *data = [self base64DataFromString:encodedStr];
    NSString decodedStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"decoded string is %@ for %@", decodedStr, testStr);

}


// encode method
-(NSString *) base64StringFromData:(NSString *)str
{
    
    NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
    int length = str.length;    
    
    unsigned long ixtext, lentext;
    long ctremaining;
    unsigned char input[3], output[4];
    short i, charsonline = 0, ctcopy;
    const unsigned char *raw;
    NSMutableString *result;
    
    lentext = [data length]; 
    if (lentext < 1)
        return @"";
    result = [NSMutableString stringWithCapacity: lentext];
    raw = [data bytes];
    ixtext = 0; 
    
    while (true) {
        ctremaining = lentext - ixtext;
        if (ctremaining <= 0) 
            break;        
        for (i = 0; i < 3; i++) { 
            unsigned long ix = ixtext + i;
            if (ix < lentext)
                input[i] = raw[ix];
            else
                input[i] = 0;
        }
        output[0] = (input[0] & 0xFC) >> 2;
        output[1] = ((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4);
        output[2] = ((input[1] & 0x0F) << 2) | ((input[2] & 0xC0) >> 6);
        output[3] = input[2] & 0x3F;
        ctcopy = 4;
        switch (ctremaining) {
            case 1: 
                ctcopy = 2; 
                break;
            case 2: 
                ctcopy = 3; 
                break;
        }
        
        for (i = 0; i < ctcopy; i++)
            [result appendString: [NSString stringWithFormat: @"%c", base64EncodingTable[output[i]]]];
        
        for (i = ctcopy; i < 4; i++)
            [result appendString: @"="];
        
        ixtext += 3;
        charsonline += 4;
        
        if ((length > 0) && (charsonline >= length))
            charsonline = 0;
    }     
    return result;
}


// decode method
- (NSData *)base64DataFromString: (NSString *)string
{
    unsigned long ixtext, lentext;
    unsigned char ch, inbuf[4], outbuf[3];
    short i, ixinbuf;
    Boolean flignore, flendtext = false;
    const unsigned char *tempcstring;
    NSMutableData *theData;
    
    if (string == nil)
    {
        return [NSData data];
    }
    
    ixtext = 0;
    
    tempcstring = (const unsigned char *)[string UTF8String];
    
    lentext = [string length];
    
    theData = [NSMutableData dataWithCapacity: lentext];
    
    ixinbuf = 0;
    
    while (true)
    {
        if (ixtext >= lentext)
        {
            break;
        }
        
        ch = tempcstring [ixtext++];
        
        flignore = false;
        
        if ((ch >= 'A') && (ch <= 'Z'))
        {
            ch = ch - 'A';
        }
        else if ((ch >= 'a') && (ch <= 'z'))
        {
            ch = ch - 'a' + 26;
        }
        else if ((ch >= '0') && (ch <= '9'))
        {
            ch = ch - '0' + 52;
        }
        else if (ch == '+')
        {
            ch = 62;
        }
        else if (ch == '=')
        {
            flendtext = true;
        }
        else if (ch == '/')
        {
            ch = 63;
        }
        else
        {
            flignore = true; 
        }
        
        if (!flignore)
        {
            short ctcharsinbuf = 3;
            Boolean flbreak = false;
            
            if (flendtext)
            {
                if (ixinbuf == 0)
                {
                    break;
                }
                
                if ((ixinbuf == 1) || (ixinbuf == 2))
                {
                    ctcharsinbuf = 1;
                }
                else
                {
                    ctcharsinbuf = 2;
                }
                
                ixinbuf = 3;
                
                flbreak = true;
            }
            
            inbuf [ixinbuf++] = ch;
            
            if (ixinbuf == 4)
            {
                ixinbuf = 0;
                
                outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
                outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
                outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);
                
                for (i = 0; i < ctcharsinbuf; i++)
                {
                    [theData appendBytes: &outbuf[i] length: 1];
                }
            }
            
            if (flbreak)
            {
                break;
            }
        }
    }
    
    return theData;

}

****************************************************
Output in console:

Decoded string is MDAwMDAw for testStr 000000;

Tuesday, June 26, 2012

php: get input and write text file, then return

Put the following codes in the server side to receive the inputs from Apps.

<?php
     
    // assign the input to the variables
    $method = $_POST['method'];
    $name = $_POST['name']; // variable to be received by server
    $id = $_POST['userID']; // variable to be received by server
    echo " user name: ".$name ." id: ". $id. " method: ". $method; // echo the received data to the Apps
    
    
    // write text file with above two variables
    $file = "file.txt";
    $fh = fopen( $file, 'w' );
    $carriageReturn = "\n";
    fwrite( $fh, $method );
    fwrite( $fh, $carriageReturn );
    fwrite( $fh, $name );
    fwrite( $fh, $carriageReturn );
    fwrite( $fh, $id );
    fclose( $fh );
   
    
    
?>