Using TwitPic API from ObjectiveC/iPhone - iphone

There's a similar question on here about this which I've read and I've tried to follow the advice given there... and I think I'm 95% complete but the remaining 5%... well you know ;-)
So I'm trying to call the twitPic API to upload an image and I've got the image contained in a UIImageView which I'm displaying on the screen (I can see it so it's definitely there). My code to form the API call looks like this:
NSURL *url = [NSURL URLWithString:#"http://twitpic.com/api/upload"];
NSString *username = #"myUsername";
NSString *password = #"myPassword";
NSData *twitpicImage = UIImagePNGRepresentation(imageView.image);
// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:twitpicImage forKey:#"media"];
[request setPostValue:username forKey:#"username"];
[request setPostValue:password forKey:#"password"];
// Initiate the WebService request
[request start];
I get an error back from it stating 'image not found'.
Is it obvious what I'm doing wrong? Any hints at all? I'm only a week deep in ObjectiveC so it's quite likely it's a real newbie error.
On the same track - it's not clear to me how I can capture a success or failure properly in code here - I'm currently dumping the 'request responseString' to an alert which isn't the best thing - how can I check the result properly?
I've also seen the use of 'NSLog' - which I suspect is a debugging/console logging tool - but I can't see the output from this anywhere in XCode - it doesn't SEEM to be shown in the debugger - any clues at all?!
Sorry if the above is really dumb - I can take a little ridicule - but I'm kind of isolated with my iPhone adventures - no one to bounce anything off etc - so I'm venting it all off here ;-)
Cheers,
Jamie.

You need to use the setData method to copy the image data into the post, like this:
[request setData:twitPicImage forKey:#"media"];
You're making a synchronous call, which is going to stall your app while you upload all that image data - you might want to switch to using an NSOperationQueue, or the ASINetworkQueue subclass that allows you to show a progress bar.
You should be able to see NSLog output in the debugger window of XCode. Make sure you've switched to this (control top left with a spray can on). You can also launch the console.

Related

How to share without links/images/media with iOS Facebook SDK?

I've gone through the Facebook Sharing Guide, and there's one simple thing I can't seem to do (without the built-in dialog): how do I simply share a status text?
I've tried the following code after some StackOverflow browsing:
FBSDKShareAPI * shareApi = [[FBSDKShareAPI alloc]init];
shareApi.message = self.fbMessage.text;
shareApi.delegate = self;
[shareApi share];
However, that fails with FB SDK Error 2 "Share content cannot be null". Seems like I am obligated to fill in the shareApi.content property with one of the content objects: link/photo/video/multimedia. But I really don't want to attach any of these...
How do I simply post a status text message?
Ok found a solution using FBSDKGraphRequest:
NSDictionary *params = #{#"message": self.fbMessage.text};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"];
[request startWithCompletionHandler:nil];

iOS FTP Upload with SCRFTPRequest on ARC

Hy Everybody, i am using SCRFTPRequest, https://github.com/Hackmodford/SCRFTPRequest to upload a file on a FTP server, i've used it before, it worked great, but now i have a project that is with ARC, the API was recoded to ARC a couple of days ago.
I have a crash, i don't know why, it crashes without any error message after startRequest, the whole function gets executed, including startUploadRequest. I really don't know why it crashes. I've tested on iPAD 1 with 5.0 and iPad 3 with 6.1.
I've tried with other files, same problem, the file is created on the FTP server with the correct name but the size is 0 Kb.
filePath = [[NSBundle mainBundle] pathForResource:#"Spiriva Mentiones Legales" ofType:#"pdf"];
SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:#"ftp://ftp.belersoft.ro/"]
toUploadFile:filePath];
ftpRequest.username = #"user";
ftpRequest.password = #"pass";
ftpRequest.delegate = self;
ftpRequest.customUploadFileName = [modifiedPathWithXLS lastPathComponent];
[ftpRequest startRequest];
I have implemented all the Delegate Callbacks, only will ftpRequestWillStart gets called
In case anyone else having the 'zero-byte file upload' issue that needs help with this, try replacing
[ftpRequest startRequest];
with
[ftpRequest startAsynchronous];
And add the following delegate method to your code:
- (void)ftpRequest:(SCRFTPRequest *)request didChangeStatus:(SCRFTPRequestStatus)status
(note the difference between the method name versus the one listed in the SCRFTPRequest docs.)
I'm not familiar enough with this library yet to be comfortable changing the library itself, but adjusting the delegate method name and using asynchronous worked for me.
use [ftpResuest startAsynchronous]

Storing NSData in app build-path with ASIHTTPRequest

I am using ASIHTTPRequest to download tiles to be used in my MKMapView. Testing the code it downloads whatever I download into a NSData variable. (My files average 50mb) Note that this is basically almost the same as this unanswered question.
I have found that I can directly download the file from my webserver into a file e.g.:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:#"/Users/ben/Desktop/my_file.txt"];
For mapview I need the files in a folder called Tiles which is inside my app's build. It ends up in this directory:
/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/MyApp.app/Tiles
Which I found out when I called:
NSLog(#"%#",[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Tiles"]);
So what I am trying to do now is download these files (take note the plural) and put them in in the Tiles folder. To start with, I can either ZIP these images together to extract later on my device and place in the Tiles folder, or I can just go ahead and download all the images separately (which seems easier since I already know all the file-names)
I tried this piece of code and it gives me an error. It is in my mapview's viewDidLoad just before I load the tiles. Take note it is only there for testing the download-code and will later be place somewhere else to be triggered by a button.
Here is the code:
NSURL *url = [NSURL URLWithString:#"http://...my-website.../25.png"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Tiles"]]; // THE PROBLEMATIC LINE
[request startSynchronous];
NSError *error = [request error];
if(!error)
{
NSLog(#"Loaded Map");
}
else
{
NSLog(#"Error occured while loading map");
NSLog(#"%#",error);
}
And this appears in the console:
2011-06-26 21:12:09.651 MyApp[5309:707] Error occured while loading map
2011-06-26 21:12:09.669 MyApp[5309:707] Error Domain=ASIHTTPRequestErrorDomain Code=8 "Failed to delete file at path '/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/MyApp.app/Tiles'" UserInfo=0x184b20 {NSUnderlyingError=0x1d4ac0 "The operation couldn’t be completed. (Cocoa error 513.)", NSLocalizedDescription=Failed to delete file at path '/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/MyApp.app/Tiles'}
So how can I store the downloaded file in a directory on the iPhone where I can find and use it later?
Thanks for the trouble.
You can't write directly into your application bundle. You should use a directory inside your app's sandbox, such as /Documents or /Library/Caches. See Getting paths to standard application directories for how to get these paths.

IOS is there anyway to send a webpage request and forget about it on iphone ios?

I want to send a call to lets say www.test.com/updateMySqlTables.php from my app but i dont want to wait around for the proccess to complete.
something like this call
NSString *checkReturn = [NSString stringWithContentsOfURL:checkURL]
But don't want to wait for the return. I want my server to cleanup any old dates in table in the background.
I think all you need is start an asynchronous request and implement its corresponding requestDidDeceiveData: method and requestDidFinish: method, and leave these two methods blank, since you don't care about any response the request might generate. Since it's an asynchronous one, it'll automatically run in the background. Your app's main thread won't be affected.
iOS SDK has its own way to post an asynchronous request. you can also consider to use a more famous ASIHTTPRequest package to do your job, which is easier to setup and monitor.
Yes. Load the request:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"www.test.com/updateMySqlTables.php"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
And be sure to add the appropriate delegate method(s) that will be called on success/error:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// Success handling here
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Error handling here
}
One way would be to load your information in a background thread, using performSelectorInBackground:withObject: or [NSThread detachNewThreadSelector:toTarget:withObject:]. Both would require you to create a new method to load the data.
Ok, now that php tag was removed, my answer is not relative anymore.
//Erase the output buffer
ob_end_clean();
//Tell the browser that the connection's closed
header("Connection: close");
//Ignore the user's abort.
ignore_user_abort(true);
//Extend time limit to 30 minutes
set_time_limit(1800);
//Extend memory limit to 10MB
ini_set("memory_limit","10M");
//Start output buffering again
ob_start();
//Tell the browser we're serious... there's really
//nothing else to receive from this page.
header("Content-Length: 0");
//Send the output buffer and turn output buffering off.
ob_end_flush();
//Yes... flush again.
flush();
//Close the session.
session_write_close();
//Do some work
stolen from: http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/
I know that in Linux it is possible to run a php script in the background.
I guess there may be a restriction on using exec in some configurations.
exec ("/usr/bin/php yourscript.php >/dev/null &");
Which runs the php script in the background (&) and sneds output to nowhere (/dev/null)

Load static google map into UIImageView?

I'm trying to show a small static google map in my app. I'm wondering if it's possible to do this using UIImageView? It doesn't seem to be working for me but maybe I'm doing something wrong.
This is what I have (I stripped the URL so that it's easier to read):
NSURL *mapurl = [[NSURL alloc] initWithString:#"http://maps.google.com/maps/api/staticmap?[...]"];
NSData *mapdata = [[NSData alloc] initWithContentsOfURL:mapurl];
UIImage *uimap = [[UIImage alloc] initWithData:mapdata];
self.map.image = uimap; // map is my UIImageView
[mapurl release];
[mapdata release];
[uimap release];
I know my UIImageView is setup correctly because if I replace the URL with one of an image then it loads fine. Also I know the google maps URL is correct because if I load it straight into a browser it shows up fine.
I'm starting to think that this might not be possible. If anyone has any ideas or alternatives please share.
Thanks.
Well I ended up finding a solution for this so I'll post it in case someone else runs into the same issue.
First I changed the way I load the image to using NSURLConnection. I don't think this particular change makes a difference but this is what allowed me to find the problem. When trying to load the URL I got a "bad url" error. So I did some searching on that and turns out there are some invalid characters in the google static map URL that must be escaped.
So basically just call this on the url and you should be good to go:
// The URL you want to load (snipped for readability)
NSString *mapurl = #"http://maps.google.com/maps/api/staticmap?[...]";
// Escape the string
mapurl = [mapurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I haven't tested this solution with the code I have in my original post but I did test it with my new NSURLConnection implementation and it works perfectly. I assume it'll work fine with the code above as well since it was just a matter of escaping the string.