Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Rashii Henry
Rashii Henry
16,433 Points

Introducing API and OAuth 2.0 Video

I'm following along through the video but im getting an weird error in the console when i run my application.

2014-04-16 08:52:54.385 photo bombers[1812:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: client_id)' *** First throw call stack:

i understand that its telling my my key for client id cant be nil but Ive copied my client id from instagram exactly how it is written, I even have the exact same code as Sam in the video but this error is causing my app to crash. Any suggestions would help.

/cc Sam Soffes

2 Answers

Sam Soffes
STAFF
Sam Soffes
Treehouse Guest Teacher

Make sure that you do the SimpleAuth configuration before you create your window in App Delegate. Since the view controller uses SimpleAuth on viewDidAppear, you need to have SimpleAuth configured first.

Rashii Henry
Rashii Henry
16,433 Points

I've figured that out already. Ever since i've configured it before i create my window. Now i just get a Thread 1: SIGABRT error with no error in the console to tell me whats wrong.

Rashii Henry
Rashii Henry
16,433 Points

Here's my AppDelegate.

'''Objective-C

import "AppDelegate.h"

import "PhotosViewController.h"

import <SimpleAuth/SimpleAuth.h>

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //register simple auth and instagram client key. SimpleAuth.configuration[@"instagram"] = @{ @"client-id" : @"0e509a84fdd7449c977635079ece8a7f", SimpleAuthRedirectURIKey : @"photobombers://auth/instagram" };

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //you can assign a class to a variable so you can pass it around. created my own nav bar and set it to the root. PhotosViewController *photosViewController = [[PhotosViewController alloc]init]; UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:photosViewController]; UINavigationBar *navBar = navController.navigationBar; navBar.barTintColor = [UIColor colorWithRed:242.0/255 green:122/255 blue:87/255 alpha:1.0]; navBar.barStyle = UIBarStyleBlackOpaque;

    //set the root viewCntroller. self.window.rootViewController = navController;

    // Override point for customization after application launch. self.window.backgroundColor = [UIColor greenColor]; [self.window makeKeyAndVisible]; return YES;

}

  • (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. }

  • (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. }

  • (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. }

  • (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. }

  • (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. }

@end '''

Rashii Henry
Rashii Henry
16,433 Points

PhotosViewController.m

import "PhotosViewController.h"

import "PhotoCell.h"

import <SimpleAuth/SimpleAuth.h>

@interface PhotosViewController ()

@end

@implementation PhotosViewController

//the initializer method. returning instanceType is just returning the instance of whatever you created. -(instancetype)init{ UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init]; layout.itemSize = CGSizeMake(159, 159);//318 px layout.minimumLineSpacing = 1.0; layout.minimumInteritemSpacing= 1.0;

return (self = [super initWithCollectionViewLayout:layout]);

}

pragma mark ViewDidLoad

-(void)viewDidLoad{ [super viewDidLoad]; self.title = @"Photo Bombers";

self.collectionView.backgroundColor = [UIColor blackColor];
//set the class for the photo identifier to the PhotoCell class, so we can customize it.
[self.collectionView registerClass:[PhotoCell class] forCellWithReuseIdentifier:@"photo"];

//authorize instagram login.
[SimpleAuth authorize:@"instagram" completion:^(id responseObject, NSError *error) {
    NSLog(@"Response: %@", responseObject);
}];

// NSURL *url = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary"]; // NSURLRequest *request = [NSURLRequest requestWithURL:url]; // NSURLSession *session = [NSURLSession sharedSession]; // NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { // //response contains all the meta data. get the contents of url on disk and log it. //
// NSString *text = [NSString stringWithContentsOfURL:location encoding:NSUTF8StringEncoding error:nil]; //
//
//
// }]; //
// //start the task, or else it will be an unused variable.. // [task resume];

}

pragma mark Number Of Items In Section.

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return 10;

}

pragma mark Cell For Item At IndexPath.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];
cell.backgroundColor = [UIColor lightGrayColor];


return cell;

}

pragma mark Number Of Sections In CollectionView.

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

return 1;

}

@end