Thursday, April 3, 2014

Twitter and Facebook user ID

Since iOS 6 there is both Facebook and Twitter support in iOS Social framework. But by default - You can get only limited information about Your Facebook and Twitter account. In one of projects, I had to have user ID from Facebook and twitter.

In order to get this user ID from Facebook, You need to import FacebookSDK in Your project and:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];

ACAccountType *FBaccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSDictionary *options = @{
    ACFacebookAppIdKey: @"13569412415218",
    ACFacebookPermissionsKey: @[@"user_birthday"],
    ACFacebookAudienceKey: ACFacebookAudienceFriends
};

[accountStore requestAccessToAccountsWithType:FBaccountType options:options completion: ^(BOOL granted, NSError *e)
{
    if (granted)
    {
        NSLog(@"access granted");
    }
    else
    {
        NSLog(@"error getting permission %@",e);
    }
}];

ACAccountStore *account = [[ACAccountStore alloc] init];

ACAccountType *facebookAccountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSArray *accounts = [account accountsWithAccountType:facebookAccountType];

if(accounts && [accounts count])
{
    ACAccount *account = [accounts objectAtIndex:0];
    
    [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,
        FBSessionState status, NSError *error)
    {
        [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result,
        NSError *error2)
        {
            NSLog(@"user id %@", [result objectForKey:@"id"]);
        }];
    }];
}

For twitter it is simpler:

ACAccountStore *account = [[ACAccountStore alloc] init];

ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
    NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

    if(arrayOfAccounts && [arrayOfAccounts count])
    {
        ACAccount *account = [arrayOfAccounts objectAtIndex:0];

        NSDictionary *properties = [account dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]];

        NSDictionary *details = [properties objectForKey:@"properties"];
        
        NSLog(@"user id %@", [details objectForKey:@"user_id"]);
    }
}];

OSX on SSD/HDD

So.. I've been using a Macbook pro for a (long) while - 2010 year model. It is becoming slower and slower over time, and also - when updating to newer OSX. Without retiring it - only option would be to get an SSD hard drive, to make it at least a tiny bit faster.

I decided to get  a small SSD hard drive, and use it in dvd drive place using a caddy, so that I would still be able to use my old hard drive for some necessary-to-have-but-not-top-priority data.

Main problem is that I don't really have time to install fresh OSX on SSD and reconfigure everything again. Simplest way would be simply make a backup from old one, and then restore from backup onto new SSD - but in my case, I have too much necessary data on old drive, and the new SSD is only 120 GB large. That means - I should install a fresh install on new SSD, and then switch user home directory to old hard drive and set up all settings, configurations as I had on old OSX system.

So I set up a plan, how I would make the switch to new SSD+HDD system:

0.) Get Caddy from ebay - to replace DVD-drive;
1.) Get another SATA HDD which I would place inside Caddy so that I would have two HDD and I would know for sure, that such a system works;
2.) Get SSD (120 GB);
3.) Place SATA HDD (80 GB) in Caddy;
4.) Tidy up old HDD - so that, before final backup - it would have below 160 GB used space;
5.) Prepare bootable Mavericks flash drive;
6.) Do final backup from existing system;
7.) Get another SATA HDD (160 GB);
8.) Switch existing old HDD (320 GB) with 160 GB;
9.) Restore from backup latest backup. So - at this point I would have two identical HDD drives. If I mess up something, I can simply put back one of old HDD, and I can start working - if necessary;
10.) Test that everything works on 160 GB hard drive;
11.) Put in SSD and install Mavericks;
12.) Put old 320 GB hard drive in Caddy place, format and prepare so that user directories would link to old HDD. (and only main applications and OSX on new SSD);
13.) Copy necessary applications on SSD;
14.) Copy back necessary data from backup (because old HDD should be formatted - fresh);
15.) Create first backup from new system setup.

I know I am reallly making things complicated :D But I wanted to be sure, that in case something goes wrong - in few minutes I can get running on some backup HDD and continue work.

I got to the 5th step, and... didn't had time to continue.. 

Month went by, and I encountered a problem with my temporary setup - 80 GB HDD, which was located in Caddy stopped working. What is the problem? 80 GB HDD died, or Caddy is faulty? And if HDD died - why did it? I decided that then it is too risky to put current 320 GB HDD in Caddy ( so that SSD could be placed in original HDD place) - because, maybe Caddy is the reason HDD died.

So I decided to leave 320 GB HDD intact, put in SSD in Caddy, install fresh Mavericks, and simply change SSD OSX user home directory to old HDD user directory. It is simply doable using Settings->Users&Groups->Click_On_Lock->Second_mouse_key_on_your_user->Advanced options  

and You will see a screen like this:


Simply change Home directory to one on old HDD, restart and.... once it loads OSX again, You will not believe Your eyes :)  (At least I didn't at the beginning, and did like 5 restarts, before starting to believe)

Everything will be as it was on previous OSX version - settings, backgrounds, folder structure in finder, files on desktop, shortcut links to application on menu, etc.. Everything will be the same. The interesting thing is - If You copy some applications from old HDD Applications folder to new SSD Applications folder - every shortcut will still continue working. But it will launch application from local SSD. If there is not such application on local SSD, it will then launch it from second HDD :)  Everything just works! :)


P.S.  One thing I did notice - I had previously Hard drive names with spaces. Once I started working with xcode on SSD, which linked to projects from old HDD, it did show problems, that it cannot find some files, or something like that. Turns out - it did not like spaces in middle of names. So - what You have to do IS - just second click on hard drive and rename with no spaces. !!!BUT!!!! - then You must open Settings->Users&Groups->.....  and change the link to Home directory again, or else - after restart You will not be able to log in. (I had this problem. To fix it - I had to boot from old HDD, rename HDD to previous name, boot again from SSD, rename again to name without spaces AND change home directory to correct one. Then restart. And it worked.).


Conclusion.
This kind of system setup is really handy. For example  - if something will happen with new SSD, I will still be able to boot up from old HDD and have the exact same settings and up to date projects. If something happens to old HDD... well... that might be a problem, but for that I try to make often backups.