Domanda

I'm trying to get access token,when I decode URL it's getting an error,I think am declaring wrong here.How to declare URLDecode to get access token?

in .h file

@interface NSString (URLDecode)

-(NSString *)URLDecode;

@end

in .m file

-(NSString *)URLDecode

    {

        NSString *result = [(NSString*)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
        result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return result;
    }


- (NSDictionary*)parsePairs:(NSString*)urlStr

{

    NSRange r = [urlStr rangeOfString:@"="];
    if(r.length == 0)
    {
        return nil;
    }

    //Here I'm getting an error

    NSString* token = [[urlStr substringFromIndex:r.location + 1 ] URLDecode];

    NSCharacterSet* objectMarkers;
    objectMarkers = [NSCharacterSet characterSetWithCharactersInString:@"{}"];
    token = [token stringByTrimmingCharactersInSet:objectMarkers];

    NSError* regexError;
    NSMutableDictionary* pairs = [NSMutableDictionary dictionaryWithCapacity:10];

    NSRegularExpression* regex;
    regex = [NSRegularExpression regularExpressionWithPattern:@"\"([^\"]*)\":\"([^\"]*)\""
                                                      options:0
                                                        error:&regexError];
    NSArray* matches = [regex matchesInString:token
                                      options:0
                                        range:NSMakeRange(0, token.length)];

    for(NSTextCheckingResult* result in matches)
    {
        for(int n = 1; n < [result numberOfRanges]; n += 2)
        {
            NSRange r = [result rangeAtIndex:n];
            if(r.length > 0)
            {
                NSString* name = [token substringWithRange:r];

                r = [result rangeAtIndex:n + 1];
                if(r.length > 0)
                {
                    NSString* value = [token substringWithRange:r];

                    [pairs setObject:value forKey:name];
                }
            }
        }
    }

    regex = [NSRegularExpression regularExpressionWithPattern:@"\"([^\"]*)\":([0-9]*)"
                                                      options:0
                                                        error:&regexError];
    matches = [regex matchesInString:token
                             options:0
                               range:NSMakeRange(0, token.length)];

    for(NSTextCheckingResult* result in matches)
    {
        for(int n = 1; n < [result numberOfRanges]; n += 2)
        {
            NSRange r = [result rangeAtIndex:n];
            if(r.length > 0)
            {
                NSString* name = [token substringWithRange:r];

                r = [result rangeAtIndex:n + 1];
                if(r.length > 0)
                {
                    NSString* value = [token substringWithRange:r];
                    NSNumber* number = [NSNumber numberWithInt:[value intValue]];

                    [pairs setObject:number forKey:name];
                }
            }
        }
    }

    return pairs;
}   

Any ideas?

È stato utile?

Soluzione

You need to make a cetgory of NSString in your Xcode project as : 1. enter image description here

  1. Then do as : enter image description here

Then when you click Next button a cetgory will be created of NSString . Then use add code in that. And then Import this category in your class as :

#import "NSString+URLDecode.h"

and then call method declared in category over NSString as

NSString *decodedUrlStr = [urlStr URLDecode];

Hope it helps you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top