iPhone Google Data API HTTP Protocol, Auth Token f

2019-06-05 17:04发布

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 ____________];

2条回答
时光不老,我们不散
2楼-- · 2019-06-05 17:27

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.

查看更多
时光不老,我们不散
3楼-- · 2019-06-05 17:36

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!

查看更多
登录 后发表回答