Question

Being an iOS and Objective-C newbie I'm trying to construct an app, where the user can authenticate through Facebook, Google+ or 3 further (Russian) social networks.

For Facebook I know, that I could use the Facebook SDK or Social.framework, but for the others - I have to use OAuth and UIWebView, because there are no good SDKs for them yet.

(I have already succeeded in that once - but that was for an Adobe AIR app and now I'm trying to learn native...)

In Xcode 5.2 I have prepared a very simple Master-Detail app for iPhone and checked it into GitHub:

screenshot

My question is about constructing a NSString for a GET (or body in POST) request -

Currently I have the following awkward source code in the DetailViewController.m:

- (NSString*)buildUrl
{
    NSString *key = _dict[kKey];
    NSString *str = _dict[kAuthUrl];

    if ([key isEqual: kFB]) {
        str = [str stringByAppendingString:@"display=touch"];
        str = [str stringByAppendingString:@"&response_time=token"];
        str = [str stringByAppendingString:@"&client_id="];
        str = [str stringByAppendingString:_dict[kAppId]];
        str = [str stringByAppendingString:@"&redirect_uri="];
        str = [str stringByAppendingString:_dict[kAppUrl]];
        //str = [str stringByAppendingString:@"&state="];
        //str = [str stringByAppendingString:rand(1000)];
    } else if ([key isEqual: kGG]) {
    } else if ([key isEqual: kMR]) {
    } else if ([key isEqual: kOK]) {
    } else if ([key isEqual: kVK]) {
    }

    return str;
}

My questions:

  1. Instead of using stringByAppendingString could I use something nicer? Like maybe an NSArray (or even better NSDictionary) and then somehow joining it with ampersands inbetween?
  2. How to escape the HTML entities in the value of redirect_uri= ?
  3. I need to append a random number as the value of state=, but I am not sure what function to use there best...

Here is what my app prints at the moment, for the above code:

MyAuth[9626:70b] request: { URL: https://graph.facebook.com/oauth/authorize?display=touch&response_time=token&client_id=432298283565593&redirect_uri=https://www.facebook.com/connect/login_success.html }

(which is no good: the URL at the end is not escaped and there is no random state number).

Was it helpful?

Solution

stringWithFormat: is your friend.

- (NSString*)buildUrl
{
    NSString *key = _dict[kKey];
    NSString *str = _dict[kAuthUrl];

    if ([key isEqual: kFB]) {
        NSString *escapedURI = [_dict[kAppUrl] stringByAddingPercentEscapesUsingEncoding:NSUTF8Encoding];
        int state = arc4random_uniform(1000);
        str = [NSString stringWithFormat:@"%@display=touch&response_time=token&client_id=%@&redirect_uri=%@&state=%d", _dict[kAuthUrl], _dict[kAppId], escapedURI, state];
    } else if ([key isEqual: kGG]) {
    } else if ([key isEqual: kMR]) {
    } else if ([key isEqual: kOK]) {
    } else if ([key isEqual: kVK]) {
    }

    return str;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top