crino / instagram-ios-sdk

Instagram SDK for iOS
http://www.followgram.me
228 stars 63 forks source link

Tutorial: How to follow a user #45

Closed fabf98dev closed 10 years ago

fabf98dev commented 10 years ago

For everyone who has the same question that I had, I figured it out so here is the method to follow a user in the wonderful example app by @crino !

First, in IGViewController.m you have to change the login methods so that the user authorizes the app to change relationships:

At the end of -(void)viewDidLoad add the @"relationships" scope in the array of authorization:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton* loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [loginButton setTitle:@"Login" forState:UIControlStateNormal];
    [loginButton sizeToFit];
    loginButton.center = CGPointMake(160, 200);
    [loginButton addTarget:self 
                    action:@selector(login) 
          forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];

    IGAppDelegate* appDelegate = (IGAppDelegate*)[UIApplication sharedApplication].delegate;

    // here i can set accessToken received on previous login 
    appDelegate.instagram.accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    appDelegate.instagram.sessionDelegate = self;
    if ([appDelegate.instagram isSessionValid]) {
        IGListViewController* viewController = [[IGListViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
    } else {
        [appDelegate.instagram authorize:[NSArray arrayWithObjects:@"comments", @"likes",@"relationships", nil]];                //ADDED @"relationships" !
    }
}

Next, also in IGViewController.m, in the -(void)login method, do the same thing:

-(void)login {
    IGAppDelegate* appDelegate = (IGAppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate.instagram authorize:[NSArray arrayWithObjects:@"comments", @"likes",@"relationships", nil]];   ADDED @"relationships" again !

Then you can switch to the view controller where you want the user to follow another user

(WARNING: In the IGListViewController, the app crashes after running the follow method cause the "data" array changes and the table view can't show the followers anymore so I advise you to either use another view controller or to comment out all the tableView stuff in IGListViewController.m and in IGListViewController.h change "UITableViewController" to "UIViewController")

and, in the action you want, add the following code:

IGAppDelegate* appDelegate = (IGAppDelegate*)[UIApplication sharedApplication].delegate;

    NSString *accessToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"instagramAccessToken"];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"follow", @"action", accessToken , @"ACCESS_TOKEN" , nil];

    [appDelegate.instagram requestWithMethodName:@"users/123456/relationship"
                                          params:params
                                      httpMethod:@"POST"
                                        delegate:self];

Replace "123456" in the method name with the instagram ID of the user you want to follow (just go to this website to lookup any instagram id: http://jelled.com/instagram/lookup-user-id)

In order to unfollow, block, etc. a user just replace "follow" with the action from Instgaram API relationship endpoints.

Hope I forgot nothing if you still have questions or something doesn't work, just comment :)