0

In my app. When user click on registration in safari browser I am returning to app. In app I am getting url: myApp://[email protected]

I want to fetch value of email from this url. How to do that.?

I have done this but this is giving me error that no key value email found:

NSString *str=[url valueForKey:@"email"];
1

3 Answers 3

1
URLParser *parser = [[[URLParser alloc] initWithURLString:@"myApp://[email protected]"] autorelease];
NSString *emailVal = [parser valueForVariable:@"email"];

Use NSScanner class given below
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

Check this Answer

NSURL pull out a single value for a key in a parameter string

This is what you actually required.

All the best

Sign up to request clarification or add additional context in comments.

Comments

0

Try with URL.query,

NSString *str=url.query;

Where Url is object of NSUrl.

Comments

0

You can also use following code.

NSString *strComplete = @"myApp://[email protected]";

NSArray *array = [strComplete componentsSeparatedByString:@"?"];

NSLog(@"%@",array);
NSString *strSecond = [array objectAtIndex:1];
NSLog(@"%@",strSecond);

Try this if it helps to you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.