Ramesh Rayadurgam
Ramesh Rayadurgam

Reputation: 75

How to check the NSString contains URL or string data?

I am fresher to iOS, i am getting problem at checking string object contains URL or string?

NSMutableArray *Arr=[NSMutableArray alloc]initWithObject:@"Welcome", @"http://abcd.com/Images/bus.png", nil];

int i;

i++;

NSString *str=[Arr objectAtIndex:i];

Now, i want to check condition, if string contains "Welcome", have to display on label or if it is URL , i need to display that URL image in ImageView. So how can i check it? Please help me in this problem.

Upvotes: 0

Views: 1442

Answers (4)

Hussain Shabbir
Hussain Shabbir

Reputation: 15005

Try like this

NSMutableArray *Arr=[[NSMutableArray alloc]initWithObjects:@"Welcome", @"http://abcd.com/Images/bus.png",nil];

    NSString *st=nil;
    for(NSString *string in Arr)
    {
     NSArray *matches = [detector 
                 matchesInString:string
                                    options:0
                                      range:NSMakeRange(0,     
                                     [string length])];
   for (NSTextCheckingResult *match in 
          matches) {
          if ([match resultType] ==    
         NSTextCheckingTypeLink) {
        NSURL *url = [match URL];
         } else 
       {
        NSlog(@"it is a string");
       }
    }

}

Upvotes: 1

SomeGuy
SomeGuy

Reputation: 9690

Instead of initiating both as NSStrings, try differentiating between them by making urls a NSURL (special container specifically for urls):

NSMutableArray* Arr = [NSMutableArray alloc]initWithObject:@"Welcome", [NSURL URLWithString:@"http://abcd.com/Images/bus.png"], nil];

for(id object in Arr)
{
    if([object isKindOfClass:[NSString class]])
    {
        NSString* string = object;
        NSLog(@"String: %@", string);
    }
    else if([object isKindOfClass:[NSURL class]])
    {
        NSURL* url = object;
        NSLog(@"URL: %@", url);
    }
}

Upvotes: 1

sohail059
sohail059

Reputation: 866

To check NSString is containing a URL You can Try This code

if ([stringName hasPrefix:@"http://"] || [stringName hasPrefix:@"https://"]) {
    //show imageVivew
} 

Upvotes: 0

PR Singh
PR Singh

Reputation: 653

Try this, it will help you:

NSMutableArray *Arr=[[NSMutableArray alloc]initWithObjects:@"Welcome", @"http://abcd.com/Images/bus.png", nil];

if([Arr count])

{

for (NSString *str in Arr)

    {
        if([str isEqualToString:@"Welcome"])

        {
            NSLog(@"str is %@",str);

            //do whatever you want
        }

        if([str isEqualToString:@"http://abcd.com/Images/bus.png"])

        {
            NSLog(@"str is %@",str);

            //do whatever you want
        }
    }
}

Upvotes: 0

Related Questions