iosobjective-cuiimageviewsalesforcesalesforce-chatter

Display User Profile Picture from Salesforce in iOS App


I am trying to display logged in user's profile picture using following:

    NSLog(@"url is : %@",[SFAccountManager sharedInstance].idData.pictureUrl);
    profilePicData=[NSData dataWithContentsOfURL:[SFAccountManager sharedInstance].idData.pictureUrl];
    if ( profilePicData )
    {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.jpg"];
        NSLog(@"pic path:  %@",filePath);
        [profilePicData writeToFile:filePath atomically:YES];
    }
    NSLog(@"pic data:  %@",profilePicData);


}

in NSLog("%@", [NSData dataWithContentsOfURL:[SFAccountManager sharedInstance].idData.pictureUrl]); shows some data but does not display picture in UIImageView.

Any Help would be appreciated .


Solution

  • Calling salesforce Rest API and getting user information .

        NSString *path=@"/services/data/v29.0/chatter/users/me";
        SFRestMethod method=0;
        SFRestRequest *request = [SFRestRequest requestWithMethod:method path:path queryParams:queryParams];
        [[SFRestAPI sharedInstance] send:request delegate:self];
    

    this will return json response. - (void)request:(SFRestRequest *)request didLoadResponse:(id)dataResponse {….}

    Parse required photo dictionary from it and use fullEmailPhotoUrl to load/save images. using largePhotoUrl and largePhotoUrl did not load picture for me. Eg. of photo dictionary parsed:

    photo = {

    fullEmailPhotoUrl = "https://na14.salesforce.com/ncsphoto/<SOMEIDENTIFIERS>";

    largePhotoUrl = "https://c.na14.content.force.com/profilephoto/<SOMEIDENTIFIERS>/F";

    photoVersionId = <PHOTOID>;

    smallPhotoUrl = "https://c.na14.content.force.com/profilephoto/<SOMEIDENTIFIERS>/T";

    standardEmailPhotoUrl = "https://na14.salesforce.com/ncsphoto/<SOMEIDENTIFIERS>";

    url = "/services/data/v29.0/chatter/users/<SOMEIDENTIFIERS>/photo";

    };