Integrating two login system into an app

2020-05-07 06:26发布

问题:

I'm trying to integrate both a Facebook and a Foursquare login system into my app. However, I have no idea how to edit the URL type and URL scheme under the myapp.plist. Should I just add a new item under URL scheme or create a new URL type?

This is the image for Facebook login.

回答1:

It looks like you're just trying to create a custom URL scheme. To do this, do something like this:

Then, all URL requests that are sent to myapp://whatever-url on the device will be directed into your app. You can identify them in the app delegate file of your app using the following method. I'm assuming you're getting data sent back to you in the URL (like a token) and you need to retrieve that information, so you'll need to parse the URL.

//Handles URL schemes specified in the info.plist file
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    //Get the URL in string format
    NSString *urlString = [url absoluteString];

    //See if the URL contains part of the URL we're expecting (ie this is the base URL with data like a token appended to it)
    if ([urlString rangeOfString:@"myapp://url_one"].location != NSNotFound) {

        //Parse the URL
        //Reference: http://stackoverflow.com/questions/8756683/best-way-to-parse-url-string-to-get-values-for-keys
        NSMutableDictionary *queryStringDictionary = [[NSMutableDictionary alloc] init];
        NSArray *urlComponents = [urlString componentsSeparatedByString:@"&"];

        for (NSString *keyValuePair in urlComponents) {

            NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
            NSString *key = [pairComponents objectAtIndex:0];
            NSString *value = [pairComponents objectAtIndex:1]; //Make sure this is URL decoded

            //Add the value to an array, dictionary, etc. for usage later here
        }
    }

    return TRUE;
}

myapp can literally be whatever you want. The purpose here is to send a URL that will only direct users to your app. So, this must be something unique. For example, don't use fb because that's used by the Facebook app.