diff --git a/yql-ios.xcodeproj/project.pbxproj b/yql-ios.xcodeproj/project.pbxproj index 78e32fb..58fe712 100644 --- a/yql-ios.xcodeproj/project.pbxproj +++ b/yql-ios.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ BB1775851631B65C00659E8A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BB1775601631B65C00659E8A /* Foundation.framework */; }; BB17758D1631B65C00659E8A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BB17758B1631B65C00659E8A /* InfoPlist.strings */; }; BB1775901631B65C00659E8A /* yql_iosTests.m in Sources */ = {isa = PBXBuildFile; fileRef = BB17758F1631B65C00659E8A /* yql_iosTests.m */; }; + BB17759B1631DC0E00659E8A /* YQL.m in Sources */ = {isa = PBXBuildFile; fileRef = BB17759A1631DC0D00659E8A /* YQL.m */; }; + BB17759C1631DC0E00659E8A /* YQL.m in Sources */ = {isa = PBXBuildFile; fileRef = BB17759A1631DC0D00659E8A /* YQL.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -58,6 +60,8 @@ BB17758C1631B65C00659E8A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; BB17758E1631B65C00659E8A /* yql_iosTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = yql_iosTests.h; sourceTree = ""; }; BB17758F1631B65C00659E8A /* yql_iosTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = yql_iosTests.m; sourceTree = ""; }; + BB1775991631DC0D00659E8A /* YQL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YQL.h; sourceTree = ""; }; + BB17759A1631DC0D00659E8A /* YQL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YQL.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -123,6 +127,8 @@ BB1775791631B65C00659E8A /* ViewController.h */, BB17757A1631B65C00659E8A /* ViewController.m */, BB1775651631B65C00659E8A /* Supporting Files */, + BB1775991631DC0D00659E8A /* YQL.h */, + BB17759A1631DC0D00659E8A /* YQL.m */, ); path = "yql-ios"; sourceTree = ""; @@ -273,6 +279,7 @@ BB17756B1631B65C00659E8A /* main.m in Sources */, BB17756F1631B65C00659E8A /* AppDelegate.m in Sources */, BB17757B1631B65C00659E8A /* ViewController.m in Sources */, + BB17759B1631DC0E00659E8A /* YQL.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -281,6 +288,7 @@ buildActionMask = 2147483647; files = ( BB1775901631B65C00659E8A /* yql_iosTests.m in Sources */, + BB17759C1631DC0E00659E8A /* YQL.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/yql-ios/ViewController.h b/yql-ios/ViewController.h index 6654d27..74e3537 100644 --- a/yql-ios/ViewController.h +++ b/yql-ios/ViewController.h @@ -7,11 +7,15 @@ // #import +#import "YQL.h" -@interface ViewController : UIViewController +@interface ViewController : UIViewController { + YQL *yql; +} @property (nonatomic, retain) IBOutlet UITextView *QueryTextView; @property (nonatomic, retain) IBOutlet UITextView *ResultsTextView; +@property (strong, nonatomic) YQL *yql; -(IBAction)Run_OnClick:(id)sender; diff --git a/yql-ios/ViewController.m b/yql-ios/ViewController.m index 4e25d33..48a3156 100644 --- a/yql-ios/ViewController.m +++ b/yql-ios/ViewController.m @@ -7,9 +7,7 @@ // #import "ViewController.h" - -#define QUERY_PREFIX @"http://query.yahooapis.com/v1/public/yql?q=" -#define QUERY_SUFFIX @"&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=" +#import "YQL.h" @interface ViewController () @@ -18,40 +16,22 @@ @interface ViewController () @implementation ViewController @synthesize QueryTextView; @synthesize ResultsTextView; +@synthesize yql; -- (void)viewDidLoad -{ +- (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. + + // Init YQL + yql = [[YQL alloc] init]; } -- (void)didReceiveMemoryWarning -{ +- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. -} - --(IBAction)backgroundTouched:(id)sender { - [QueryTextView resignFirstResponder]; - [ResultsTextView resignFirstResponder]; } -(IBAction)Run_OnClick:(id)sender { - [sender resignFirstResponder]; - - NSString *query = [NSString stringWithFormat:@"%@%@%@", QUERY_PREFIX, [QueryTextView.text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], QUERY_SUFFIX]; - - NSLog(@"Request URL: %@", query); - - NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding]; - NSError *error = nil; - NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil; - if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription); - - NSLog(@"Results: %@", [results valueForKeyPath:@"query.results"]); - - // lame :( - ResultsTextView.text = [NSString stringWithFormat:@"%@", [results valueForKeyPath:@"query.results"]]; + NSString *results = [yql query:QueryTextView.text]; + ResultsTextView.text = results; } @end diff --git a/yql-ios/YQL.h b/yql-ios/YQL.h new file mode 100644 index 0000000..056a418 --- /dev/null +++ b/yql-ios/YQL.h @@ -0,0 +1,15 @@ +// +// YQL.h +// yql-ios +// +// Created by Guilherme Chapiewski on 10/19/12. +// Copyright (c) 2012 Guilherme Chapiewski. All rights reserved. +// + +#import + +@interface YQL : NSObject + +- (NSString *)query:(NSString *)statement; + +@end diff --git a/yql-ios/YQL.m b/yql-ios/YQL.m new file mode 100644 index 0000000..d4427cd --- /dev/null +++ b/yql-ios/YQL.m @@ -0,0 +1,36 @@ +// +// YQL.m +// yql-ios +// +// Created by Guilherme Chapiewski on 10/19/12. +// Copyright (c) 2012 Guilherme Chapiewski. All rights reserved. +// + +#import "YQL.h" + +#define QUERY_PREFIX @"http://query.yahooapis.com/v1/public/yql?q=" +#define QUERY_SUFFIX @"&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=" + +@implementation YQL + +- (NSString *) query: (NSString *)statement { + NSLog(@"Start yql call"); + + NSString *query = [NSString stringWithFormat:@"%@%@%@", QUERY_PREFIX, [statement stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], QUERY_SUFFIX]; + + NSLog(@"Request URL: %@", query); + + NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error = nil; + NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil; + + if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription); + + NSLog(@"Results: %@", [results valueForKeyPath:@"query.results"]); + + NSLog(@"Done yql call"); + + return [NSString stringWithFormat:@"%@", [results valueForKeyPath:@"query.results"]]; +} + +@end