Dropbox Api in iphone - iphone

I am trying to integrate dropbox with iphone and done with login option.I want to load folders and files on root path in table view just after login.
code for this is
- (void)viewWillAppear:(BOOL)animated {
[self.restClient loadMetadata:#"/"];
}
This loadMetadata is method in DBRestClient.m class.I managed to print json value on console
Code is
- (void)parseMetadataWithRequest:(DBRequest*)request {
NSAutoreleasePool* pool = [NSAutoreleasePool new];
NSDictionary* result = (NSDictionary*)[request resultJSON];
DBMetadata* metadata = [[[DBMetadata alloc] initWithDictionary:result] autorelease];
[self performSelectorOnMainThread:#selector(didParseMetadata:) withObject:metadata waitUntilDone:NO];
NSLog(#"Meta data is :%#",result);
[pool drain];
}
How could I use this result value in my view controller to display this in tableview?

Get the contents of a directory and put them into an array.
- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata
{
for (DBMetadata* child in metadata.contents)
{
if (child.isDirectory)
{
[self.restClient loadMetadata:child.path withHash:hash];
}
else
{
if ( [self.directory isEqualToString:#"a directory"] ) {
/* child.path is the file, so put it into an array */
}
}
}
}
Than load your array into your UITableView as you would any other NSArray / NSMutableArray

Related

How to write OCUnit Test Case

I want to write unit tests using Apple's default SenTestingKit for the below method:
- (NSDictionary*)getValueTags {
return _tags;
}
- (NSString*)getFlag {
NSString* jo = #"";
for (NSString* key in _tags) {
jo = [jo stringByAppendingFormat:#"%#=\"%#\"&", key, [_tags objectForKey:key]];
}
if ([jo length] > 0) {
jo = [jo substringToIndex:[jo length] - 1];
}
return jo;
}
I used default SenTesting
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
-(void)testValueTags{
}
-(void)testGetFlag{
}
I am new to writing TestCases, I need some guideline for sample methods to write test cases
A test case has four distinct phases:
set up
exercise
verify
tear down
Some of these phases can be empty. For example, most tear down happens automatically if you use ARC.
When you're starting, don't put anything into the setUp or tearDown methods. Just write a single unit test. Here's a worked example. (I'm going to change the names, because Objective-C idiom is not to use the word "get". So instead of getFlag let's just call it flag.) I'm going to call the class `Example, and I'll use ARC. And I use the abbreviation "sut" for "system under test".
- (void)testFlagGivenOneEntry
{
// set up
Example *sut = [[Example alloc] init];
[sut setTags:#{ #"key1" : #"value1" }];
// execute & verify
STAssertEqualObjects([sut flag], #"key1=\"value1\"", nil);
}
That's one test. Let's add another.
- (void)testFlagGivenTwoEntries
{
// set up
Example *sut = [[Example alloc] init];
[sut setTags:#{ #"key1" : #"value1",
#"key2" : #"value2" }];
// execute & verify
STAssertEqualObjects([sut flag], #"key1=\"value1\"&key2=\"value2\"", nil);
}
At this point, we have duplicate code: the creation of the sut. Now we can promote the variable up to an instance variable of the class. Then we create it in setUp and destroy it in tearDown:
#interface ExampleTest : SenTestCase
#end
#implementation ExampleTest
{
Example *sut;
}
- (void)setUp
{
[super setUp];
sut = [[Example alloc] init];
}
- (void)tearDown
{
sut = nil;
[super tearDown];
}
- (void)testFlagGivenOneEntry
{
[sut setTags:#{ #"key1" : #"value1" }];
STAssertEqualObjects([sut flag], #"key1=\"value1\"", nil);
}
- (void)testFlagGivenTwoEntries
{
[sut setTags:#{ #"key1" : #"value1",
#"key2" : #"value2" }];
STAssertEqualObjects([sut flag], #"key1=\"value1\"&key2=\"value2\"", nil);
}
#end
For a more involved example, see Objective-C TDD: How to Get Started.

NSValueTransformer, inserting NULL into core data

I'm having a problem with NSValueTransformer, instead of transforming the value (NSString) it's putting NULL into the database where the attributes are transformable. I'm using the apple example, which is the same are everyone else's example and I just can't get it to work on a simple level. I'm also putting breakpoints at transformedValue and reverseTransformedValue, but they never get hit.
My custom class
+ (void)registerValueTransformer {
[NSValueTransformer setValueTransformer:[[[self alloc] init] autorelease] forName:#"myTransformerTest"];
}
#pragma mark -
#pragma mark NSValueTransformer implementation
+ (BOOL)allowsReverseTransformation {
return YES;
}
+ (Class)transformedValueClass {
return [NSString class];
}
- (id)transformedValue:(id)value {
return #"Test in";
}
- (id)reverseTransformedValue:(id)value {
return #"Test Out";
}
And this in my app delegate
+(void) initialize
{
[super initialize];
[transformerTest registerValueTransformer];
}
And then in Core data I've made my attribute transformable with a name "myTransformerTest". And according to the apple documentation that should be it.

how to stop creating object Instance when its not need

The class do have
-(void) trackByPage : (NSString*) pageName {
TrackPage *track_p;
= [[TrackPage alloc] init];
track_p.page1 = #"welcome";
track_p.page2= self.String1;
[track_p release];
}
I am accessing from controller class this method.
- (void)viewDidLoad {
[super viewDidLoad];
TrackPageMeasurement *trackPage_Measurement = [[TrackPageMeasurement alloc]init];
[trackPage_Measurement trackByPage:#"Msg"];
[trackPage_Measurement release];
}
- (void)selectedDataValue {
TrackPageMeasurement *trackPage_Measurement = [[TrackPageMeasurement alloc]init];
[trackPage_Measurement trackByPage:#"DataValue"];
[trackPage_Measurement release];
}
I am accessing that through another class. trackByPage. by passing string ..
Each time i am accessing each time object instance is created how to stop those thing.
selectedDataValue shouldn't be calling [super viewDidLoad]; The code doesn't exactly inspire me with confidence; it looks to me more like that you want to retrieve some tracking object rather than creating a new one each time. Do you know what a singleton is?
Using a singleton would look more like:
TrackPage *track_p = [TrackPage instance];
track_p.page1 = #"welcome";
track_p.page2 = self.String1;
How about
TrackPage *track_p;
if(track_p==nil)
{
track_p= [[TrackPage alloc] init];
track_p.page1 = #"welcome";
track_p.page2= self.String1;
}
[track_p release];

Passing an object between 2 view controllers not working

I'm trying to pass 3 objects to another VC but they are showing up as Null in the destination VC.
VC1:
- (void)specificExerciseTableViewController:(SpecificExerciseTableViewController *)specificExerciseTableViewController didSelectSpecificExerciseWithURL:(NSString *)exerciseURL muscleName:(NSString *)muscleName muscleURL:(NSString *)muscleURL;
{
[self addExercise];
}
-(void)addExercise
{
PFObject *exerciseInRoutine = [[PFObject alloc] initWithClassName:#"exerciseInRoutine"];
[exerciseInRoutine setObject:self.selectedExercise forKey:#"name"];
[exerciseInRoutine setObject:self.muscleName forKey:#"muscle"];
[exerciseInRoutine setObject:self.muscleURL forKey:#"picture"];
[exerciseInRoutine saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self.tableView reloadData];
} else {
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
The class that is passing the objects to VC1:
if ([self.delegate respondsToSelector:#selector(specificExerciseTableViewController:didSelectSpecificExerciseWithURL:muscleName:muscleURL:)])
{
NSString *exerciseName = [[self.exerciseArray objectAtIndex:selectedRowIndex.row] objectForKey:#"exerciseName"];
[self.delegate specificExerciseTableViewController:self didSelectSpecificExerciseWithURL:exerciseName muscleName:self.muscleName muscleURL:self.muscleURL];
[self dismissModalViewControllerAnimated:YES];
}
Edit:
I've updated my method to set the objects to the VC's properties, but have same problem:
- (void)specificExerciseTableViewController:(SpecificExerciseTableViewController *)specificExerciseTableViewController didSelectSpecificExerciseWithURL:(NSString *)exerciseURL muscleName:(NSString *)muscleName muscleURL:(NSString *)muscleURL;
{
self.muscleName = exerciseURL;
self.muscleName = muscleName;
self.muscleURL = muscleURL;
[self addExercise];
}
What do you expect to happen when you execute the code shown above?
You second code block calls -specificExerciseTableViewController:didSelectSpecificExerciseWithURL:muscleName:muscleURL: on some delegate but the implementation of that method in your "VC1" example ignores the arguments passed to that method. You don't seem to be doing anything with the data you are given.
The problem is you are passing arguments to the delgate object.but inside the delegate method you didnt either pass the arguments to the function addExercise or assign the parameters to your properties before you call the metho.

RestKit with Three20 Integration

I'm trying to work with Restkit to call my web server api, but things are not working.
My controller just show the activity indicator and nothing happens.
I have an api call which suppose to return the top 50 videos ,for example:
http://example.com/services/getTop50Video
The return is in the format of :
<results>
<mysql_host>72.9.41.97</mysql_host>
<results>
<title/>
<views/>
<video_id>j2xFxHgENt4</video_id>
<thumbnail>http://img.youtube.com/vi/j2xFxHgENt4/2.jpg</thumbnail>
<url/>
</results>
...
</results>
My App delegate Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Configure RestKit Object Manager
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:#"http://example.com/services"];
RKObjectMapper* mapper = objectManager.mapper;
[mapper registerClass:[YouTubeVideo class] forElementNamed:#"video"];
// Other non relevant stuff
}
TWYouTubeVideo Class :
#implementation TWYouTubeVideo
#synthesize title = _title;
#synthesize numberOfViews = _numberOfViews;
#synthesize videoID = _videoID;
#synthesize thumbnailURL = _thumbnailURL;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
#"title", #"title",
#"views", #"numberOfViews",
#"video_id", #"videoID",
#"thumbnail", #"thumbnailURL",
nil];
}
My Controller code :
-(id) initWithResourcePath:(NSString*) requestedResourcePath
{
if (self = [super init])
{
self.resourcePath = requestedResourcePath;
}
return self;
}
- (void)createModel {
self.model = [[[RKRequestTTModel alloc] initWithResourcePath:self.resourcePath] autorelease];
}
- (void)didLoadModel:(BOOL)firstTime {
[super didLoadModel:firstTime];
RKRequestTTModel* model = (RKRequestTTModel*)self.model;
NSMutableArray* items = [NSMutableArray arrayWithCapacity:[model.objects count]];
for (YouTubeVideo* video in model.objects) {
TableSubtitleItem *item = [TableSubtitleItem itemWithText:video.title
subtitle:[NSString stringWithFormat:#"%# views", video.numberOfViews]
imageURL:video.thumbnailURL
defaultImage:[YouTubeVideo defaultThumbnail]
URL:nil
accessoryURL:nil];
[items addObject:item];
}
// Ensure that the datasource's model is still the RKRequestTTModel;
// Otherwise isOutdated will not work.
TTListDataSource* dataSource = [TTListDataSource dataSourceWithItems:items];
dataSource.model = model;
self.dataSource = dataSource;
}
And pushing the Controller:
SecondYouTubeController* viewController = [[SecondYouTubeController alloc] initWithResourcePath:#"/getTop50VideoXML?json=true"];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
First, I guess I need to somehow tell the parser that the video objects appear inside a "response" node.
Second, I'm not sure I'm doing all the calls as should.
I would really appreciate help here.
You can drill down into the "responses" element by setting the keyPath property on your RKRequestTTModel to "responses".
Is your response actually XML? Currently RestKit doesn't support XML payloads (it's on the roadmap to get working again). Can you get a JSON payload? If not, and you feel like fighting the good fight, all you need to do is implement the RKParser protocol that can turn XML to/from Cocoa objects (Dictionaries and Arrays).
I found this question looking to get XML working with RestKit 0.20.
At the time of the answer above it wasn't possible - it now is via an additional module:
https://github.com/RestKit/RKXMLReaderSerialization
You can make use of it by adding this to your Podfile:
pod 'RKXMLReaderSerialization', :git => 'https://github.com/RestKit/RKXMLReaderSerialization.git', :branch => 'master'
And registering it for use thus:
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:#"application/xml"];