I'm trying to get data and show in tableView. As I have to send Request in viewDidLoad. But it took much time while loading my data, so I need it to be done in async way.
Any Suggestions or Recommendations.?
- (void)viewDidLoad {
//------castApi----//
[super viewDidLoad];
[[self tableView2]setDelegate:self ];
[[self tableView2]setDataSource:self];
array=[[NSMutableArray alloc]init];
NSString *castString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/movie/%#/credits?api_key=c4bd81709e87b12e609433c49",movieIDinString];
NSURL *url=[NSURL URLWithString:castString];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
connection=[NSURLConnection connectionWithRequest:request delegate:self];
if (connection){
webData= [[NSMutableData alloc]init];
}
}
Try this
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.themoviedb.org/3/movie/%#/credits?api_key=c4bd81709e87b12e609433c49",movieIDinString]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
}];
Related
I am trying to do a task which I am completely not aware of, that is video uploading to server in objective c. I am a beginner and I was using swift, but now my requirement is in objective c.
Here I have to send an asynchronous request using multipart form data to server with three values. here is the data need to send in body:
body->form-data
projectId : String
sessionId : String
file : file
Its getting crashed at this line
" [urlRequest addValue:#"65" forHTTPHeaderField:#"projectId"];"
Can anyone help me to do this.
Thanks in advance.
NSString *str = [NSString stringWithFormat:#"http://abcdefghijkl.mnopqrst.com:8965/upload"];
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[urlRequest addValue:#"65" forHTTPHeaderField:#"projectId"];
[urlRequest addValue:#"43" forHTTPHeaderField:#"sessionId"];
[urlRequest addValue:#"" forHTTPHeaderField:#"file"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(#"Error,%#", [error localizedDescription]);
} else {
NSLog(#"%#", [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]);
}
}];
You should use NSMutableURLRequest
NSMutableURLRequest is a subclass of NSURLRequest that allows you to
change the request’s properties.
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:#"65" forHTTPHeaderField:#"projectId"];
...
request = [mutableRequest copy];
I am trying to get the number of twitter followers in my iOS App.
I find some Documentation regarding this issue:
https://dev.twitter.com/rest/reference/get/users/lookup
This is the code I have so far:
NSString *apiURLString = [NSString stringWithFormat:#"https://api.twitter.com/1.1/users/lookup.json?screen_name=%#", #"barackobama"];
NSURL *apiURL = [NSURL URLWithString:apiURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:apiURL];
request.HTTPMethod = #"GET";
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// ((NSHTTPURLResponse *)response).statusCode
NSLog(#"response = %#", response);
}];
However, I am pretty confused as how to send all the other parameters required for the request.
Thanks in advance!
I am using google places api to get that place's photo but i am unable to locate "photo_reference" field in the returned JSON. Here is what i have done so far.
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/details/json?placeid=%#&key=%#",placeId,appKey];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSLog(#"Got Place Details");
NSDictionary *details = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString *photo_reference = [[[[details valueForKey:#"result"] valueForKey:#"photos"] objectAtIndex:0] valueForKey:#"photo_reference"];
NSLog(#"");
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/photo?maxwidth=302&maxheight=275&photoreference=%#&key=%#",photo_reference,appKey];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(#"Inner Request");
if (!connectionError) {
}];
}
}];
Places API documentation says that photo_reference could either be found from "Place Search" or "Place Details" requests. I got it from none.
I requested place details using place_id but the json doesnt have any "photos" key.
Same issue here
https://stackoverflow.com/questions/28317739/places-api-dont-retrieve-photos-on-detailed-petition-since-yesterday
Something change since yesterday but I don't know what or why.
EDIT: Today it's working again, maybe some random error from google.
UPDATE: Here's all the file's I'm using in my github repo. Perhaps it will help more than my code snippets:
appDelegate
mainViewController
calendarHandler
NSString* result = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:&err];
That's the line I'm using in my project to get data from a website. I run this in an NSOperationQueue and when I run with my app in the foreground, it works without issue. However I also have my app set to run in the background using performFetchWithCompletionHandler. All of my setup code works fine using the performFetch, but when it hits the line I've outlined above, the app just hangs until I bring my app into the foreground again.
How can I let this work in the background?
In my AppDelegate I have performFetchWithCompletionHandler:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
id topViewController = navigationController.topViewController;
if ([topViewController isKindOfClass:[ViewController class]])
{
[(ViewController*)topViewController autoLogin];
}
completionHandler(UIBackgroundFetchResultNewData);
}
My NSOperationQueue is built like this in my MainViewController:
- (void) autologin
{
NSOperationQueue* backgroundQueue = [NSOperationQueue new];
ch = [[myEventHandler alloc] init];
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:ch selector:#selector(runEvents:) object:login];
[backgroundQueue addOperation:operation];
}
And in MyEventHandler I have runEvents
- (void) runEvents
{
NSURL* urlRequest = [NSURL URLWithString:#"http://www.google.com"];
NSError* err = nil;
NSString* result = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:&err];
}
Try with This code:
NSString *urlString=[NSString stringWithFormat:#"%#listcontact.php?uid=%#&page=0",LocalPath,appdel.strid];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSError *error1;
NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
}];
have you tried using NSURLConnection (asynchronous) instead of stringWithContentsOfURL (synchronous)?
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]];
[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *theResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// blah blah
}];
I'm just doing exactly what I do for HTTP requests:
NSString *urlStrig = [NSString stringWithFormat:#"https://mysite.com/search.json?param1=10¶m2=String¶m3=20"];
NSURL *url = [NSURL URLWithString:urlStrig];
NSOperationQueue *queue = [NSOperationQueue new];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:Timeout];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(#"URL:%#\nResponse: %#\nStatusCode: %i\nError: %#", url, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], ((NSHTTPURLResponse *)response).statusCode, error);
}];
But all I got is a 403 status code everytime. Testing it via curl in the terminal works fine.
Is there any problem between HTTPS and NSURLConnection?