Skip to content
Browse files

added a share button

  • Loading branch information...
1 parent 51c4396 commit 054d67ca99d25418b6aee7f483b5d1fdb3b69292 @jchris jchris committed May 14, 2011
Showing with 37 additions and 9 deletions.
  1. +3 −2 OnTheSpot/RootViewController.h
  2. +34 −7 OnTheSpot/RootViewController.m
View
5 OnTheSpot/RootViewController.h
@@ -27,9 +27,10 @@
NSMutableArray* _locationSamples;
NSMutableArray* _headingSamples;
NSTimer* _sampleTimer;
- NSURL *_dbURL;
+ NSURL *_couchURL;
}
-@property (nonatomic, retain) NSURL *dbURL;
+@property (nonatomic, retain) NSURL *couchURL;
+@property(nonatomic, retain)UIBarButtonItem *syncItem;
- (IBAction)takePicture;
View
41 OnTheSpot/RootViewController.m
@@ -26,7 +26,8 @@ @implementation RootViewController
@synthesize sampleTimer = _sampleTimer;
@synthesize images = _images;
-@synthesize dbURL=_dbURL;
+@synthesize couchURL=_couchURL;
+@synthesize syncItem;
- (id)initWithCoder:(NSCoder *)aDecoder
{
@@ -43,10 +44,35 @@ - (id)initWithCoder:(NSCoder *)aDecoder
}
- (void)couchbaseDidStart:(NSURL *)serverURL {
- self.dbURL = [serverURL URLByAppendingPathComponent:@"spot"];
+ self.couchURL = serverURL;
+ self.syncItem = [[[UIBarButtonItem alloc]
+ initWithTitle:@"Share" style:UIBarButtonItemStyleBordered
+ target:self
+ action:@selector(sync)
+ ] autorelease];
+ self.navigationItem.rightBarButtonItem = self.syncItem;
+ self.navigationItem.rightBarButtonItem.enabled = YES;
NSLog(@"Couch is ready!");
}
+-(void)sync
+{
+// self.syncItem = self.navigationItem.rightBarButtonItem;
+ NSString *theJSON = @"{\"source\":\"spot\",\"target\":\"http://jchris.iriscouch.com/spot\"}";
+ NSNumber *contentLength = [NSNumber numberWithUnsignedInt: [theJSON length]];
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self.couchURL URLByAppendingPathComponent:@"_replicate"]];
+ assert(request != nil);
+ // create the Document
+ [request setHTTPMethod: @"POST"];
+ [request setHTTPBody: [theJSON dataUsingEncoding: NSUTF8StringEncoding]];
+ [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
+ [request setValue:[contentLength description] forHTTPHeaderField:@"Content-Length"];
+
+ NSData *replicate = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
+
+ NSLog(@"replicate %@", [[NSString alloc] initWithData:replicate encoding:NSUTF8StringEncoding]);
+}
+
- (void)viewDidLoad
{
[super viewDidLoad];
@@ -192,7 +218,7 @@ - (void)dealloc
{
[_motionManager release];
[_locationManager release];
- [_dbURL release];
+ [_couchURL release];
[super dealloc];
}
@@ -332,9 +358,9 @@ - (void)storeImage:(UIImage*)anImage withMetaData:(NSDictionary*)metaData
else {
NSString *jsonString = [[NSString alloc] initWithData:theJSON encoding:NSUTF8StringEncoding];
NSLog(@"Saving JSON %@", jsonString);
- NSLog(@"Couch URL %@", self.dbURL);
+ NSLog(@"Couch URL %@", self.couchURL);
NSNumber *contentLength = [NSNumber numberWithUnsignedInt: [theJSON length]];
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.dbURL];
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self.couchURL URLByAppendingPathComponent:@"spot"]];
assert(request != nil);
// create the Database, just in case
@@ -358,8 +384,9 @@ - (void)storeImage:(UIImage*)anImage withMetaData:(NSDictionary*)metaData
NSData *photo = UIImageJPEGRepresentation(anImage, 0.35);
contentLength = [NSNumber numberWithUnsignedInt: [photo length]];
- NSString *urlString = [NSString stringWithFormat:@"%@/%@/%@?rev=%@",
- [self.dbURL absoluteString],
+ NSString *urlString = [NSString stringWithFormat:@"%@/%@/%@/%@?rev=%@",
+ [self.couchURL absoluteString],
+ @"spot",
[parsedJSON valueForKey:@"id"],
@"photo.jpg",
[parsedJSON valueForKey:@"rev"]];

0 comments on commit 054d67c

Please sign in to comment.
Something went wrong with that request. Please try again.