Tag Archives: iOS

Posts that include information about Apple’s mobile operating system or about programming for iOS.

Accessing an SMB share in iOS using Swift

As you might guess from my Books page, I read/listen to a lot of books. It became apparent that I needed a way to track all of these books, basically just for my own sanity. I’m not sure if you are like me but when you read this many books, I really don’t remember all of them (most of them?). There are a lot of series in there that span years, so I often forget which ones I’ve read.

To alleviate this problem, I wrote a book database app for iOS/iPhone, using Swift and SwiftUI. It’s super simple, only has a couple of screens but as important as tracking my books, it allows me to search them. This is invaluable. Here are a couple of screenshots:

Since friends and family will ask what I’m reading, I decided to add the ability to post my list of books read to this website. It’s just a json file that lives on my server and is read by a script. The catch was when I first wrote the app, my website was hosted and the only way to push files to the server was via FTP. iOS supported FTP upload a long time ago and while depreciated, it still worked. And once you have something working, you tend to forget about it. When I moved to self hosting my website, FTP was quick and dirty to get going and I only had to change the login data inside the app.

But all the depreciated warnings bugged me, so I set out to move to SMB, as my server has a bunch of shares on it for various other things. iOS added native support for SMB way back in iOS 13 but it was pretty low level and I could never find any good example code on the web to work from. Fast forward to this week and I finally have tackled the problem thanks to a super easy SMB client library I found on GitHub.

The readme has very clear instructions, although I think he changed the fileName property to name in all instances and just forgot to update it. It really was as simple as adding the library via the Swift Package Manager (SPM), importing the library into my helper swift file that deals with saving the data, and then following the code examples under “Usage”. The author doesn’t use any do {...} catch {...} blocks to deal with errors in the example code but they are easy enough to implement. If you incorporate this functionality using a function, you’ll need to mark it as asynchronous and use a Task to call it, again pretty straight-forward. I commented out the old FTP code, tested, and everything worked. Sweet!

I’m not sure it’s worth providing more code here, given the extent of code in the readme on the GitHub page but if you have any questions, feel free to contact me or leave a comment.

Updated:
Uploading a file using this client will not overwrite an existing file but will cause a name collision error. You can however, delete the file prior to uploading, which worked find.

Creating a Transparent Modal UIViewController in iOS 8

I’ve just started to scratch the surface of all the cool things iOS 8 can do, so much to learn! One of my apps presents a modal view controller that asks the user to select a date from a custom calendar view I built. The app displays photos and this modal vc is presented over an image, so I wanted the modal vc to be (semi) transparent, so the image could be seen behind it. I scoured the web but there seemed to be a lot of conflicting ideas out there and none of them worked for me. I happened to stumble upon a/the solution on my own, while poking around Attributes Inspector.

Select the (modal’s – i.e. the one you want to be transparent) view controller from your storyboard and then click on the Attributes Inspector. As you can see in the screen shot below, there are Transition Style and Presentation options now available. The Transition Style is just what you expect it is and for Presentation, you want to select Over Current Context. Viola! Crazy simple.

Attributes Inspector

I’d be interested to hear what has worked for you in iOS 7, although backward compatibility is quickly becoming less interesting to me.

How To Convert HTML to NSAttributedString

I’m not sure what the best practice is these days when displaying HTML content in your app. It seems you can either put it into a UIWebView or you can, as of iOS 7, convert it to an NSAttributedString and put it into a UILabel, UITextField, etc. I have been leaning toward the latter recently and thought I’d post the 2 lines of code required to make that happen.

NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithData:[self.htmlText dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];

self.htmlLabel.attributedText = stringWithHTMLAttributes;

The above code assumes you have HTML stored in an NSString variable called “self.htmlText” and a UILabel called htmlLabel and that your encoding is UTF8.

Let me know if you have any questions or if there is a better practice I’m not aware of.

How to Convert an ISO8601 Date in iOS

I still find working with dates in iOS a little tricky. There is always a new format to deal with and it’s all about getting the identifiers for the format just right! I recently ran into ISO8601 and hope the bit of code below is of some use.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *date = [dateFormatter dateFromString:self.releaseDate];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
self.dateString = [dateFormatter stringFromDate:date];

You can, of course, pick any other date format style (instead of Long) as the output. As always, let me know if you have any questions.