Skip to main content

Posts

Showing posts from March, 2014

Beyond $_POST, $_GET and $_FILE: Working with Blob in JavaScript and PHP

If you've ever built an HTML form and used a PHP script for the processing part, then you'll be used to the concepts of $_POST and $_GET. Also likely is that you are aware that it is possible to upload files to a server using a similar form and PHP combo . The questions is how do we successfully upload/send data created using JavaScript to a PHP script when it is more than a small snippet of data. JavaScript/jQuery Let's pretend we've saved a JSON string to localStorage and that we want to send this as a Blob to a PHP script. In our JavaScript, we could write: <!DOCTYPE html> <html> <head> // We'll import the jQuery library <script src="js/jquery.js"></script> </head> <body> // We've retrieved data in some way, perhaps using jQuery getJSON() $.getJSON("json.php", function(data) { // We've then added the data to Local Storage localStorage.setItem('json', JSON.stringi

More haste less defaults (NSUserDefaults)

If you have an iOS app that relies on setting a user default, then don't be too hasty exiting the app when testing. For example, if you test for iCloud preference only on first opening, as suggested by Apple's documentation, [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SubsequentLaunch"]; don't open the app once and then close immediately after the code for setting the notification for the app having been opened has been sent. Why not? Because this approach will not provide enough time for the associated .plist file to be created and so it will look like the preference was never set, even though the NSUserDefault method was called to set the BOOL or value. Follow @sketchytech

How to change the background colour of an NSView in OS X 10.8 onwards (Xcode; update Swift 3, Xcode 8)

** Jump to Swift 3 code ** In iOS changing the background colour of a UIView is a trivial thing to achieve. We simply write: myView.backgroundColor = [UIColor yellowColor];  // myView is a UIView Changing the background colour of an NSWindow While it is as simple to change the background colour of a window in OS X [self.window setBackgroundColor:[NSColor yellowColor]]; it is slightly more complicated to change the background colour of an NSView, but not that much more complicated from OS X 10.8 onwards. Changing the background colour of an NSView from OS X 10.8 onwards There are three steps. The first of which is to import the Quartz Core framework in the relevant header file: #import <QuartzCore/QuartzCore.h> The second is to call the setWantsLayer: method in order to enable layer support [myView setWantsLayer:YES]; // myView is an NSView And finally we can write, just as we might in iOS if we were changing the background colour of a layer: myView.layer

HTML tags for XML markup

for publishers using custom internal tagging systems on plain text (good!) why not use HTML tags instead of your bespoke tags? #eprdctn — Hugh McGuire (@hughmcguire) March 25, 2014 In response to Hugh McGuire's question I wanted to write something a bit longer than a tweet, since there are a number reasons why, although Hugh's suggestion makes absolute sense, and I agree that in the main we should save ourselves brain energy and use HTML tags, it is another issue, as to whether or not we should make all XML documents valid HTML5. And there are a number of practical reasons for this. What are these practical reasons? InDesign is a tedious bore when it comes to using attributes in XML tags, and so it's easier and quicker to use solely tag names, especially for mapping to styles Using HTML tags we are at the mercy of the W3C additions, deprecations and alterations HTML tags were not specifically written for books, for example there's no <ref> or <refe

You've probably never heard of Edward Drantler, that's because he doesn't exist

The world wide web is a place of experimentation. My first experimentation with the world wide web was in 2000, when I created a website called theliteraypages.org.uk using my BA essays as content. In 2007, I experimented for a second time. This time by creating a pseudonym and seeing if posting drawings of a highly questionable quality to Wikipedia (and Wikimedia Commons) ended with a right royal flaming or if they met with acceptance. Accept or reject? Most were rejected by Wikipedia gateholders, especially in the UK, but the images remained on Wikimedia Commons , and other countries have adopted some of the drawings. For example, the Portugeuse Wikipedia site uses the Claude Leví-Strauss picture, and the Slovakian Wikipedia uses the drawing of Lacan I made seven years ago. The picture of Claude Leví-Strauss was also used in an edition of Anthropology Now , while a picture of Schoenberg was featured in the programme notes for a performance at the Lobero Theatre, Santa

Practical notes on using pop-up notes in iBooks

A few notes on using pop-up notes in iBooks. Placement of note content Pop-up note content although linked by id reference, must be placed after the close of the current paragraph containing the note referent. Like this: <p> ... <sup id="ref2"><a epub:type="noteref" href="#note2">2</a></sup> </p> <aside epub:type="footnote" id="note2"><p>Here what Manning calls ‘technicity’ brings ‘into alignment the more-than-human that is the intersection of the human and technology’ (Manning, 2010b).</p> </aside> Multiple notes in the same paragraph In a paragraph where there are multiple notes the asides follow one after another, as in this example: <p>In this sense, by engaging with ‘bodies,’ with ‘embodiment and embodiment,’ we are also engaging with the process of differentiation, with the contact and difference that produces bodies from within bodies. Each body in

EPUB2 to EPUB3: Lessons Learnt in Conversion

Following a recent conversion of an EPUB2 file created with Sigil, I'm recording the real-world issues (and realisations) that arose. (Note: in order to understand this post you should be aware of the zipping and unzipping process for EPUBs .) META-INF folder and container.xml file First, nothing about your META-INF folder or mimetype changes. As always the container.xml file points to the root file, which remains the OPF one. <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> <rootfiles> <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"> </rootfile> </rootfiles> </container> Note: the folder needn't be called OEBPS and the file needn't be called content.opf. The filename simply needs to end .opf and the full-path of the rootfile needs to point to it. Changes to the content.opf file Update version attribute of package tag from

NSUndoManager made simple (Xcode/iOS)

Building undo and redo actions needn't be complicated. In the following example I have three buttons wired up from a view controller in the storyboard: a change colour button, an undo button and a redo button. NSUndoManager The change colour button does one simple thing, which is to change the background colour, but each time it does this it captures the current colour and sends this information to the NSUndoManager and tells it that if it ever needs to undo the action, then simply send the current colour (i.e. what has become the old colour) as the new colour to the same method. In terms of programming what is happening, when the undo is called, is that the colour is set again before the undo is recursively taken off the stack. One thing to notice is that the NSUndoManager when implementing the method ignores the call to prepareWithInvocationTarget: or rather it adds the call to the redo stack. A redo is an undo of an undo! And in contrast to an undo, a redo is a new acti

Some notes on working with iCloud (Xcode)

Apple's iCloud Fundamentals guide and Matt Neuburg's Programming iOS 7 will get you well on your way to using iCloud. There are some important things to note, however, especially if you are using Programming iOS 7 : The NSFileManager method  URLForUbiquityContainerIdentifier: will return the URL for the iCloud container, but if you want the files you save to be visible in System Preferences on OS X, for example, then save them to the Documents  subdirectory. If you are using Neuburg's book and want to modify the code to obtain a URL to the Documents subdirectory, then after you've got you NSURL named ubiq , add a new property to the app delegate called ubiqdocs and use the following code:  self.ubiqdocs = [ubiq URLByAppendingPathComponent:@"Documents" isDirectory:YES]; You can then use this whenever you want files to be visible and ubiq when you want them to be invisible. Files saved to the address of a current file will not be written and instead gen

Code posted to GitHub: FileDelete for iOS (Xcode)

Simple FileDelete class for iOS to delete files and subdirectories from the Library and Documents Directories posted to GitHub. It's really easy to use, just import the FileDelete.h file and then use the following class methods, like so: [FileDelete deleteFileFromDocumentsDirectory:@"helpme" inSubDirectory:nil]; [FileDelete deleteFileFromLibraryDirectory:@"helpme" inSubDirectory:@"/help/theworld];     [FileDelete deleteSubDirectoryFromDocumentsDirectory:@"helpless"]; [FileDelete deleteSubDirectoryFromLibraryDirectory:@"Libraryofhelplessness/mine"]; See companion class FileSave as well. Follow @sketchytech

Code posted to GitHub: FileSave for iOS (Xcode)

Simple FileSave class for iOS to save files to the Library and Documents Directories with optional subdirectories posted to GitHub . It's really easy to use, just import the FileSave.h file and then use the following class methods, like so: [FileSave saveContentsOfStringToDocumentsDirectory:@"Hello World" withName:@"helloword.txt" andSubDirectory:nil]; [FileSave saveContentsOfStringToLibraryDirectory:@"Hello World" withName:@"/helpmeworld.text" andSubDirectory:@"INeedHelp]; NSData *fileData = // your data; [FileSave saveDataToDocumentsDirectory:fileData withName:@"Hello.json" andSubDirectory:@"hero"]; [FileSave saveDataToLibraryDirectory:fileData withName:@"Hello.json" andSubDirectory:@"/help/me/world"]; Notice that it doesn't matter whether you begin with a slash or not on the filename or directory name. Follow @sketchytech

Last and least, but really useful: NSEnumerator (iOS/Xcode)

Apple's documentation for enumeration begins with the familiar  for in statement before moving swiftly to the more complex block-based enumeration. This is a leap in complexity and most people are probably mentally exhausted before reaching the final item on the page - 'Using an Enumerator' - which is based around a simple and useful class  with only two methods. This is a great shame because the class, NSEnumerator, is so simple that it can be entirely covered in a brief piece of example code:  NSArray *anArray = @[@"hello",@"world"];     NSEnumerator *enumerator = [anArray objectEnumerator];     id object;         while ((object = [enumerator nextObject])) {         NSLog(@"Array item: %@",object);     }         NSDictionary *aDictionary = @{@"hello":@"Weird",@"world":@"Home"};     NSEnumerator *enumeratorDictionary = [aDictionary keyEnumerator];             id objectKey;         w

Working together with NSRegularExpression and NSPredicate (when you should use NSException) (iOS/Xcode)

I've just started reading around search implementation in iOS (see Further Reading in this post) and it strikes me that if we want to implement Regex searching with NSPredicate in iOS then we immediately hit an issue, which is the lack of error handling in the NSPredicate methods. Hacking it! Now we could get around this by borrowing a newer class that has error handling built in, i.e. NSRegularExpression, and then if no errors are generated by an NSRegularExpression, we could presume ourselves safe to proceed. This approach offers us the benefits of NSPredicate without the risks of crashing on a malformed string. Here's some code to illustrate: NSArray *books = @[@{@"title":@"An Amazing book"},@{@"title":@"A Semi-amazing Book"},@{@"title":@"An OK book"}];       // Pointer to error     NSError *error = NULL;       NSString *keyTitle = @"title";     // The string we are supposing the user ent

Easiest way to zip and unzip files in your iOS app (Xcode)

If you're searching Google for a solution to zipping and unzipping files in your app then you're likely to encounter ZipArchive  first of all. However, there is a simplified version called SSZipArchive that won't throw ARC-related warnings at you when you try and run it. Notes To enable the use of SSZipArchive, you first download the files from GitHub. Add the files from the SSZipArchive folder (i.e.  SSZipArchive.m ,  SSZipArchive.h and the whole of the minizip folder) to your project Next click on the blue project file in Xcode and go to the Library and Frameworks section Add the libz.dylib library to your app (it is always present in Xcode, you don't need to download anything extra) Finally, wherever you use the SSZipArchive class remember to import the header file All will now work as expected when you follow the usage instructions provided on GitHub. EPUB Warning If you are working with EPUB files in Xcode and want to unzip them, then dragging and

