Thursday 8 March 2012

how to fetch facebook contacts,,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    nav.navigationBar.barStyle = UIBarStyleBlack;
   
    [self.window addSubview:nav.view];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    facebook = [[Facebook alloc] initWithAppId:@"181376618640260" andDelegate:self];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    if (![facebook isSessionValid]) {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_likes",
                                @"read_stream",
                                @"friends_about_me",
                                @"user_about_me",
                                @"user_birthday",
                                @"friends_birthday",
                                nil];
        [facebook authorize:permissions];
        [permissions release];
    }
    strFBUrl = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends?access_token=%@&fields=id,name,picture,first_name,birthday",facebook.accessToken];
    NSURL *jsonURL = [NSURL URLWithString:strFBUrl];
    
    arrContacts = [[NSMutableArray alloc] initWithArray:[[self getJsonData:jsonURL] objectForKey:@"data"]];   
 
    return YES;
}



- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
   
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [facebook handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [facebook handleOpenURL:url];
}

-(NSMutableDictionary*)getJsonData :(NSURL *)jsonURL {
   
    NSMutableDictionary *dict = [[[NSMutableDictionary alloc] init] autorelease];
    NSError *err;
    NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL usedEncoding:nil error:&err];
           
        if(jsonData == nil){
            //NSLog(@"Data NIL.....");
        }
        else{
            SBJSON *json = [[SBJSON alloc] init];
            NSError *error = nil;
            dict = [json objectWithString:jsonData error:&error];
            [json release];
        }
        [jsonData release];
   
   
    return dict;
}




// to disply facebook contacts in tableview



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80.0;
}

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [appdel.arrContacts count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        // Show disclosure only if this view is related to showing nearby places, thus allowing
        // the user to check-in.
      
        }
    NSMutableDictionary *dictTemp = [appdel.arrContacts objectAtIndex:indexPath.row];
   
    cell.textLabel.text = [dictTemp objectForKey:@"name"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 2;
    // If extra information available then display this.
        cell.detailTextLabel.text = [dictTemp objectForKey:@"birthday"];
        cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
        cell.detailTextLabel.lineBreakMode = UILineBreakModeCharacterWrap;
        cell.detailTextLabel.numberOfLines = 2;
   
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dictTemp objectForKey:@"picture"]]]];
    cell.imageView.image = image;
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Only handle taps if the view is related to showing nearby places that
    // the user can check-in to.
    }


No comments:

Post a Comment