Archive

Archive for the ‘xcode’ Category

When projects collide…

September 12, 2013 Leave a comment

The iPhone/iPad app I am developing is for live music use (not giving away any details just yet since I am sure any reasonable iPhone developer could knock up the same app in a couple of days and beat me to the finish line!) which means your hands are not so free. My original idea was to use a blue tooth connected foot pedal that I already happen to have (Airturn BT-105) to control the app. However it occurred to me that it would be pretty simple to add a blue tooth shield to an Arduino, hook up a few switches and make my own blue tooth enabled stomp box to control my app, plus I could have more than the two switches my current pedal has.

So, once the current Arduino project is done I’ll be starting on creating a blue tooth enabled stomp pedal to control this app and looking at blue tooth enabling my app to work with my custom pedal.

Categories: Coding, iPhone, xcode Tags: , , ,

Upgraded my ‘virtual Mac’…

August 4, 2013 1 comment

I’ve been using an Xcode plan from http://virtualmacosx.com/ for a little while but now I need to be able to install other software so that I can generate data for my app and the trouble with the Xcode plan is that you don’t have admin authority. They did say they could install anything I needed but there are a number of other limitations with the Xcode plan that mean that while it has been great for getting started with iOS development, it’s now time to upgrade to a more serious plan.

So I’ve gone for one of their Virtual Private Desktop plans. My wife did  say I could go and get a Mac but I figure that since I am still not totally committed to this yet and still in ‘play’ mode pretty much, I could buy a LOT of months on a virtual machine for the price of a new iMac (plus I object to paying a premium for ‘style’). There are other advantages to the new plan too apart from more storage and more memory. I can use TeamViewer to connect to the desktop instead of vanilla VNC. TeamViewer is free for non commercial use (which this definitely is right now), it performs better than VNC and has encryption, something that I was never able to enable using VNC to the Xcode desktop. The new image also has ADMIN privileges so I can install stuff from iTunes! and since it is a personal desktop, not shared like with the Xcode plan, the iOS simulator is no longer shared. not that it was ever an issue for me but I don’t have to shut it down after every use now either.

The hardest part of the whole process was probably transferring my development certificates from the old Xcode machine to this new one. Apple seem to be as useless as everyone else in explaining this whole process in terms a numbskull like me can understand but I seem to have managed it now. I rebuilt my app on the new machine and managed to install it on my iPod and run it so I ‘think’ I am good to go.

Anyway I’ve ‘committed’ to the new environment by cancelling my Xcode plan from the next billing cycle which is  in a few days. After that, if I messed up, I gotta fix it the hard way (or hope I backed it up!).

Displaying a PDF file within an App…

July 24, 2013 Leave a comment

As part of the app I am working on I wanted to use a PDF document for the help file. I am using a storyboard and the help displays in a separate view. Since the help document contains multiple pages I wanted to allow the user to be able to navigate through the document using swipe gestures.

This is what I ended up with:

Since I learned so much about how to do this from various places on the Internet, I thought I’d return the favor and show my code. I am sure it could be improved a lot (I am a noob at this Objective-C stuff after all) and I’ll probably cringe if I come back to this in six months but regardless, I offer my code in the hope that it may be of use to someone. Oh, and if you spot any REALLY BAD errors, feel free to let me know. I’m not proud at all and always willing to learn.

My screen consists of a navigation bar with the ‘Done’ button (btnHelpDone) on it and a UIView (pdfView) that fills the rest of the screen. There are also four swipe gesture controllers, one for each direction. The ‘Done’ button just closes the current view and returns to the previous one in the storyboard.

I replaced the UIView’s drawRect with my own which does the bulk of the work to display the PDF.

This the he helpViewController.h file…

//
//  HelpViewController.h
//
//  Created by David Ellis on 2013-07-14.
//  Copyright (c) 2013 David Ellis. All rights reserved.
//

#import <UIKit/UIKit.h>

// see drawHelp
extern NSInteger g_helpDocPageNum;
extern CGPDFDocumentRef g_helpDocRef; 
extern NSInteger g_helpDocMaxPage;

