Twitter engine SA_OAuthTwitterEngine integration? - iphone

I successfully added SA_OAuthTwitterEngine in my app but I got a very strange issue. When I login in to Twitter my app will crash in:
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username
{
[_engine sendUpdate:[NSString stringWithFormat:#"This Tweet for testing!"]];
NSLog(#"Authenicated for %#", username);
}
The app crashes on [_engine sendUpdate:[NSString stringWithFormat:#"This Tweet for testing!"]];. If I use [_engine sendUpdate:nil]; instead then it will work without crash but tweet cannot be sent from my account.
When I am trying to trace on sendUpdate method then I got actual problem on [theRequest prepare]; statement in following function,
- (NSString *)_sendRequestWithMethod:(NSString *)method path:(NSString *)path queryParameters:(NSDictionary *)params body:(NSString *)body requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
{
NSString *fullPath = path;
NSString *urlString = [NSString stringWithFormat:#"%#://%#/%#", (_secureConnection) ? #"https" : #"http", _APIDomain, fullPath];
NSURL *finalURL = [NSURL URLWithString:urlString];
if (!finalURL) {
return nil;
}
OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL consumer:self.consumer token:_accessToken realm: nil signatureProvider:nil];
if (method) {
[theRequest setHTTPMethod:method];
}
[theRequest setHTTPShouldHandleCookies:NO];
// Set headers for client information, for tracking purposes at Twitter.
[theRequest setValue:_clientName forHTTPHeaderField:#"X-Twitter-Client"];
[theRequest setValue:_clientVersion forHTTPHeaderField:#"X-Twitter-Client-Version"];
[theRequest setValue:_clientURL forHTTPHeaderField:#"X-Twitter-Client-URL"];
// Set the request body if this is a POST request.
BOOL isPOST = (method && [method isEqualToString:#"POST"]);
if (isPOST) {
// Set request body, if specified (hopefully so), with 'source' parameter if appropriate.
NSString *finalBody = #"";
if (body) {
finalBody = [finalBody stringByAppendingString:body];
}
if (_clientSourceToken) {
finalBody = [finalBody stringByAppendingString:[NSString stringWithFormat:#"%#source=%#", (body) ? #"&" : #"?" , _clientSourceToken]];
}
if (finalBody) {
[theRequest setHTTPBody:[finalBody dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[theRequest prepare];
// Create a connection using this request, with the default timeout and caching policy,
// and appropriate Twitter request and response types for parsing and error reporting.
MGTwitterHTTPURLConnection *connection;
connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest delegate:self requestType:requestType responseType:responseType];
if (!connection) {
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
}
return [connection identifier];
}
The crash report is,
0 0x01f023ef in CFRelease ()
1 0x0008d04c in -[OAMutableURLRequest URLEncodedString:] (self=0x7bc673b0, _cmd=0x9ee5f, string=0x0) at /Users/ben/Dropbox/Dev/External Projects/Twitter-OAuth-iPhone/OAuthConsumeriPhoneLib/OAuthConsumerSrc/OAMutableURLRequest.m:287
2 0x0008bbe3 in -[OAMutableURLRequest prepare] (self=0x7bc673b0, _cmd=0x978cc) at /Users/ben/Dropbox/Dev/External Projects/Twitter-OAuth-iPhone/OAuthConsumeriPhoneLib/OAuthConsumerSrc/OAMutableURLRequest.m:131
3 0x00088460 in -[SA_OAuthTwitterEngine _sendRequestWithMethod:path:queryParameters:body:requestType:responseType:] (_cmd=0x96833, method=0xc0250, body=0x7b971210, requestType=<value temporarily unavailable, due to optimizations>) at SA_OAuthTwitterEngine.m:315
4 0x0007f673 in -[MGTwitterEngine sendUpdate:inReplyTo:] (self=<value temporarily unavailable, due to optimizations>, _cmd=0x96914, updateID=2073243024) at MGTwitterEngine.m:1019
5 0x0007f496 in -[MGTwitterEngine sendUpdate:] (self=0x7b932d90, _cmd=0x95e02, status=0xc0510) at MGTwitterEngine.m:995
6 0x0007bf3f in -[DMUAppDelegate OAuthTwitterController:authenticatedWithUsername:] (self=0x7b918a20, _cmd=0x96152) at DMUAppDelegate.m:572
7 0x0008503b in -[SA_OAuthTwitterController gotPin:] (_cmd=0x975b4, pin=0x7ae573e0) at SA_OAuthTwitterController.m:125
8 0x00086137 in -[SA_OAuthTwitterController webViewDidFinishLoad:] (self=0x7b944a00, _cmd=0xf070e2, webView=0x7ae46750) at SA_OAuthTwitterController.m:224
9 0x00be036a in -[UIWebView webView:didFinishLoadForFrame:] ()
10 0x00be1956 in -[UIWebViewWebViewDelegate webView:didFinishLoadForFrame:] ()
11 0x01f5c51d in __invoking___ ()
12 0x01f5c437 in -[NSInvocation invoke] ()
13 0x01f8749a in -[NSInvocation invokeWithTarget:] ()
14 0x03462aff in -[_WebSafeForwarder forwardInvocation:] ()
15 0x01f5d0c9 in ___forwarding___ ()
16 0x01f5cce2 in __forwarding_prep_0___ ()
17 0x01f5c51d in __invoking___ ()
18 0x01f5c437 in -[NSInvocation invoke] ()
19 0x04005ae3 in SendMessage ()
20 0x04006115 in HandleDelegateSource ()
21 0x01fca97f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
22 0x01f2db73 in __CFRunLoopDoSources0 ()
23 0x01f2d454 in __CFRunLoopRun ()
24 0x01f2cdb4 in CFRunLoopRunSpecific ()
25 0x01f2cccb in CFRunLoopRunInMode ()
26 0x01e60879 in GSEventRunModal ()
27 0x01e6093e in GSEventRun ()
28 0x00a29a9b in UIApplicationMain ()
29 0x00040296 in main (argc=1, argv=0xc003c604) at /Users/dhirenshah/Desktop/Projects/Projects/DMU/DMU/main.m:16
(gdb)

Yes, at last i solve my problem. i Just replacing code,
- (SA_OAuthTwitterEngine *) initOAuthWithDelegate: (NSObject *) delegate
{
if (self = (id) [super initWithDelegate: delegate]) {
self.requestTokenURL = [NSURL URLWithString: #"https://twitter.com/oauth/request_token"];
self.accessTokenURL = [NSURL URLWithString: #"https://twitter.com/oauth/access_token"];
self.authorizeURL = [NSURL URLWithString: #"https://twitter.com/oauth/authorize"];
}
return self;
}
Modifying SA_OAuthTwitterEngine.m function. I got help from MGTwitterEngine crash in iOS5, this link.

I'm running iOS 6.1.3 and my device for testing is iPhone 4s. I got the same error/crash that xcode pointed the crash to [theRequest prepare]; method.
My fixed was:
OAMutableURLRequest *theRequest = [[[OAMutableURLRequest alloc]
initWithURL:finalURL
consumer:self.consumer
token:_accessToken
realm: nil
signatureProvider:nil] autorelease];
putting the autorelease on this line and checking again [_engine isAuthorized] before posting tweet:
if ([_engine isAuthorized])
{
NSLog(#"post Tweet");
. . . . . .
} else {
NSLog(#"cant post Tweet");
. . . . . .
}
I can't expalin further more, since i'm not yet familiar with this but this works for me.

I have made changes in the above code, because twitter api have been changed so the old one will not work.
And for any new users if they are using the above format with MGTwitterEnginer, it will not work, you will need to replace
self.requestTokenURL = [NSURL URLWithString: #"https://api.twitter.com/oauth/request_token"];
self.accessTokenURL = [NSURL URLWithString: #"https://api.twitter.com/oauth/access_token"];
self.authorizeURL = [NSURL URLWithString: #"https://api.twitter.com/oauth/authorize"];

Related

NSURLConnection won't respond after I get Error Domain=NSPOSIXErrorDomain Code=57

So here's the brief description of the issue:
I have an app that uses web sockets SocketRocket - client-side, Nodejs + Socket.io - server side.
I establish a socket connection upon launch of the application and everything works just fine. In case i suspend the app (going to background, locking screen, etc) I'm able to reconnect upon entering foreground.
However if i lose connection (for example, if i block myself on the router (i get a socket failure error in 5-10 seconds), then unblock myself) - i won't be able to connect to ANY url with NSURLConnection. (Why am I bothering with this? Well, people tend to loose often on mobile devices, so I have to handle this error)
This code is getting called upon connectivity restore/entering foreground if i previously had a working connection.
-(void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary*)params withNamespace:(NSString *)endpoint
{
if (!_isConnected && !_isConnecting)
{
_isConnecting = YES;
_host = host;
_port = port;
_endpoint = [endpoint copy];
NSMutableString *query = [[NSMutableString alloc] initWithString:#""];
[params enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
[query appendFormat:#"&%#=%#",key,value];
}];
NSString *s = [NSString stringWithFormat:HANDSHAKE_URL, _host, _port, rand(), query];
[self log:[NSString stringWithFormat:#"Connecting to socket with URL: %#",s]];
_timeout = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(disconnect) userInfo:nil repeats:NO];
[[LRResty client] get:s withBlock:^(LRRestyResponse *response) {
[_timeout invalidate], _timeout = nil;
if ( response.status == 200 )
{
NSString *responseString = [response asString];
[self log:[NSString stringWithFormat:#"requestFinished() %#", responseString]];
NSArray *data = [responseString componentsSeparatedByString:#":"];
_sid = [data objectAtIndex:0];
[self log:[NSString stringWithFormat:#"sid: %#", _sid]];
_heartbeatTimeout = [[data objectAtIndex:1] floatValue] + 7.0;
[self log:[NSString stringWithFormat:#"heartbeatTimeout: %f", _heartbeatTimeout]];
NSString *t = [data objectAtIndex:3];
NSArray *transports = [t componentsSeparatedByString:#","];
[self log:[NSString stringWithFormat:#"transports: %#", transports]];
[self openSocket];
}
else
{
[_delegate socketIOHandshakeFailed:self];
}
}];
}
}
That's what i get in a normal case:
Connecting to socket with URL: https://example.com:9001/socket.io/1/?t=282442
requestFinished() ==> 843717395328441553:60:60:websocket
debug: Finished request GET https://example.com:9001/socket.io/1/?t=282442 <LRRestyRequest>
In case i try to reconnect after connection failure:
Connecting to socket with URL: https://example.com:9001/socket.io/1/?t=282475249
I tried running a synchronous request, yet it would block my UI and will never finish. So, actually request is probably trying to start, but never gets to the point of execution.
This is what i get if i pause an app in the debugger:
you can see the screenshot here -> http://avvs.co/static/app_paused.jpg
Check thread 7 -
#0 0x9855fc22 in mach_msg_trap ()
#7 0x026852d3 in -[NSRunLoop(NSRunLoop) run] ()
#8 0x00019b1b in -[LRURLRequestOperation start]
#9 0x00016670 in -[LRRestyRequest start]
UPDATE
this is the complete operations list for this thread, would be really glad if you can point out what's wrong in here
#0 0x9855fc22 in mach_msg_trap ()
#1 0x9855f1f6 in mach_msg ()
#2 0x0073c10a in __CFRunLoopServiceMachPort ()
#3 0x0069f5d5 in __CFRunLoopRun ()
#4 0x0069ed84 in CFRunLoopRunSpecific ()
#5 0x0069ec9b in CFRunLoopRunInMode ()
#6 0x0268540f in -[NSRunLoop(NSRunLoop) runMode:beforeDate:] ()
#7 0x026852d3 in -[NSRunLoop(NSRunLoop) run] ()
#8 0x00019b0b in -[LRURLRequestOperation start]
#9 0x00016660 in -[LRRestyRequest start]
I've finally found the solution. Actual problem was not in the NSURLConnection, but in the socket implementation. When loosing connection, app was closing input and output streams, but they were not removed from the running loop, nor were they dispatched. By fixing it I was able to restore connectivity.

Converting from ASIHTTPRequest to AFNetworking

I am converting my app routines from ASIHTTP to AFNetworking due to the unfortunate discontinuation of work on that project ... and what I found out later to be the much better and smaller codebase of AFNetworking.
I am finding several issues. My code for ASIHTTPRequest is built as a method. This method takes a few parameters and posts the parameters to a url ... returning the resulting data. This data is always text, but in the interests of making a generic method, may sometimes be json, sometimes XML or sometimes HTML. Thus I built this method as a standalone generic URL downloader.
My issue is that when the routine is called I have to wait for a response. I know all the "synchronous is bad" arguments out there...and I don't do it a lot... but for some methods I want synchronous.
So, here is my question. My simplified ASIHTTP code is below, followed by the only way i could think of coding this in AFNetworking. The issue I have is that the AFNetworking sometimes does not for the response before returning from the method. The hint that #mattt gave of [operation waitUntilFinished] totally fails to hold the thread until the completion block is called... and my other method of [queue waitUntilAllOperationsAreFinished] does not necessarily always work either (and does NOT result in triggering the error portion of the [operation hasAcceptableStatusCode] clause). So, if anyone can help, WITHOUT The ever-present 'design it asynchronously', please do.
ASIHTTP version:
- (NSString *) queryChatSystem:(NSMutableDictionary *) theDict
{
NSString *response = [NSString stringWithString:#""];
NSString *theUrlString = [NSString stringWithFormat:#"%#%#",kDataDomain,kPathToChatScript];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:theUrlString]];
for (id key in theDict)
{
[request setPostValue:[theDict objectForKey:key] forKey:key];
}
[request setNumberOfTimesToRetryOnTimeout:3];
[request setAllowCompressedResponse:YES];
[request startSynchronous];
NSError *error = [request error];
if (! error)
{
response = [request responseString];
}
return response;
}
AFNetworking version
- (NSString *) af_queryChatSystem:(NSMutableDictionary *) theDict
{
NSMutableDictionary *theParams = [NSMutableDictionary dictionaryWithCapacity:1];
for (id key in theDict)
{
[theParams setObject:[theDict objectForKey:key] forKey:key];
}
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:kDataDomain]];
NSMutableURLRequest *theRequest = [httpClient requestWithMethod:#"POST" path:[NSString stringWithFormat:#"/%#",kPathToChatScript] parameters:theParams];
__block NSString *responseString = [NSString stringWithString:#""];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:theRequest] autorelease];
operation.completionBlock = ^ {
if ([operation hasAcceptableStatusCode]) {
responseString = [operation responseString];
NSLog(#"hasAcceptableStatusCode: %#",responseString);
}
else
{
NSLog(#"[Error]: (%# %#) %#", [operation.request HTTPMethod], [[operation.request URL] relativePath], operation.error);
}
};
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];
[queue waitUntilAllOperationsAreFinished];
[httpClient release];
return responseString;
}
Thanks very much for any ideas.
- (void)af_queryChatSystem:(NSMutableDictionary *) theDict block:(void (^)(NSString *string))block {
...
}
Now within the completionBlock do:
block(operation.responseString);
block will act as the delegate for the operation. remove
-waitUntilAllOperationsAreFinished
and
return responseString
You call this like:
[YourInstance af_queryChatSystem:Dict block:^(NSString *string) {
// use string here
}];
Hope it helps. You can refer to the iOS example AFNetworking has
I strongly recommend to use this opportunity to convert to Apple's own NSURLConnection, rather than adopt yet another third party API. In this way you can be sure it won't be discontinued. I have found that the additional work required to get it to work is minimal - but it turns out to be much more robust and less error prone.
My solution is manually to run the current thread runloop until the callback have been processed.
Here is my code.
- (void)testRequest
{
MyHTTPClient* api = [MyHTTPClient sharedInstance]; // subclass of AFHTTPClient
NSDictionary* parameters = [NSDictionary dictionary]; // add query parameters to this dict.
__block int status = 0;
AFJSONRequestOperation* request = [api getPath:#"path/to/test"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// success code
status = 1;
NSLog(#"succeeded");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure
status = 2;
NSLog(#"failed");
}];
[api enqueueHTTPRequestOperation:request];
[api.operationQueue waitUntilAllOperationsAreFinished];
while (status == 0)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate date]];
}
STAssertEquals(status, 1, #"success block was executed");
}

iPhone app crashes if view is swapped during data download

I have an iphone app that when the user clicks a row in a uitable, it takes the row value and downloads some data from the web to populate the next view. However if the user switches back to the first view when the data is being downloaded the app crashes. I think i've found the problem but need some help fixing it:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
BlogRssParser *blogRss = [[BlogRssParser alloc] init];
blogRss.terms = [[selectedObject valueForKey:#"data"] description];
RssFunViewController *rssFun = [[RssFunViewController alloc] initWithNibName:#"RssFunViewController" bundle:nil];
rssFun.rssParser = blogRss;
[blogRss release];
[self.navigationController pushViewController:rssFun animated:YES];
rssFun.navigationItem.title=blogRss.terms;
[rssFun release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
So where it says [self.navigationController pushViewController:rssFun animated:YES]; this is where it crashes because once it finishes the download this is the next line of code and it can push a view if its not on the right screen if that makes any sense!? Thanks for any advice anyway!
BlogRssParser:
-(BOOL)fetchAndParseRss{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//To suppress the leak in NSXMLParser
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSString *urlTerm = terms;
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:#" " withString:#"+"];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:#"\t" withString:#""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:#"&" withString:#""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:#"'" withString:#""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:#"-" withString:#""];
urlTerm = [urlTerm stringByReplacingOccurrencesOfString:#"_" withString:#""];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"xxxxxxxxxxxxx/app.php?s=%#", urlTerm]];
NSLog(#"%#", url);
BOOL success = NO;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
[parser setShouldReportNamespacePrefixes:YES];
[parser setShouldResolveExternalEntities:NO];
success = [parser parse];
[parser release];
[pool drain];
return success;
}
Console:
2010-12-06 19:15:09.826 Example[452:207] -[NSCFString processCompleted]: unrecognized selector sent to instance 0x6123d30
2010-12-06 19:15:09.855 Example[452:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString processCompleted]: unrecognized selector sent to instance 0x6123d30'
*** Call stack at first throw:
(
0 CoreFoundation 0x02664b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x027b440e objc_exception_throw + 47
2 CoreFoundation 0x026666ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x025d62b6 ___forwarding___ + 966
4 CoreFoundation 0x025d5e72 _CF_forwarding_prep_0 + 50
5 Foundation 0x000423ca __NSThreadPerformPerform + 251
6 CoreFoundation 0x02645faf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
7 CoreFoundation 0x025a439b __CFRunLoopDoSources0 + 571
8 CoreFoundation 0x025a3896 __CFRunLoopRun + 470
9 CoreFoundation 0x025a3350 CFRunLoopRunSpecific + 208
10 CoreFoundation 0x025a3271 CFRunLoopRunInMode + 97
11 GraphicsServices 0x02f4300c GSEventRunModal + 217
12 GraphicsServices 0x02f430d1 GSEventRun + 115
13 UIKit 0x002d1af2 UIApplicationMain + 1160
14 Example 0x0000244a main + 84
15 Example 0x000023ed start + 53
)
terminate called after throwing an instance of 'NSException'
unrecognized selector means that you've attempted to send a message to an object that doesn't know how to handle it.
For example, suppose you had a class AlienParser and it had two methods: land and probe. You create an instance of it called myParser, and then tried to call [myParser destroyAllHumans]. The resulting object wouldn't know what to do, and you'd get an exception thrown. It compiles because you can send any message to anything with Obj-C, because at runtime it may know how to handle it even if the compiler couldn't detect so.
Somewhere (the hex is your clue, it doesn't show a full backtrace) you've got some code calling another object with a message it just plain doesn't support. It's probably worth mentioning that ANY message to nil does nothing and returns nil so you've obviously got an actual object there you are sending messages to.
Have you tried downloading the XML in a background thread? This may alleviate some of the issues as the main thread won't be blocked. You should be able to push on the RssFunViewController while the XML is/being downloaded.

asi-http-request asynchronous selector error

I keep getting a very weird error when using asi-http-request when I use selectors to register callbacks for success and failure.
The following methods are used:
// Sets up a request and takes care of the stuff that needs to be for each
// and every request.
- (void) setup:(NSString *) fragment successCallback:(SEL) success_callback failureCallback:(SEL) failure_callback {
// Convert the string to a url
NSString *url = [[NSString alloc] initWithFormat:#"%#%#",
self.server.serverUrl,
fragment
];
NSURL *full_url = [NSURL URLWithString: url];
// Time to setup the request
self.curRequest = [ASIFormDataRequest requestWithURL:full_url];
[self.curRequest setDelegate:self];
[self.curRequest setDidFailSelector:success_callback];
[self.curRequest setDidFinishSelector:failure_callback];
// If we're using iphone os version 4.0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
[self.curRequest setShouldContinueWhenAppEntersBackground:YES];
#endif
// Clean up time!
[url release];
[full_url release];
}
The register method
- (void) register_user:(NSString *) email password:(NSString *) password confirmPassword:(NSString *) confirm_password successCallback:(SEL) success_callback failureCallback:(SEL) failure_callback {
[self setup:#"/users/register"
successCallback:success_callback
failureCallback:failure_callback
];
// Setup the input to the method
[self.curRequest setPostValue:email forKey: #"email"];
[self.curRequest setPostValue:password forKey: #"password"];
[self.curRequest setPostValue:confirm_password forKey: #"confirm_password"];
NSLog(#"We got here yo :)~ %# %# %#", email, password, confirm_password);
[self.curRequest startAsynchronous];
}
Success and Failure Callbacks
- (void) registerSuccess: (ASIHTTPRequest *) request {
NSString *response = [request responseString];
NSLog(#"Response string: %s", response);
}
// Called when the http request fails. That means it could not
// reach the server for an unknown reason.
- (void) registerFailure: (ASIHTTPRequest *) request {
NSError *error = [request error];
NSLog(#"Register error: %s", [error localizedFailureReason]);
}
I invoke the register method like so:
[self.appdelegate.userModel register_user:self.emailAddress.text
password:self.password.text
confirmPassword:self.confirmPassword.text
successCallback:#selector(registerSuccess:)
failureCallback:#selector(registerFailure:)
];
The callstack and error from gdb:
2010-12-02 12:33:56.889 ProjectPrototype[2201:6003] -[__NSCFType absoluteURL]: unrecognized selector sent to instance 0x6457c60
2010-12-02 12:33:56.891 ProjectPrototype[2201:6003] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType absoluteURL]: unrecognized selector sent to instance 0x6457c60'
*** Call stack at first throw:
(
0 CoreFoundation 0x0292eb99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02a7e40e objc_exception_throw + 47
2 CoreFoundation 0x029306ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x028a02b6 ___forwarding___ + 966
4 CoreFoundation 0x0289fe72 _CF_forwarding_prep_0 + 50
5 CoreFoundation 0x02843b04 CFURLCopyAbsoluteURL + 100
6 CFNetwork 0x023fe5e8 _ZN11HTTPMessage10initializeEPK10__CFStringPK7__CFURLS2_ + 52
7 CFNetwork 0x023fe50c CFHTTPMessageCreateRequest + 80
8 ProjectPrototype 0x00011360 -[ASIHTTPRequest main] + 852
9 Foundation 0x000c83ca __NSThreadPerformPerform + 251
10 CoreFoundation 0x0290ffaf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
11 CoreFoundation 0x0286e39b __CFRunLoopDoSources0 + 571
12 CoreFoundation 0x0286d896 __CFRunLoopRun + 470
13 CoreFoundation 0x0286d350 CFRunLoopRunSpecific + 208
14 CoreFoundation 0x02870614 CFRunLoopRun + 84
15 ProjectPrototype 0x00023b55 +[ASIHTTPRequest runRequests] + 173
16 Foundation 0x000b376c -[NSThread main] + 81
17 Foundation 0x000b36f8 __NSThread__main__ + 1387
18 libSystem.B.dylib 0x9689881d _pthread_start + 345
19 libSystem.B.dylib 0x968986a2 thread_start + 34
)
terminate called after throwing an instance of 'NSException'
I appreciate any and all help, thanks alot :)~
Dudester you have two serious syntax priblems:
First, each use of this is incorrect:
self.curRequest
The first time only you set request, use self.request = blah.
After that, just use [curRequest xyz]
Second serious problem,
[self.curRequest setDidFailSelector:success_callback];
[self.curRequest setDidFinishSelector:failure_callback];
You forgot the 'selector' bit, it should look more like this:
[request setDidFinishSelector:#selector(blahCommuniqueDone:)];
[request setDidFailSelector:#selector(blahCommuniqueDoneProblem:)];
(Be careful with the colons, also.) Forget about trying to pass them in as SELs. The two routines are right there in the same object. You simply point to them.
Here's a typical complete working example. If you follow this you'll have no problems. These three routines just sit in the same file.
-(void) blahCommunique
{
// it sends the string blah for pid (even if blank)
// from V5 it sends the blah as well ..
NSURL *url = [NSURL URLWithString:#"https://blah?blah"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:blahAA forKey:#"blahAA"];
[request setPostValue:blahBB forKey:#"blahBB"];// new in V5
[request setPostValue:SIMPLESTRINGTHISVERSION forKey:#"version"];
#define QQ(a) [NSString stringWithFormat:#"%d", a]
[request setPostValue:QQ(vfourblahCount) forKey:#"cr"];
[request setPostValue:QQ(vfourblahCount) forKey:#"sb"];
[request setPostValue:QQ(vfourblahCount) forKey:#"so"];
[request setPostValue:QQ(vfourblahCount) forKey:#"lc"];
[request setPostValue:QQ(OLDlaunchCount) forKey:#"olc"];
#undef QQ
[request setPostValue:[[NSLocale preferredLanguages] objectAtIndex:0] forKey:#"langpref"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(statsCommuniqueDone:)];
[request setDidFailSelector:#selector(statsCommuniqueDoneProblem:)];
[request startAsynchronous];
}
-(void) statsCommuniqueDone:(ASIHTTPRequest *)request
{
NSData *newStringData = [request responseData];//todo, check [incomingFloatData length]
self.factoryMessageIfAny =
[[[NSString alloc] initWithData:newStringData encoding:NSUTF8StringEncoding] autorelease];
if ( [self.factoryMessageIfAny isEqualToString:#"OK"] )
self.factoryMessageIfAny = #"";
}
-(void) statsCommuniqueDoneProblem:(ASIHTTPRequest *)request
{
// statsCommuniqueDoneProblem ... !
self.factoryMessageIfAny = #"";
}

Save and Retrieve of an UIImage on CoreData

In my app, I am trying to save and retrieval of an image in core data. I am able to save an image successfully after convention of UIimage into NSData, But when I am trying to get an image as NSData it shows output as given below,
case 1: When trying to display as a string from DB.
<Event: 0x5b5d610> (entity: Event; id: 0x5b5ce30 <x-coredata://F51BBF1D-6484-4EB6-8583-147E23D9FF7B/Event/p1> ; data: <fault>)
case 2: When trying to display as Data
[Event length]: unrecognized selector sent to instance 0x5b3a9c0
2010-07-28 19:11:59.610 IMG_REF[10787:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Event length]: unrecognized selector sent to instance 0x5b3a9c0'
My code,
to save:
NSManagedObjectContext *context = [self managedObjectContext];
newsObj = [NSEntityDescription insertNewObjectForEntityForName:#"Event" inManagedObjectContext:context];
NSURL *url = [NSURL URLWithString:#"http://www.cimgf.com/images/photo.PNG"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
uiImage = [UIImage imageWithData:data];
NSData * imageData = UIImagePNGRepresentation(uiImage);
[newsObj setValue:imageData forKey:#"imgPng"];
NSError *error;
#try{
if (managedObjectContext != nil) {
if (![managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
NSString * infoString = [NSString stringWithFormat:#"Please check your connection and try again."];
UIAlertView * infoAlert = [[UIAlertView alloc] initWithTitle:#"Database Connection Error" message:infoString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[infoAlert show];
[infoAlert release];
}
}
}#catch (NSException *exception) {
NSLog(#"inside exception");
}
to retrieve,
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity1 = [NSEntityDescription entityForName:#"Event" inManagedObjectContext:context];
[fetchRequest setEntity:entity1];
NSError *error;
NSArray * array = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (array == nil) {
NSLog(#"Testing: No results found");
}else {
NSLog(#"Testing: %d Results found.", [array count]);
}
NSData * dataBytes = [[array objectAtIndex:0] data];
image = [UIImage imageWithData:dataBytes];
[fetchRequest release];
}
#catch (NSException *exception) {
NSLog(#"inside exception");
}
Error:
Testing: 3 Results found.
2010-07-28 23:27:51.343 IMG_REF[11657:207] -[Event data]: unrecognized selector sent to instance 0x5e22ce0
2010-07-28 23:27:51.344 IMG_REF[11657:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Event data]: unrecognized selector sent to instance 0x5e22ce0'
*** Call stack at first throw:
(
0 CoreFoundation 0x02566919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x026b45de objc_exception_throw + 47
2 CoreFoundation 0x0256842b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x024d8116 ___forwarding___ + 966
4 CoreFoundation 0x024d7cd2 _CF_forwarding_prep_0 + 50
5 IMG_REF 0x00003b06 -[IMG_REFViewController showAction] + 353
6 UIKit 0x002bae14 -[UIApplication sendAction:to:from:forEvent:] + 119
7 UIKit 0x004c214b -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
8 UIKit 0x002bae14 -[UIApplication sendAction:to:from:forEvent:] + 119
9 UIKit 0x003446c8 -[UIControl sendAction:to:forEvent:] + 67
10 UIKit 0x00346b4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
11 UIKit 0x003456f7 -[UIControl touchesEnded:withEvent:] + 458
12 UIKit 0x002de2ff -[UIWindow _sendTouchesForEvent:] + 567
13 UIKit 0x002c01ec -[UIApplication sendEvent:] + 447
14 UIKit 0x002c4ac4 _UIApplicationHandleEvent + 7495
15 GraphicsServices 0x02dccafa PurpleEventCallback + 1578
16 CoreFoundation 0x02547dc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
17 CoreFoundation 0x024a8737 __CFRunLoopDoSource1 + 215
18 CoreFoundation 0x024a59c3 __CFRunLoopRun + 979
19 CoreFoundation 0x024a5280 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x024a51a1 CFRunLoopRunInMode + 97
21 GraphicsServices 0x02dcb2c8 GSEventRunModal + 217
22 GraphicsServices 0x02dcb38d GSEventRun + 115
23 UIKit 0x002c8b58 UIApplicationMain + 1160
24 IMG_REF 0x00002aac main + 102
25 IMG_REF 0x00002a3d start + 53
)
terminate called after throwing an instance of 'NSException'
Note: Above error is coming when going to execute NSData * dataBytes = [[array objectAtIndex:0] data]; line.
Data Model http://www.freeimagehosting.net/uploads/7c286931cc.png
I spent a lot of time with this. Please help me out!
Not sure if you've gotten this straightened out yet, but I am able to save/retrieve UIImage objects in Core Data as follows:
To save:
NSData *imageData = UIImagePNGRepresentation(yourUIImage);
[newManagedObject setValue:imageData forKey:#"image"];
and to load:
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:#"image"]];
[[newCustomer yourImageView] setImage:image];
Hope this helps. I am using a UITableView with Core Data and pulling the image from the database in my tableView:didSelectRowAtIndexPath: method.
Here's the proper solution that works very well.
1) Storing UIImage with Core Data:
NSData* coreDataImage = [NSData dataWithData:UIImagePNGRepresentation(delegate.dancePhoto)];
Make sure that "coreDataImage" is of type NSData. You have to set type to "Binary data" for "coreDataImage" in your model.
2) Retrieving UIImage from Core Data:
UIImage* image = [UIImage imageWithData:selectedDance.danceImage];
That's all there's to it, works great.
When you retrieve the image, you're performing the fetch request and storing the results in the variable array, meaning array holds an NSArray of Event objects. Then, later, you assign:
dataBytes = [array objectAtIndex:0];
This means that dataBytes, which you declared as NSData, is now actually an instance of Event. Then when you go to initialize the image, part of the implementation of imageWithData: calls length on what it expects to be your NSData object, but is actually an Event object, hence the error message.
You should adjust your code to read:
dataBytes = [[array objectAtIndex:0] imgPng];
That way, you're getting the first Event object out of the array, then fetching its imgPng property (an instance of NSData, which is what you want).
As a side note, your declaration of dataBytes using the alloc-init on the line above may be extraneous, since you change dataBytes to be the data from your Event immediately afterwards.
The Solution I used was to create the category bellow. You just need it in your project for it to work. Using this storing images works just like you where storing an NSData
UIImage+NSCoding.h
#interface UIImage (UIImage_NSCoding) <NSCoding>
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
#end
UIImage+NSCoding.m
#import "UIImage+NSCoding.h"
#implementation UIImage (UIImage_NSCoding)
- (void)encodeWithCoder:(NSCoder *)aCoder
{
NSData *imageData = UIImagePNGRepresentation(self);
[aCoder encodeDataObject:imageData];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
[self autorelease];
NSData* imageData = [aDecoder decodeDataObject];
self = [[UIImage alloc] initWithData:imageData];
return self;
}
#end
This sample code has example how to store UIImage in Core Data:
https://developer.apple.com/library/ios/#samplecode/PhotoLocations/Introduction/Intro.html