Thursday, May 23, 2013

New app from my brother on the AppStore

Любимые сказки
Любимые сказки - Pavel Gnatyuk

Saturday, May 11, 2013

Objective-C. Input from console.

I was asked how to make a very simple console application for Mac OS X that will read the input from the console.
First thing coming to the mind is scanf - a good old thing from the standard C:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        char text[2048] = { 0 };
        printf("Type a text:");
        scanf("%s", text);
        
        NSString *str = [NSString stringWithUTF8String:text];
        NSLog(@"Text: %@", str);
    }
    
    return 0;
}

Of course everyone may use what ever he prefers: gets, getline, etc.
There is a pure Objective-C way:
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {

        NSLog(@"type a text:");
        NSFileHandle *input = [NSFileHandle fileHandleWithStandardInput];
        NSData *inputData = [input availableData];
        
        NSString *str = [[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding];
        NSLog(@"Text: %@", str);
    }
    
    return 0;
}

It allows to type a very long string in the console. The console:
2013-05-11 08:28:17.173 ConsoleInput[479:303] type a text:
Here is a text
2013-05-11 08:28:23.429 ConsoleInput[479:303] Text: Here is a text

 

Tuesday, February 5, 2013

Prompt user to rate your app

How to prompt the user to rate your app? 
Launch iTunes and show the page of your app there.
That's what the following code is doing:

NSString *reviewURL = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@"appleID];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
 
It's actually switch to another app (iTunes), and now, in order to go back to your app, the user is supposed to click on your app icon in the task bar. I'd prefer my users do not leave my app at all. :)

In iOS 6 the following code launches iTunes screen in the modal mode in my app:

SKStoreProductViewController *productController = [[SKStoreProductViewController alloc] init];
productController.delegate = (id<SKStoreProductViewControllerDelegate>)self;
NSDictionary *productParameters = @{SKStoreProductParameterITunesItemIdentifier:appleID};
[productController loadProductWithParameters:productParameters completionBlock:NULL];
        
[self presentViewController:productController animated:YES completion:nil];
The screen, showing an app in iTunes, looks so:

A demo app you can find on github.

Saturday, February 2, 2013

Xcode: few debugging commands

Few Xcode debugging commands:
1. p - evaluate an expression. For example, 2+2:

(lldb) p 2+2
(int) $0 = 4
the result is an integer 4 and this result can be used in the next expression:

(lldb) p $0 * 12
(int) $1 = 48
(lldb) 

Or even more, it allows to call a method:

(lldb) p (NSString *)[name stringByAppendingString:@", how are you?"]
(NSString *) $5 = 0x08a749c0 @"Hamlet, how are you?\200R\246"
(lldb) 


2. l - stands for 'listing'. For example, if an app stopped in a breakpoint, 'l' typed in the Xcode debugging console will show the source code around:

(lldb) l
   37      if ( [name length] == 0 ) {
   38          name = @"World";
   39      }
   40      NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", name];
   41      self.label.text = greeting;
   42  }
   43  
   44  - (BOOL)textFieldShouldReturn:(UITextField *)textField
   45  {
   46      if ( textField == [self textField] ) {
   47          [textField resignFirstResponder];
(lldb) 


3. bt 5 - backtrace - show five function calls from the stack

(lldb) bt 5
* thread #1: tid = 0x1c03, 0x00002a9b HelloWorld`-[ViewController changeGreeting:](self=0x0717ddb0, _cmd=0x0000370d, sender=0x07180ce0) + 203 at ViewController.m:37, stop reason = breakpoint 1.1
    frame #0: 0x00002a9b HelloWorld`-[ViewController changeGreeting:](self=0x0717ddb0, _cmd=0x0000370d, sender=0x07180ce0) + 203 at ViewController.m:37
    frame #1: 0x010e2705 libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 77
    frame #2: 0x000162c0 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96
    frame #3: 0x00016258 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    frame #4: 0x000d7021 UIKit`-[UIControl sendAction:to:forEvent:] + 66
(lldb) 


4. po - is short for 'print object' - a way to inspect objects:

(lldb) po name
$2 = 0x0885d130 Hamlet
(lldb) 


5. c - to continue the execution. The output in the console will be:

(lldb) c
Process 62097 resuming

Sunday, January 20, 2013

Big-endian and little-endian (C) Jonathan Swift

The terms big-endian and little-endian come from Jonathan Swift’s eighteenth-century satire Gulliver’s Travels. The subjects of the empire of Blefuscu were divided into two factions: those who ate eggs starting from the big end and those who ate eggs starting from the little end.
Source: Memory Management Programming Guide for Core Foundation

Saturday, December 1, 2012

iTunes 11. Restore the defaults

Type in Terminal:
defaults write com.apple.iTunes high-contrast-mode-enable -bool FALSE

In the previous version of iTunes, in the Preferences there was an option to use a dark grid colors - that what I used. In iTunes 11(I got the update today), this option was removed. Probably, as a result of this removal, all iPhone screens in the updated iTunes were black, including the text. The solution was to restore the defaults as it is shown above.