@interface HelpViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *pdfView;
- (IBAction)btnHelpDone:(id)sender;
- (IBAction)helpLeftSwipe:(id)sender;
- (IBAction)helpRightSwipe:(id)sender;
- (IBAction)helpUpSwipe:(id)sender;
- (IBAction)helpDownSwipe:(id)sender;

@end

This is the helpViewController.m file

//
//  HelpViewController.m
//
//  Created by David Ellis on 2013-07-14.
//  Copyright (c) 2013 David Ellis. All rights reserved.
//

#import "HelpViewController.h"

@interface HelpViewController  ()

@end

@implementation HelpViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // self.pdfView.backgroundColor = [UIColor redColor];   // test just to show this code runs
    // NSLog(@"helpviewdidload");

   }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnHelpDone:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)helpLeftSwipe:(id)sender {
    // next page
    // NSLog(@"helpLeftSwipe");
    if (g_helpDocPageNum < g_helpDocMaxPage) {
        g_helpDocPageNum=g_helpDocPageNum+1;

        [UIView transitionWithView:self.pdfView duration:1
            options:UIViewAnimationOptionTransitionCurlUp      
            animations:^{
            //     [self.pdfView.layer displayIfNeeded];
            }  completion:nil];

        [self.pdfView  setNeedsDisplay];
    }
}

- (IBAction)helpRightSwipe:(id)sender {
    // prev page
    // NSLog(@"helpRightSwipe");
    if (g_helpDocPageNum > 1) {
        g_helpDocPageNum = g_helpDocPageNum-1;

        [UIView transitionWithView:self.pdfView duration:1
            options:UIViewAnimationOptionTransitionCurlDown
            animations:^{
           //     [self.pdfView.layer displayIfNeeded];
            }  completion:nil];

        [self.pdfView setNeedsDisplay];
    }
}

- (IBAction)helpUpSwipe:(id)sender {
    // same as left swipe = next page
    // NSLog(@"helpUpSwipe");
    [self helpLeftSwipe:sender];
}

- (IBAction)helpDownSwipe:(id)sender {
    // same as right swipe = prev page
    // NSLog(@"helpDownSwipe");
    [self helpRightSwipe:sender];
}

- (void)viewDidUnload {
    [self setPdfView:nil];
    [super viewDidUnload];
}

@end

The helpViewController.m file implements the code to close the view when the Done button is pressed and the gesture controller code. The Up and down gesture code just calls the Left and Right code.

The pdfView UIview is sub classed to the drawHelp class. This is the drawHelp.h file (not much in it as you can see!):

//
//  drawHelp.h
//
//  Created by David Ellis on 2013-07-15.
//  Copyright (c) 2013 David Ellis. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface drawHelp : UIView

@end

And finally, the drawHelp.m class file:

//
//  drawHelp.m
//
//  Created by David Ellis on 2013-07-15.
//  Copyright (c) 2013 David Ellis. All rights reserved.
//

#import "drawHelp.h"

CGPDFDocumentRef g_helpDocRef=NULL;
NSInteger g_helpDocPageNum=1;
NSInteger g_helpDocMaxPage=0;

@implementation drawHelp

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

CGPDFDocumentRef MyGetPDFDocumentRef (NSString *filename)
{

    CGPDFDocumentRef document;
    size_t count;

    NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType :@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:path];

    document = CGPDFDocumentCreateWithURL ((__bridge CFURLRef)url);

    count = CGPDFDocumentGetNumberOfPages (document);
    if (count == 0) {
        NSLog(@"`%@' needs at least one page!", filename);
        return NULL;
    }

    return document;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{

    // Drawing code

    CGPDFPageRef page;

    // NSLog(@"This is drawRect");

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // set background color
    CGContextSetRGBFillColor(ctx, 255.0, 255.0, 255.0, 1.0);
    CGContextFillRect(ctx,rect);

    // flip co-ordinates for pdf
    CGContextGetCTM(ctx);
    CGContextScaleCTM(ctx,1,-1);
    CGContextTranslateCTM(ctx,0,-rect.size.height);

    g_helpDocRef=MyGetPDFDocumentRef(@"Help");
    g_helpDocMaxPage=CGPDFDocumentGetNumberOfPages(g_helpDocRef);
    page = CGPDFDocumentGetPage(g_helpDocRef,g_helpDocPageNum);

    CGRect cropRect = CGPDFPageGetBoxRect(page,kCGPDFCropBox);
    CGContextScaleCTM(ctx, rect.size.width / cropRect.size.width, rect.size.height/ cropRect.size.height);
    CGContextTranslateCTM(ctx,-cropRect.origin.x,-cropRect.origin.y);

    CGContextDrawPDFPage(ctx,page);

    CGPDFDocumentRelease(g_helpDocRef);

}

