Skip to content

Commit

Permalink
added oauth and user recent media
Browse files Browse the repository at this point in the history
  • Loading branch information
Harminder Sandhu committed May 2, 2012
1 parent fc38302 commit b17e3f6
Show file tree
Hide file tree
Showing 18 changed files with 314 additions and 14 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ build/*
*.perspective
*.perspectivev3
*.pbxuser
*.xcworkspace
*.mode1
*.mode2v3
*.mode1v3
Expand Down
14 changes: 13 additions & 1 deletion HSInstagramSample/HSInstagramSample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
F835D79E15509A840041404D /* HSMyMediaViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F835D79D15509A840041404D /* HSMyMediaViewController.m */; };
F8402EF414CA8FB6000D3F64 /* HSLocationService.m in Sources */ = {isa = PBXBuildFile; fileRef = F8402EF314CA8FB6000D3F64 /* HSLocationService.m */; };
F8402EFA14CA90B2000D3F64 /* Notifications.m in Sources */ = {isa = PBXBuildFile; fileRef = F8402EF914CA90B1000D3F64 /* Notifications.m */; };
F8402EFC14CA90FC000D3F64 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8402EFB14CA90FC000D3F64 /* CoreLocation.framework */; };
Expand All @@ -20,6 +21,7 @@
F85E081314C9552900D05107 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F85E081114C9552900D05107 /* InfoPlist.strings */; };
F85E081514C9552900D05107 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F85E081414C9552900D05107 /* main.m */; };
F85E081914C9552900D05107 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F85E081814C9552900D05107 /* AppDelegate.m */; };
F87CAFE51550BD1F00547D4D /* HSInstagramUserMedia.m in Sources */ = {isa = PBXBuildFile; fileRef = F87CAFE41550BD1F00547D4D /* HSInstagramUserMedia.m */; };
F88E402514C9641D005B41C7 /* AFHTTPClient.m in Sources */ = {isa = PBXBuildFile; fileRef = F88E400A14C9641D005B41C7 /* AFHTTPClient.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
F88E402614C9641D005B41C7 /* AFHTTPRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F88E400C14C9641D005B41C7 /* AFHTTPRequestOperation.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
F88E402714C9641D005B41C7 /* AFImageCache.m in Sources */ = {isa = PBXBuildFile; fileRef = F88E400E14C9641D005B41C7 /* AFImageCache.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
Expand All @@ -35,6 +37,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
F835D79C15509A840041404D /* HSMyMediaViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HSMyMediaViewController.h; sourceTree = "<group>"; };
F835D79D15509A840041404D /* HSMyMediaViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HSMyMediaViewController.m; sourceTree = "<group>"; };
F8402EF214CA8FB6000D3F64 /* HSLocationService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HSLocationService.h; sourceTree = "<group>"; };
F8402EF314CA8FB6000D3F64 /* HSLocationService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HSLocationService.m; sourceTree = "<group>"; };
F8402EF814CA90B1000D3F64 /* Notifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Notifications.h; sourceTree = "<group>"; };
Expand All @@ -58,6 +62,8 @@
F85E081614C9552900D05107 /* HSInstagramSample-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "HSInstagramSample-Prefix.pch"; sourceTree = "<group>"; };
F85E081714C9552900D05107 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
F85E081814C9552900D05107 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
F87CAFE31550BD1F00547D4D /* HSInstagramUserMedia.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HSInstagramUserMedia.h; sourceTree = "<group>"; };
F87CAFE41550BD1F00547D4D /* HSInstagramUserMedia.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HSInstagramUserMedia.m; sourceTree = "<group>"; };
F88E400914C9641D005B41C7 /* AFHTTPClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPClient.h; sourceTree = "<group>"; };
F88E400A14C9641D005B41C7 /* AFHTTPClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFHTTPClient.m; sourceTree = "<group>"; };
F88E400B14C9641D005B41C7 /* AFHTTPRequestOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPRequestOperation.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -219,6 +225,8 @@
F88E402414C9641D005B41C7 /* HSInstagramLocation.m */,
F8402F0014CA9B14000D3F64 /* HSInstagramLocationMedia.h */,
F8402F0114CA9B14000D3F64 /* HSInstagramLocationMedia.m */,
F87CAFE31550BD1F00547D4D /* HSInstagramUserMedia.h */,
F87CAFE41550BD1F00547D4D /* HSInstagramUserMedia.m */,
);
path = Models;
sourceTree = "<group>";
Expand All @@ -232,6 +240,8 @@
F8402F0414CAAF15000D3F64 /* HSMediaGridViewController.m */,
F8402F0614CB5505000D3F64 /* HSImageViewController.h */,
F8402F0714CB5505000D3F64 /* HSImageViewController.m */,
F835D79C15509A840041404D /* HSMyMediaViewController.h */,
F835D79D15509A840041404D /* HSMyMediaViewController.m */,
);
path = Controllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -262,7 +272,7 @@
F85E07FB14C9552900D05107 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0420;
LastUpgradeCheck = 0430;
ORGANIZATIONNAME = Pushbits;
};
buildConfigurationList = F85E07FE14C9552900D05107 /* Build configuration list for PBXProject "HSInstagramSample" */;
Expand Down Expand Up @@ -318,6 +328,8 @@
F8402F0214CA9B14000D3F64 /* HSInstagramLocationMedia.m in Sources */,
F8402F0514CAAF15000D3F64 /* HSMediaGridViewController.m in Sources */,
F8402F0814CB5505000D3F64 /* HSImageViewController.m in Sources */,
F835D79E15509A840041404D /* HSMyMediaViewController.m in Sources */,
F87CAFE51550BD1F00547D4D /* HSInstagramUserMedia.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@class HSLocationService;

@interface HSLocationsTableViewController : UITableViewController
@interface HSLocationsTableViewController : UITableViewController

@property (nonatomic, strong) HSLocationService* locationService;
@property (nonatomic, strong) NSArray* locations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "HSLocationService.h"
#import "HSInstagramLocation.h"
#import "HSMediaGridViewController.h"
#import "HSMyMediaViewController.h"

@implementation HSLocationsTableViewController

Expand Down Expand Up @@ -47,6 +48,10 @@ - (void)viewDidLoad
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.title = @"locations";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"My Photos"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(loginAction)];

[[NSNotificationCenter defaultCenter] addObserverForName:kNewLocationNotification
object:nil
Expand Down Expand Up @@ -177,4 +182,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[self.navigationController pushViewController:grid animated:YES];
}

#pragma mark - Actions

- (void)loginAction
{
HSMyMediaViewController* media = [[HSMyMediaViewController alloc] init];
[self.navigationController pushViewController:media animated:YES];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#import "HSInstagramLocationMedia.h"
#import "HSImageViewController.h"

const NSInteger kthumbnailWidth = 80;
const NSInteger kthumbnailHeight = 80;
const NSInteger kImagesPerRow = 4;
const NSInteger klocationThumbnailWidth = 80;
const NSInteger klocationThumbnailHeight = 80;
const NSInteger klocationImagesPerRow = 4;

@interface HSMediaGridViewController (private)
- (void)loadImages;
Expand Down Expand Up @@ -69,14 +69,14 @@ - (void)loadView
self.images = records;
int item = 0, row = 0, col = 0;
for (NSDictionary* image in records) {
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(col*kthumbnailWidth,
row*kthumbnailHeight,
kthumbnailWidth,
kthumbnailHeight)];
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(col*klocationThumbnailWidth,
row*klocationThumbnailHeight,
klocationThumbnailWidth,
klocationThumbnailHeight)];
button.tag = item;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
++col;++item;
if (col >= kImagesPerRow) {
if (col >= klocationImagesPerRow) {
row++;
col = 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// HSMyMediaViewController.h
// HSInstagramSample
//
// Created by Harminder Sandhu on 12-05-01.
// Copyright (c) 2012 Pushbits. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HSMyMediaViewController : UIViewController <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView* webView;
@property (nonatomic, strong) UIScrollView* gridScrollView;
@property (nonatomic, strong) NSArray* images;
@property (nonatomic, strong) NSMutableArray* thumbnails;

@property (nonatomic, strong) NSString* accessToken;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//
// HSMyMediaViewController.m
// HSInstagramSample
//
// Created by Harminder Sandhu on 12-05-01.
// Copyright (c) 2012 Pushbits. All rights reserved.
//

#import "HSMyMediaViewController.h"
#import "HSInstagramUserMedia.h"
#import "HSImageViewController.h"

const NSInteger kthumbnailWidth = 80;
const NSInteger kthumbnailHeight = 80;
const NSInteger kImagesPerRow = 4;

@interface HSMyMediaViewController ()
- (void)requestImages;
- (void)loadImages;
@end

@implementation HSMyMediaViewController

@synthesize webView = _webView;
@synthesize images = _images;
@synthesize thumbnails = _thumbnails;
@synthesize gridScrollView = _gridScrollView;
@synthesize accessToken = _accessToken;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
/*
* Uncomment to re-auth a new user on each view.
*
NSHTTPCookie *cookie;
for (cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if ([[cookie domain] compare:@"instagram.com"] == NSOrderedSame)
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
*/
}
return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
[super loadView];

