5 // Created by Christian Brunschen on 29/05/2008.
7 * Copyright 2008 ZXing authors
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
22 #import "URIParsedResult.h"
23 #import "OpenUrlAction.h"
24 #import "EmailAction.h"
27 @implementation NSString (ZXingURLExtensions)
29 - (bool)looksLikeAURI {
30 if ([self rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location != NSNotFound) {
33 if ([self rangeOfString:@":"].location == NSNotFound) {
39 - (NSString *)massagedURLString {
40 NSRange colonRange = [self rangeOfString:@":"];
41 if (colonRange.location == NSNotFound) {
42 return [NSString stringWithFormat:@"http://%@", self];
44 return [NSString stringWithFormat:@"%@%@",
45 [[self substringToIndex:colonRange.location] lowercaseString],
46 [self substringFromIndex:colonRange.location]
53 @implementation URIParsedResult
55 #define PREFIX @"URL:"
57 @synthesize urlString;
60 - (ResultAction *)createAction {
61 NSString *scheme = [self.URL scheme];
63 if ([@"mailto" isEqualToString:scheme]) {
64 return [EmailAction actionWithRecipient:[urlString substringFromIndex:7]
67 } else if ([@"sms" isEqualToString:scheme]) {
68 return [SMSAction actionWithNumber:[urlString substringFromIndex:4]];
71 return [OpenUrlAction actionWithURL:self.URL];
74 - initWithURLString:(NSString *)s URL:(NSURL *)url {
75 if ((self = [super init]) != nil) {
82 - initWithURLString:(NSString *)s {
83 return [self initWithURLString:s URL:[NSURL URLWithString:s]];
86 + parsedResultForString:(NSString *)s {
87 NSRange prefixRange = [s rangeOfString:PREFIX options:NSCaseInsensitiveSearch];
88 if (prefixRange.location == 0) {
89 int restStart = prefixRange.location + prefixRange.length;
90 return [[[self alloc] initWithURLString:[[s substringFromIndex:restStart] massagedURLString]]
94 if ([s looksLikeAURI]) {
95 NSString *massaged = [s massagedURLString];
96 NSURL *url = [NSURL URLWithString:massaged];
98 return [[[self alloc] initWithURLString:massaged URL:url] autorelease];
105 - (NSString *)stringForDisplay {
106 return self.urlString;
109 - (void)populateActions {
110 NSLog(@"creating action to open URL '%@'", self.urlString);
111 [actions addObject:[self createAction]];