1

in my app while fetch data from server i am getting image url and then i load that image in my imageview. its working perfectly but when my url getting arabic character its getting crash. so how can i find out that my urlstring is contain arabic character or not?

i only want A to Z and number and / and - and . in my urlstring

right now i just avoiding crash by this code but its not proper thing

this is my code

 if (newimage.characters.count != 0)
        {

            if (newimage != "http://www.******.**/****/*****/uploads/2015/**/سيباستيان-فيتيل-فيراري-فورمولا-1.jpg")
            {
                ImageLoader.sharedLoader.imageForUrl(newimage) { (images, url) -> () in

                    if (images != nil)
                    {
                        print(images)
                        cell.image1.image = images!
                    }

                }
            }
            else
            {
                print("XYZ")
            }

        }
Govind Rakholiya
  • 109
  • 1
  • 1
  • 7
  • You can use regex method defined in [this](http://stackoverflow.com/a/3819561/437146) post to validate URL. This method will return false, if URL contains any invalid character (includes arabic characters as well). – NightFury Mar 10 '16 at 12:39
  • Where does this new image URL come from? You can validate the text as you input it, stopping somebody typing in an invalid URL @ the stable door! – user3069232 Mar 10 '16 at 12:47

1 Answers1

0

You can use regex to check your URL:

let regex = try! NSRegularExpression(pattern: "[^A-Za-z0-9 \\-\\.\\:\\/]", options: [])

let firstMatch = regex.rangeOfFirstMatchInString(newImage, options: [], range: NSMakeRange(0, newImage.characters.count))
if firstMatch.location != NSNotFound {
    // Invalid character found
}
Code Different
  • 90,614
  • 16
  • 144
  • 163