@end

Some global variables (prefixed g_) keep track of the pdf reference, the current page number and the max page number (I’m sure I should be doing those differently but this works for now).

The MyGetPDFDocumentRef routine loads the pdf and returns a reference to it.

The drawRect function function sets up the UIview background color, flips the co-ordinate system for the pdf display, loads the pdf and draws the current page.

You’ll notice that the drawRect function has nothing to do with the paging or the curlup/down animation. That is all handled by the swipe gesture controllers up in the helpViewController.m file. In case it is confusing, a swipe to the right means you want to go BACK a page so the right swipe handler reduces the page number (provided it does not go below 1) while a swipe to the left means you want to go to the next page so it adds one to the page number provided it does not exceed the number of pages in the document.

The gesture controller then sets up the animation (curl up or curl down) and initiates a redraw of the pdfView UIView using [self.pdfView setNeedsDisplay] which triggers the drawRect code to display whatever page is currently selected. iOS handles the actual curl up./down animation.

Categories: Coding, iPhone, xcode Tags: , ,

Stepping back in order to learn …

July 19, 2013 Leave a comment

The little iOS pdf toolkit I found was awesome and very easy to use but it was a pretty big chunk of code and more importantly for me, the look of the rendered page did not match the look I had for my app. There did not appear to be any ‘easy’ way to change the general look (color of the tool bar in particular) without reverting to changing the code so I decided to figure out how to do it myself in order to get the look I wanted, but also to force me to learn more about iOS programming.

I found some example code out there to use as a starting point as well as the Apple documentation so between that lot I did manage to figure it out in the end. Of course it’s not as  simple as just following someone else’s code as their code never quite matched my environment or goal, but the whole process of figuring out which parts of their code you need to take and use as a model for your own is how you learn this stuff.

Overall it has been a worthwhile exercise and while I am no expert yet at iOS app coding, I am a little more educated than I was a week ago.

So now I can get back to the main code for my app.

Categories: Coding, iPhone, xcode

Displaying PDF documents on iOS…

July 13, 2013 Leave a comment

I found this truly awesome (and free) toolkit (also no license limitation on use in commercial projects):

http://www.vfr.org/

I was able to implement it very easily into my project by following the tutorial:

http://mobile.tutsplus.com/tutorials/iphone/reading-displaying-pdf-documents/

There’s a few things I need to change ‘look’ wise either in it, or my own app to get a common look, but that can come later. For now I am very happy with this.

And so on to the next part which involves getting messy with Apple’s CoreMidi amongst other things although I might do some of the UI stuff first since that looks to be ‘easier’!

 

Categories: Coding, iPhone, xcode

Hello World…

May 18, 2013 Leave a comment

It has been quite an uphill battle to get this far with xcode development. This has not been helped at  all by the fact that control+click+drag does not work over a VNC connection so following the various tutorials I have found online has been ‘difficult’ to say the least. After several false starts and a couple of days of trying I finally figured out the corresponding right click and drag operations (which does work) and, following an example in the iOS programming book I got, managed to get this to work:

xcodeHellowWorld

I’m slowly getting my head around the programming model although the language itself is a complete mystery at this point. In comparison the C++ book I am reading is a piece of cake (I never thought I’d say that about any version of C!).

Categories: xcode Tags:

And they’re off. Developing in the cloud…

May 10, 2013 Leave a comment

I found this service virtualmacosx.com so I signed up for their xcode plan on a monthly basis. I got my account details this morning so I fired of my VNC viewer and logged in:

vmOSX-first-time

Very occasionally it seems to slow down as in nothing seems to be happening but that might just be me hitting the wrong button on the tool bar! But in general it seems to be pretty responsive. I might be able to tweak the vnc settings to improve the network delay but so far it seems pretty usable. I guess I’ll really find out once I do my first app.

Categories: xcode Tags: