Contents

Develop
2014.05.12 16:43

[ios] URL 파라미터 파싱~

조회 수 3682 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
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
번호 분류 제목 글쓴이 날짜 조회 수
709 Develop [python][django] request.cookie 읽어오기 ㅋㅋㅋ (쓰기) hooni 2019.12.06 1696
708 Develop [php] mysql_ 과 mysqli_ 의 차이 hooni 2017.12.01 1699
707 Develop [ios] Xcode cannot run using the selected device hooni 2014.08.14 1798
706 Develop [swift] UIView에서 subview 찾기 hooni 2022.12.09 1800
705 Develop [iOS] Xcode 불필요한 캐시 삭제하기 hooni 2021.10.12 1806
704 Develop [iOS] 시뮬레이터에 푸시 알림을 보내는 방법 file hooni 2021.10.13 1834
703 Develop [ios] Objective-C 에서 자주 사용하는 수학 함수와 유용한 Define hooni 2014.08.08 1867
702 Develop [ios] 카메라 사용 권한 확인해서 분기하는 방법 hooni 2015.02.26 1875
701 Develop [php][laravel] 초간단 MacOS에서 Laravel 개발 환경 구축 hooni 2017.12.15 1889
700 Develop [android] 가속도 센서를 이용한 흔듦(Shake) 감지 file hooni 2014.11.04 1898
699 Develop [ios] 웹뷰 history.back() ㅋㄷ file hooni 2016.06.27 1908
698 Develop [matlab] ZigZag-Scanning (2-D Array) file hooni 2016.10.15 1996
Board Pagination Prev 1 ... 7 8 9 10 11 12 13 14 15 16 ... 71 Next
/ 71