Contents

Develop
2014.05.12 16:43

[ios] URL 파라미터 파싱~

Views 3731 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
URL 파라미터 파싱하는..

URLParser.h
@interface URLParser : NSObject

@property (nonatomic, retain) NSArray *variables;
@property (nonatomic, retain) NSArray *keys;

- (id)initWithURLString:(NSString *)url;
- (NSString *)valueForVariable:(NSString *)varName;

@end

URLParser.m
@implementation URLParser

- (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 *keys = [NSMutableArray new];
        NSMutableArray *vars = [NSMutableArray new];

        //ignore the beginning of the string and skip to the vars
        [scanner scanUpToString:@"?" intoString:nil];

        while ([scanner scanUpToString:@"&" intoString:&tempString]) {
            [vars addObject:[tempString copy]];
            
            [keys addObject:
                [[tempString componentsSeparatedByString:@"="]
                    objectAtIndex:0]];
        }
        self.variables = vars;
        self.keys = keys;
    }
    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;
    self.keys = nil;
}

@end


?

List of Articles
No. Category Subject Author Date Views
661 Develop [ios] Xcode를 사용해서 Static Library 만들기 (시뮬레이터 + 디바이스) file hooni 2015.01.03 3389
660 Develop [ios] 앱 딜리게이트 얻어오기. (AppDelegate) hooni 2014.05.10 3394
659 Develop [ios] Sprite Kit & 사운드 재생시 백그라운드 진입시 앱이 비정상적으로 종료됨 hooni 2014.04.18 3428
658 Develop [ios] NSString URL Encode/Decode (인코딩/디코딩) hooni 2014.05.02 3478
657 Develop [android] dp, px 서로 변환 hooni 2016.10.21 3493
656 Develop [ios] 아이폰에서 진동(Vibrate) 기능 추가하기 hooni 2014.04.18 3519
655 Develop [js] jQuery 셀 병합 1 file hooni 2014.09.23 3552
654 Develop [ios] 유용한 매크로 hooni 2014.03.26 3694
653 Develop [ios] UIWebView 캐쉬 삭제 hooni 2014.04.08 3702
» Develop [ios] URL 파라미터 파싱~ hooni 2014.05.12 3731
651 Develop [ios] SQLite 사용하기(튜토리얼) + 샘플코드 file hooni 2014.03.28 3748
650 Develop [ios] iOS에서 디바이스 종류 알아오기 hooni 2014.05.24 3749
Board Pagination Prev 1 ... 11 12 13 14 15 16 17 18 19 20 ... 71 Next
/ 71