self.thumbnails = [[NSMutableArray alloc] init];
self.gridScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.gridScrollView.contentSize = self.view.bounds.size;
[self.view addSubview:self.gridScrollView];

self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
self.webView.delegate = self;
NSURLRequest* request =
[NSURLRequest requestWithURL:[NSURL URLWithString:
[NSString stringWithFormat:kAuthenticationEndpoint, kClientId, kRedirectUrl]]];
[self.webView loadRequest:request];
[self.view addSubview:self.webView];

self.title = @"my photos";
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.wantsFullScreenLayout = NO;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Web view delegate

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{

if ([request.URL.absoluteString rangeOfString:@"#"].location != NSNotFound) {
NSString* params = [[request.URL.absoluteString componentsSeparatedByString:@"#"] objectAtIndex:1];
self.accessToken = [params stringByReplacingOccurrencesOfString:@"access_token=" withString:@""];
self.webView.hidden = YES;
[self requestImages];
}

return YES;
}

#pragma mark - image loading

- (void)requestImages
{
[HSInstagramUserMedia getUserMediaWithId:@"self" withAccessToken:self.accessToken block:^(NSArray *records) {
self.images = records;
int item = 0, row = 0, col = 0;
for (NSDictionary* image in records) {
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(col*kthumbnailWidth,
row*kthumbnailHeight,
kthumbnailWidth,
kthumbnailHeight)];
button.tag = item;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
++col;++item;
if (col >= kImagesPerRow) {
row++;
col = 0;
}
[self.gridScrollView addSubview:button];
[self.thumbnails addObject:button];
}
[self loadImages];
}];
}

