xcodeで追加の設定を行いました
XCode 4 (iOS) への YAJL フレームワークのインストール
* In Build Phases, make sure its listed in Link Binary With Libraries, along with:
o CoreGraphics.framework
o Foundation.framework
o UIKit.framework
* In Build Settings:
o Under Framework Search Paths make sure the (parent) directory to YAJLiOS.framework is listed.
o Under Other Linker Flags in your target, add -ObjC and -all_load
* Import with #import <YAJL/YAJL.h>.
編集済み
カスタムクラスを作成するか、任意のクラスに以下のコードを記述できますが、次のようにカスタムクラスを作成することをお勧めします:
.hファイルで言うtest.h
#import <Foundation/Foundation.h>
#import "OAuthConsumer.h"
#import <GHUnit/GHUnit.h>
#import <YAJL/YAJL.h>
@interface test : NSObject
{
NSMutableData *responseData;
NSDictionary *JSON1 ;
}
- (NSMutableDictionary *) getData ;
@end
今test.m
ファイルに
#import "test.h"
#import "OAuthConsumer.h"
@implementation test
- (void)test:(NSString *)urlString
{
NSURL *URL = [NSURL URLWithString:@"http://api.yelp.com/v2/search?term=restaurants&location=new%20york"];
OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:@"yourKey" secret:@"yourKey"] autorelease];
OAToken *token = [[[OAToken alloc] initWithKey:@"yourKey-" secret:@"yourKey-Bc"] autorelease];
id<OASignatureProviding, NSObject> provider = [[[OAHMAC_SHA1SignatureProvider alloc] init] autorelease];
NSString *realm = nil;
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:URL
consumer:consumer
token:token
realm:realm
signatureProvider:provider];
[request prepare];
responseData = [[NSMutableData alloc] init];
//[self prepare];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//[self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];
//NSDictionary *JSON = [responseData yajl_JSON];
//GHTestLog(@"JSON: %@", [JSON yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@" "]);
//NSLog(@"%@",[JSON valueForKey:@"region"]);
[connection release];
[request release];
}
- (void) setString
{
//NSMutableString *JSON = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(@"JSON Data Parsing:--->%@",JSON);
JSON1 = [responseData yajl_JSON];
NSArray *arry = [JSON1 valueForKey:@"businesses"];
for (int i = 0; i < [arry count]; i ++)
{
NSLog(@"Res Name : %@",[[arry objectAtIndex:i] valueForKey:@"name"]);
}
NSDictionary *temp = [arry objectAtIndex:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error: %@, %@", [error localizedDescription], [error localizedFailureReason]);
//[self notify:kGHUnitWaitStatusFailure forSelector:@selector(test)];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self setString];
//[self notify:kGHUnitWaitStatusSuccess forSelector:@selector(test)];
}
- (NSDictionary *) getData
{
return JSON1 ;
}
- (void)tearDown
{
[responseData release];
responseData = nil;
}
@end
お役に立てば幸いです。それは私のために働いています....