在我的应用中。当用户在safari浏览器中点击注册时,我将返回app。在应用程序中我得到网址:myApp://?email=pjayesh999@gmail.com
我想从此网址中获取电子邮件的价值。怎么做。?
我已经这样做但是这给了我错误,没有找到关键值电子邮件:
NSString *str=[url valueForKey:@"email"];
答案 0 :(得分:1)
URLParser *parser = [[[URLParser alloc] initWithURLString:@"myApp://?email=pjayesh999@gmail.com"] autorelease];
NSString *emailVal = [parser valueForVariable:@"email"];
使用下面给出的NSScanner类 的 URLParser.h 强>
@interface URLParser : NSObject {
NSArray *variables;
}
@property (nonatomic, retain) NSArray *variables;
- (id)initWithURLString:(NSString *)url;
- (NSString *)valueForVariable:(NSString *)varName;
@end
<强> URLParser.m 强>
@implementation URLParser
@synthesize variables;
- (id) initWithURLString:(NSString *)url{
self = [super init];
if (self != nil) {
NSString *string = url;
NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"&?"]];
NSString *tempString;
NSMutableArray *vars = [NSMutableArray new];
[scanner scanUpToString:@"?" intoString:nil]; //ignore the beginning of the string and skip to the vars
while ([scanner scanUpToString:@"&" intoString:&tempString]) {
[vars addObject:[tempString copy]];
}
self.variables = vars;
[vars release];
}
return self;
}
- (NSString *)valueForVariable:(NSString *)varName {
for (NSString *var in self.variables) {
if ([var length] > [varName length]+1 && [[var substringWithRange:NSMakeRange(0, [varName length]+1)] isEqualToString:[varName stringByAppendingString:@"="]]) {
NSString *varValue = [var substringFromIndex:[varName length]+1];
return varValue;
}
}
return nil;
}
- (void) dealloc{
self.variables = nil;
[super dealloc];
}
@end
检查此答案
NSURL pull out a single value for a key in a parameter string
这是你实际需要的。
一切顺利
答案 1 :(得分:0)
尝试使用 URL.query ,
NSString *str=url.query;
其中Url是NSUrl的对象。
答案 2 :(得分:0)
您还可以使用以下代码。
NSString *strComplete = @"myApp://?email=pjayesh999@gmail.com";
NSArray *array = [strComplete componentsSeparatedByString:@"?"];
NSLog(@"%@",array);
NSString *strSecond = [array objectAtIndex:1];
NSLog(@"%@",strSecond);
如果对您有帮助,请尝试此操作。