Two ways to open a file sent to your app (Xcode/iOS)

Once you've registered your app with the ability to open certain types of file, then you have the responsibility to try and open whichever files are sent to you within these limitations. Open a file sent to your app on launch Within the AppDelegate file there is first of all the method application:willFinishLaunchingWithOptions: which provides us with a opportunity to retrieve an  NSURL object with the following line of code: NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]; This is all very well, but if your app is already running when the "Open In" call is made to it then the  application:willFinishLaunchingWithOptions: method won't be called. Open a file sent to app when it's in the background In this instance we could use instead the application:openURL:sourceApplication:annotation: method like this: - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication ann

Open a file in another app using a UIDocumentInteractionController (Xcode/iOS)

A Swift version of this post with added detail . (1) The first step is to create a button by dragging one out from within the storyboard and placing it on your view. (2) Once you've done this, wire the button up to your view controller's header file. (3) Navigate to the .m file and within your new method place the following code: - (IBAction)exportFile:(id)sender {       // Create an NSURL for the file you want to send to another app     NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"txt"];       // Create the interaction controller     sendFile = [UIDocumentInteractionController interactionControllerWithURL:fileURL];       // Figure out the CGRect of the button that was pressed     UIButton *button = sender;     CGRect buttonFrame = button.frame;       // Present the app picker display     [sendFile presentOptionsMenuFromRect:buttonFrame inView:self.view animated:YES]; } You'll notice I call