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.

    // 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 );

php: return data in json

In server side, the following code can return the data in JSON format to client(Apps).


    $name = "Ben";
    $id = "id1234";
    $method = "get data";
    // build result in json format
    $arr = array("result" => "success",
                 "uid" => $id,
    echo json_encode($arr);

php: generates Token with random string

The following code generate the random string with certain length to the client. It can be used to create the token and send to client's apps.

    // generate random string
    function random_gen($length)
        $random= "";
        $char_list .= "abcdefghijklmnopqrstuvwxyz";
        $char_list .= "1234567890";
        // Add the special characters to $char_list if needed
        for($i = 0; $i < $length; $i++)  
            $random .= substr($char_list,(rand()%(strlen($char_list))), 1);  
        return $random;
    $random_string = random_gen(30); //This will return a random 30 character string
    echo "New Token: ".$random_string;

Monday, June 25, 2012

Distribute beta App over the air

Now it is not necessary to send ipa file and distribution profile to beta tester for Apps testing. There is a distribution OTA method, the apps can be uploaded to server and let tester to install it from server.

1. to create a provisioning distribution profile for Ad Hoc build with the selected iDevices (devices' UDID are registered in the portal).
2. in Xcode, associate this provisioning distribution profile with your build. In the Code Signing section choose the new Provisioning Profile.
3. Build and Archive.
4. Click on the Share button, and choose Distribute for Enterprise – fill in the URL to the location where you plan to host the application.
5. Upload ipa, plist and icon files to the server you typed in step 4.
6. Build the HTML to let iDevice to access and install the app.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  <title>OTA Test App</title> 
    <br><font size = "20">You are invited to test new app.</font></br>
    <br><<font size="15"><a href="itms-services://?action=download-manifest&url=http://www.yourwebsite.com">Tap Here to Install the Application</a></font>

Monday, June 18, 2012

No previous prototype for function

How to handle the Xcode warning “no previous prototype for function…”?

Go to project's Build Settings. Search for "prototype". There is an option called "Missing Function Prototypes" under Apple LLVM complier 3.1 warning; disable it. 
Optionally, you can also do this to the specific target(s) in question.

Sunday, June 17, 2012

Label with dynamic height

The below code creates the dynamic height to fit for the inputed text

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 300, 40)];
label.backgroundColor = [UIColor blackColor];
[self.view addSubview:label];
NSString *text = @"This is label with dynamic height";
label.text = text;
label.lineBreakMode = UILineBreakModeWordWrap;
// resize the height to fit the content
CGSize newLabelSize = [text sizeWithFont:label.font 
CGRect newFrame = instructions.frame;

if (newLabelSize.height > 40) {
      newFrame.size.height = newLabelSize.height;
} else {
      newFrame.size.height = 40
label.frame = newFrame;
label.numberOfLines = 0;

NSLog(@"Label height is %f", label.frame.size.height);

Thursday, June 14, 2012

Error: more than max 5 filtered album trying to register, this will fail

Per the subject, the following code may help to solve it.
I tried in my project, it seems work.

- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [self setModalInPopover:YES];}

Tuesday, June 12, 2012

Limit the number of characters in UITextView

#define MAX_LENGTH 50

// UITextView delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {    
    NSLog(@"type %d char", myTextView.text.length);    
    if (myTextView.text.length >= MAX_LENGTH && range.length == 0)
        NSLog(@"max length is reached");
        return NO; // return NO to not change text

Tuesday, June 5, 2012

Limit the number of lines in UITextView

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
    // limit the number of lines in textview
    NSString* newText = [mytextView.text stringByReplacingCharactersInRange:range withString:text];

    // pretend there's more vertical space to get that extra line to check on
    CGSize tallerSize = CGSizeMake(mytextView.frame.size.width-15, mytextView.frame.size.height*2); 

    CGSize newSize = [newText sizeWithFont:mytextView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    if (newSize.height > mytextView.frame.size.height)
        NSLog(@"two lines are full");
        return NO;
    // dismiss keyboard and send comment
    if([text isEqualToString:@"\n"]) {
        [mytextView resignFirstResponder];
        return NO;
    return YES;