Most recently, I tried my hand at pulling an API and funneling the information in a way that suited a well versed app. I went with MakerBots Thingiverse API which is pretty unique. On the backend, they don’t allow for a custom URL scheme, so I had to find a workaround to link a page on a website back to my app. This was pretty neat and interesting.
(You can download the source at my Github. You will need to create a “Constants.h” and “Constants.m” file after signing up at http://www.thingiverse.com/developers and receiving a client_ID and client_secret)
A customURL scheme is a url you can set in your xcode project to have webviews link back to your app. For OAuth, when a user logs in, they are redirected back this custom URL. In the case of the MakerBot API, I ping a page on my website, and have that page redirect back to my app through a simple line in html. The url I am redirecting to is set in my custom URL scheme.
<meta http-equiv="refresh" content="0; url=thingiversesearch://oauth" />
Under Projects -> Targets -> ProjectName -> URL Types
This step solved my OAuth issues and I was able to receive an access token after a user signed in.
Regex
When using regex, I find it great to use a resource my former teacher once showed the class called “Rubular“. It is a ruby based site that allows for you to play around with regex expressions and find matches. I find this helpful when doing custom url’s to find and replace strings or things that I can’t just flat out Google.
I’m using regex’s in a couple of different places in my app. One of the most notable places where it makes a big impact on my code is when pulling image info. When calling the API for a “thing” in the “Thingiverse”, you get an array of dictionaries that somewhat resemble the code below.
{
"id":244,
"name":"potatono",
"url":"https://api.thingiverse.com/users/potatono",
"thumbnail":"http://thingiverse-production.s3.amazonaws.com/renders/ff/46/e6/04/1f/ApocolypseBurningMan2010_thumb_medium.jpg",
"bio":"",
"location":"New York, NY",
"registered":"2008-11-29T10:26:26-05:00",
"last_active":"2012-12-31T17:29:19-05:00",
"things":"https://api.thingiverse.com/users/potatono/things",
"copies":"https://api.thingiverse.com/users/potatono/copies",
"likes":"https://api.thingiverse.com/users/potatono/likes",
"default_license":"cc",
"email":"justin.day@makerbot.com"
}
Within that code, you get a string URL for the user’s profile image. It is a similar line that you get for the thing’s thumbnail image. Instead of making a second call to the API to get a list of images for a given object, I wanted to take this string and replace the end to create the URL to a bigger image myself. This speeds up the process of having to call an API asynchronously and wait for a response before loading an image after already having obtained the object.
NSString *thingPreviewThumbnail = thingDictionary[@"thumbnail"]; NSString *pattern = @"(thumb_medium.jpg)"; NSRegularExpression *regex = [NSRegularExpressionregularExpressionWithPattern:pattern options:0error:nil]; NSString *largePreviewThumbnail = [regex stringByReplacingMatchesInString:thingPreviewThumbnail options:0 range:NSMakeRange(0, [thingPreviewThumbnail length]) withTemplate:@"display_medium.jpg"];
The above code takes the url
http://thingiverse-production.s3.amazonaws.com/renders/ff/46/e6/04/1f/ApocolypseBurningMan2010_thumb_medium.jpg
And matches for the pattern “(thumb_medium.jpg)” that is at the end of every url returned by the Thingiverse API. In this case, I was able to directly target the section I wanted to replace. I used Thingiverse.com to open up image urls and found that the only difference between the API call and the url I wanted was replacing the “thumb_medium.jpg” with “display_medium.jpg”. As a result, my images went from
without having to make another call to the API.
Lastly, I wanted to implement a search bar into a tableview so that users could search for specific products. It was my first time implementing such a feature. I used the delegate method from the search bar to make calls to the API. By pulling the search text into the string url, you are easily able to make any call to the API for a search. I created the custom method below to pull in from the Search
- (void) searchforThing:(NSString *)searchText withCompletion:(void(^)(NSArray * things))completionBlock{ NSUserDefaults *defaults =[NSUserDefaultsstandardUserDefaults]; NSString *accessToken = [defaults objectForKey:@"thingiverseAccessToken"]; NSString *getUserInfo = [NSStringstringWithFormat:@"https://api.thingiverse.com/search/%@?access_token=%@", searchText, accessToken];
The search text that gets pulled into this custom method above comes from the Search Bar delegate method
– (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
Enhancements
There are a couple of features I would like to make better in this project app.
– Cache images for faster loading time
– I want to implement this feature due to the size of the images I am pulling from the API. By caching the images and also implementing a background thread for this, I should have smoother scrolling throughout my app when images are being loaded.
– Posting feature
– I want to include a feature where the user can search for a Thingiverse object and then select that object to put into a collection.
– Better UI/UX
Now that I have the hi-res images, I want to implement better UI to make it feel more like and app that users would want to interact with.








