venerdì 28 agosto 2015

Ottenere dati con Json da un sito Internet in un app iOS

Si possono ottenere dati da un sito internet anche attraverso json, così da farle visualizzare ad esempio su di una tableview di un'applicazione per Iphone.
Seguendo questi spunti di codice di esempio creati con Xcode si leggono 3 elementi restituiti da una pagina php:

>> nel controller.h
@property (nonatomic, strong) NSMutableArray *jsonArray;

>> nel controller.m
#define getDataUrl @"http://www.example.com/data.php"
//...
@implementation OnlineViewController
@synthesize jsonArray;

- (void)viewWillAppear:(BOOL)animated
{
 [self retrieveData];
}
//other code.....

#pragma mark -
#pragma mark Class Methods

-(void) retrieveData{
 NSURL *url = [NSURL URLWithString:getDataUrl];
 NSData *data = [NSData dataWithContentsOfURL:url];
 jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

 for (int i =0 ; i < jsonArray.count; i++){
 NSString *cPosition = [NSString stringWithFormat:@"%@%d", @"",(i+1)];
 NSString *cScore = [[jsonArray objectAtIndex:i] objectForKey:@"score_player"];
 NSString *cName = [[jsonArray objectAtIndex:i] objectForKey:@"name_player"];
 //...other code to set value on tableview
 }
 [self.tableView reloadData];
}

lunedì 10 agosto 2015

Far scendere la tastiera per poter scrivere in un campo testo in un app iOS

Come si può far scendere o ripristinare la tastiera in uso in un campo testo di un'applicazione per iOS/Iphone?
Semplice, con questi due metodi è possibile ripristinarla al punto iniziale cliccando su di un qualsiasi punto dello schermo.
Con Xcode collegare il metodo a "Editing Did End" del campo testo.

- (void)textViewDidEndEditing:(UITextView *)textView
{
    CGAffineTransform translateUp = CGAffineTransformMakeTranslation(0.0, 0.0);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    self.view.transform = translateUp;
    [UIView commitAnimations];
}

//fieldName: UITextField defined in File.h
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([fieldName isFirstResponder] && [touch view] != fieldName) {
        [fieldName resignFirstResponder];
    }
    else if ([fieldCountry isFirstResponder] && [touch view] != fieldCountry) {
        [fieldCountry resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}

lunedì 3 agosto 2015

Far salire la tastiera per poter scrivere in un campo testo in un app iOS

Come si può far salire la tastiera per utilizzarla in un campo testo di un'applicazione per Iphone?
Semplice, con queste poche righe di codice è possibile far apparire la tastiera; con Xcode collegare il metodo a "Editing Did Begin" del campo testo:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    CGAffineTransform translateUp = CGAffineTransformMakeTranslation(0.0, -150.0);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    self.view.transform = translateUp;
    [UIView commitAnimations];
}