问题
Google Docs returns a long 3 line string when supplied with credentials. This is the format SID=stuff... LSID=stuff... Auth=long authorization token
if I have it stored in NSString, what is the best function to trim all the way up to the "=" behind Auth, and keep the rest?
NSData *returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:theResponse error:NULL];
NSString *newDataString = [[NSString alloc]initWithData:returnedData encoding:NSUTF8StringEncoding];
NSString *authToken = [newDataString ____________];
回答1:
I figured out the answer on my own through the documentation for NSString:
there is a method called -(NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator {
that gives back an array of different strings, separated by an NSCharacterSet.
There is a class method of NSCharacterSet called
+(NSCharacterSet *)newLineCharacterSet {
that will split up a string with the newline symbol into pieces, so each line becomes its own object. Here is how it works:
NSData *returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:NULL];
NSString *newDataString = [[NSString alloc]initWithData:returnedData encoding:NSUTF8StringEncoding];
NSCharacterSet *theCharacterSet = [NSCharacterSet newlineCharacterSet];
NSArray *lineArray = [newDataString componentsSeparatedByCharactersInSet:theCharacterSet];
Now, the object lineArray contains different strings, each one is the start of a new line.
You're welcome!
回答2:
If it is a three-line string, I assume it is split with newline (\n) characters.
NSArray *_authComponents = [threeLineString componentsSeparatedByString:@"\n"];
NSString *_sidToken = [_authComponents objectAtIndex:0]; // "SID=..."
NSString *_lsidToken = [_authComponents objectAtIndex:1]; // "LSID=..."
NSString *_authToken = [_authComponents objectAtIndex:2]; // "Auth=..."
Hopefully that gets you started. Once you have individual components, you can repeat on the equals (=) character, for example.
来源:https://stackoverflow.com/questions/1955990/iphone-google-data-api-http-protocol-auth-token-from-string