I ended yesterday’s 100 Days of Swift challenge on such a high note. I really thought I was getting it. Looking back over the past four weeks I have come light years beyond what I had expected. If this kept up I was almost thinking of creating business cards that said, “iOS Developer” on them. I had proudly showed family and friends the projects I had completed with an air of confidence I had heretofore now had.
So today when I looked at the challenges for Project 4 I was feeling pretty good about life. I am not normally someone who puts a lot of credence into foreshadowing but after today maybe I should. The day started with a bowl of Frosted Mini Wheats, which I believe is the breakfast of code champions; or at least that’s what I thought. But here is where things began to turn. On the second bite I felt a snap followed by something hard in my mouth coming loose. Oh great, I broke a tooth on a piece of cereal.
After a quick phone call to the dentist who miraculously had an emergency spot open up I rushed over and spent a couple of hours in the dentist’s chair. While he had a drill stuck in my mouth asking me how things were going at home, I was trying to concentrate on the Project 4 challenges and how I was going to approach them. Needless to say, by the end of the appointment I had forgotten all about closures, optionals, and WebKit. I was too busy trying not to droll on myself and not talk like I had just had a stroke.
As I got back to my computer, I re-read the challenges. First up, in our Safe Browsing app we were to catch when a user tried to go to a web site not contained within our list of places we would allow them to surf to. That seemed easy enough. We had already created a function that would read the array of web sites and allow a page to display. All I really needed to do was show an alert after the for loop that would pop up an alert with a message if the web site wasn’t in our list.
Confident I coded the one line of code and placed it where I thought it should go. Now here is where I should note that I had extended the original array. In the assignment the array contained Apple.com and HackingWithSwift.com. I figured it was my application so I would add this blog to the list as well. I ran the code and clicked on Apple.com. The site was found and things worked perfectly. I then clicked on my blog link and I got the alert box telling me the site was blocked.
Wait, what was that? No, no, no, that’s not right. I went back through the code and checked spelling, syntax, and the array. Everything looked ok. Maybe I had the alert placed in the wrong place. So, I moved it and tried again. Same results. What the heck? I went back through the code and again moved the alert line of code and ran again. Same problem. Each time I hit the “Run” button and saw the blocked alert box my confidence level dropped another rung. I was sure I had a handle on this. For the next hour I went through the code with a fine-tooth comb and yet no matter what I tried I got the same failure. What was even odder was that if I left the simulator on my blog, I would get the error alert, it would disappear then reappear again. This was getting crazy!
No matter what I was trying, it wasn’t working. I decided to set it down and work on something else. The second challenge was to make two toolbar buttons to allow the user to go back and forward a page on the stack. After my experience with the first challenge my confidence was definitely shaken. But rather than let it get the best of me I took a swing at it and voila it worked the first time.
Still not wanting to go back and look at the first challenge just yet I looked to see what the third and final challenge was. For this one we were to create a new TableViewController that showed the list of safe web sites and allow the user to choose one to be taken to the ViewController to see the actual site. This was to be more complex. I dove into that and quickly made the changes to the code and it too worked right off the bat.
So, after taking on the more advanced challenges I went back to the first one. This time I removed my blog from the array and ran the code without making any other modifications. It ran perfectly. What?!?! I added my blog back in and ran it and it failed. I replaced my blog in the array with another one of my web sites and it worked without any issues. I can’t believe it, my app hates my blog!
Suddenly this ceased to be a Swift challenge and became a web site challenge. Why would this code not work when surfing to my blog? Well, funny I should ask. It would appear that I was using a plug-in for WordPress that was making a call back to a vendor’s site. Since the vendor’s site was not in my safe websites array it was accurately telling me that was blocked. I just didn’t know that was happening. I removed the offending plug-in and found there was another file that was being called externally. After going through my WordPress files and recoding some of the theme to remove those external calls it started working for a minute.
I was no longer getting the Blocked message when my blog loaded but was getting it when I went to an individual blog entry. It then dawned on me that Apple requires these sites to be running https. And while my web pages were being rewritten from http to https, the images on past posts were still pointing to non-secure versions. I went out to the database and wrote some queries that would search out http and replace it with https on all tables. Once that happened, I was able to access my blog through the app I had written, and no longer did I get any errors.
I guess the moral to this story is to always make sure your test data is accurate and never assume that anything on the web is configured correctly especially if you are trusting yourself to do it. This particular challenge ended up being a lot more challenging than I expected but through perseverance and logical problem solving nothing is impossible.
0 Comments