- (void)loadImages
{
int item = 0;

for (HSInstagramUserMedia* media in self.images) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
NSString* thumbnailUrl = media.thumbnailUrl;
NSData* data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:thumbnailUrl]];
UIImage* image = [UIImage imageWithData:data];

dispatch_async(dispatch_get_main_queue(), ^ {
UIButton* button = [self.thumbnails objectAtIndex:item];
[button setImage:image forState:UIControlStateNormal];
});
});
++item;
}

}

#pragma mark - button actions

- (void)buttonAction:(id)sender
{
UIButton* button = sender;
HSImageViewController* img = [[HSImageViewController alloc] initWithMedia:[self.images objectAtIndex:button.tag]];
[self.navigationController pushViewController:img animated:YES];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Notifications.h"
#import "HSInstagram.h"
#endif
1 change: 1 addition & 0 deletions HSInstagramSample/HSInstagramSample/Notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
extern NSString* const kNewLocationNotification;
extern NSString* const kInstagramLocationsNotification;
extern NSString* const kInstagramMediaNotification;
extern NSString* const kInstagramAuthNotification;

@end
1 change: 1 addition & 0 deletions HSInstagramSample/HSInstagramSample/Notifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ @implementation Notifications
NSString* const kNewLocationNotification = @"NewLocationNotification";
NSString* const kInstagramLocationsNotification = @"InstagramLocationsNotification";
NSString* const kInstagramMediaNotification = @"InstagramMediaNotification";
NSString* const kInstagramAuthNotification = @"InstagramAuthNotification";

@end
1 change: 1 addition & 0 deletions HSInstagramSample/HSInstagramSample/Vendor/AFOAuth2Client
Submodule AFOAuth2Client added at 06acab
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

extern NSString * const kInstagramBaseURLString;
extern NSString * const kClientId;
extern NSString * const kRedirectUrl;

// Endpoints
extern NSString * const kLocationsEndpoint;
extern NSString * const kLocationsMediaRecentEndpoint;

extern NSString * const kUserMediaRecentEndpoint;
extern NSString * const kAuthenticationEndpoint;

@interface HSInstagram : AFHTTPClient

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
NSString * const kInstagramBaseURLString = @"https://api.instagram.com/v1/";
#warning Include your client id from instagr.am
NSString * const kClientId = @"INSTAGRAM-CLIENT-KEY";
#warning Include your redirect uri
NSString * const kRedirectUrl = @"http://example.com";

// Endpoints
NSString * const kLocationsEndpoint = @"locations/search";
NSString * const kLocationsMediaRecentEndpoint = @"locations/%@/media/recent";
NSString * const kUserMediaRecentEndpoint = @"users/%@/media/recent";
NSString * const kAuthenticationEndpoint =
@"https://instagram.com/oauth/authorize/?client_id=%@&redirect_uri=%@&response_type=token";

@implementation HSInstagram

Expand Down
Loading

0 comments on commit b17e3f6

Please sign in to comment.