I am trying to download a PFFile which is a tfss file(?). It gets opened as a text file. This is the code I use for retrieveing the data:
-(void) queryVideoWithDictionary:(NSDictionary *)dictionary {
PFQuery *videoQuery = [PFQuery queryWithClassName:#"EventVideos"];
[videoQuery whereKey:#"objectId" equalTo:[dictionary objectForKey:#"videoId"]];
[videoQuery findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
if (!error) {
PFFile *videoFile = [results[0] objectForKey:#"video"];
[videoFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSURL *movieURL = [NSURL fileURLWithPath:dataString];
NSMutableDictionary *videoDictionary = [NSMutableDictionary dictionaryWithObject:data forKey:[dictionary objectForKey:#"videoId"]];
[self.videoArray addObject:videoDictionary];
NSLog(#"video array count: %ld", self.videoArray.count);
NSLog(#"dataString: %#", dataString);
// NSLog(#"movieURL: %#", movieURL);
[self.player setItemByStringPath:dataString];
[self.player play];
});
}];
} else {
NSLog(#"error");
}
}];
}
When dataString gets logged, it returns null, so I don't know how to get the data and play it as a video. It is saved as a QuickTimeMovie by the way.
I use SCRecorder to play and record videos.
Edit: data is definitely not nil, I still don't know how to solve this issue.
Related
I have a problem with uploading profile picture from facebook to my API and saving to Parse. This code is from Udemy. I'm using Xcode 7.2.
Error:
2016-01-18 10:14:15.296 MatchedUp[14381:5418567] Error in FB request Error Domain=com.facebook.sdk Code=5 "(null)" UserInfo={com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ErrorSessionKey=<PFReceptionist: 0x12663d2e0>, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
"fbtrace_id" = "FXTRrxOh0+c";
message = "(#100) Tried accessing nonexisting field (photo) on node type (User)";
type = OAuthException;
};
};
code = 400;
}}
Code:
-(void)updateUserInformation
{
//
FBRequest *request = [FBRequest requestForGraphPath:#"me/?fields=name,birthday,first_name,location,gender,interested_in,photo"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error){
NSDictionary *userDictionary = (NSDictionary *)result;
//create URL
NSString *facebookID = userDictionary[#"id"];
NSLog(#"%#",result);
NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1", facebookID]];
NSData *imageData = [NSData dataWithContentsOfURL:pictureURL];
// UIImage *fbImage = [UIImage imageWithData:imageData];
NSLog(#"%#",result);
NSMutableDictionary *userProfile = [[NSMutableDictionary alloc] initWithCapacity:8];
NSLog(#"%#",result);
if(userDictionary[#"name"]){
userProfile[kCCUserProfileNameKey] = userDictionary[#"name"];
}
if(userDictionary[#"user_birthday"]){
userProfile[kCCUserProfileBirthdayKey] = userDictionary[#"user_birthday"];
}
if(userDictionary[#"first_name"]){
userProfile[kCCUserProfileFirstNameKey] = userDictionary[#"first_name"];
}
if(userDictionary[#"location"][#"name"]){
userProfile[kCCUserProfileLocationNameKey] = userDictionary[#"location"][#"name"];
}
if(userDictionary[#"gender"]){
userProfile[kCCUserProfileGenderKey] = userDictionary[#"gender"];
}
if(userDictionary[#"interested_in"]){
userProfile[kCCUserProfileInterestedInKey] = userDictionary[#"interested_in"];
}
if([pictureURL absoluteString]){
userProfile[kCCUserProfilePictureURL] = [pictureURL absoluteString];
}
[[PFUser currentUser] setObject: userProfile forKey:kCCUserProfileKey];
[[PFUser currentUser] saveInBackground];
[self requestImage];
}
else{
NSLog(#"Error in FB request %#", error);
}
}];
}
-(void) uploadPFFileToParse:(UIImage *)image
{
NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
if(!imageData){
NSLog(#"imageData was not found");
return;
}
PFFile *photoFile = [PFFile fileWithData:imageData];
[photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if(succeeded){
PFObject *photo = [PFObject objectWithClassName:kCCPhotoClassKey];
[photo setObject:[PFUser currentUser] forKey:kCCPhotoUserKey];
[photo setObject:photoFile forKey:kCCPhotoPictureKey];
[photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
NSLog(#"Photo saved successfully");
}];
}
}];
}
-(void) requestImage
{
PFQuery *query = [PFQuery queryWithClassName:kCCPhotoClassKey];
[query whereKey:kCCPhotoUserKey equalTo:[PFUser currentUser]];
[query countObjectsInBackgroundWithBlock:^(int number, NSError * _Nullable error) {
if(number == 0 )
{
PFUser *user = [PFUser currentUser];
self.imageData = [[NSMutableData alloc] init];
NSURL *profilePictureUrl = [NSURL URLWithString:user [kCCUserProfileKey][kCCUserProfilePictureURL]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:profilePictureUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:4.0f];
NSURLConnection *urlConnection = [[NSURLConnection alloc]initWithRequest:urlRequest delegate:self];
if(!urlConnection){
NSLog(#"Failed to Download Picture");
}
}
}];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.imageData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *profileImage = [UIImage imageWithData:self.imageData];
[self uploadPFFileToParse:profileImage];
}
#end
Any idea how can i fix this problem?
Thanks a lot!
Try this.
- (void)getFBProfilePicture {
NSString *facebookId = #"yourFacebookId";
NSString *imageUrl = [NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1", facebookId];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithData:imageData];
});
});
}
I have created an iCarousel View and am trying to display a video preview in each "cell"/view.
I have the videos stored in Parse and am trying to
Query from the cloud
Retrieve the data from the PFFile
Convert the data to URL
Play URL using AVPlayer
Here my code so far.
-(void)getPast{
dataArray = [[NSMutableArray alloc]init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
PFQuery *query = [PFQuery queryWithClassName:#"History"];
[query whereKey:#"Location" containsString:[defaults objectForKey:#"location"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
pastArray = [NSMutableArray arrayWithArray:objects];
for (PFObject *files in objects){
PFFile *file = [files objectForKey:#"File"];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
[dataArray addObject:data];
}];
}
[self.carouselView reloadData];
}];
}
Im getting an error saying that my dataArray is empty,
I think the problem here could be that since I'm querying in the background, the For loop is finishing before I have received the data and therefore the array is empty, although I could be wrong and I don't know how to fix this even if I was right.
Code for displaying preview
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
PFObject *object = [pastArray objectAtIndex:index];
NSData *data = [dataArray objectAtIndex:index];
NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:dataString];
NSLog(#"URL %#",URL);
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 275)];
self.playerViewController = [[AVPlayerViewController alloc]init];
self.playerViewController.player = [AVPlayer playerWithURL:URL];
self.playerViewController.view.frame = view.bounds;
self.playerViewController.showsPlaybackControls = NO;
[view addSubview:self.playerViewController.view];
view.autoresizesSubviews = YES;
self.playerViewController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerViewController.player.currentItem];
[self.playerViewController.player play];
return view;
}
How can I fix my code so that each view autoplays the PFFile video corresponding to its index in the Array.
My problems:
Array is empty
Playing content for each view isn't working
Ps. Im aware that I'm not using PFObject *object.
As you guessed the for cycle finishes it's execution way before the blocks are called, you have to make sure the data is loaded before you call reloadData
The first thing that comes to my mind on how to handle this will be something like
for (PFObject *files in objects){
PFFile *file = [files objectForKey:#"File"];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
[dataArray addObject:data];
[self checkData];
}];
}
- (void)checkData {
//Check the if the data is completed
if(dataArray.count == numberOfFiles) { //Maybe a more complex if is required here but you get the idea
//All files are downloaded
dispatch_async(dispatch_get_main_queue(), ^{
//We are sure the data is ready so we reload it
[self.carouselView reloadData];
});
}
}
Also you should always check if NSData is valid before loading it
About every single tutorial and example on the internet I see shows how to fetch JSON from some url and show it in Tableview. This is not my problem I know how to do that with AFNetworking framework or with native APIs.
My problem is that after I have downloaded the JSON, I want to show some of it in my UIView labels. I have actually succeeded doing this when I was trying to find a way around NSURLSession inability to cache in iOS 8. But I didn't realize that it was synchronous.
Factory.m
+ (Factory *)responseJson
{
static Factory *shared = nil;
shared = [[Factory alloc] init];
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:#"http://urltojson.com/file.json"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:10.0];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
NSLog(#"error");
} else {
//-- JSON Parsing
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
//NSLog(#"Result = %#",result);
shared.responseJson = result;
}
return shared;
}
My question is that is it possible to use for example AFNetwoking to do the same thing? Am I missing some method that I need to call like in case of a TableView
[self.tableView reloadData];
I would like to use that framework because I need to check Reachability and it seems to implement it already.
Edit as asked to show more code
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self factoryLoad];
[self setupView];
}
- (void)factoryLoad
{
Factory *shared = [Factory responseJson];
self.titles = [shared.responseJson valueForKeyPath:#"data.title"];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
- (void)setupView
{
self.issueTitleLabel.text = [self.titles objectAtIndex:0];
}
There are a couple oddities in the code you posted.
Factory, which appears to be a singleton class, should be instantiated inside a dispatch_once to ensure thread safety.
In ViewController.m, you are calling factoryLoad on the main thread, which is subsequently calling sendSynchronousRequest on the main thread. Apple's NSURLConnection Documentation warns against calling this function on the main thread as it blocks the thread, making your application unresponsive to user input.
You should not be passing in nil as the error parameter in NSJSONSerialization JSONObjectWithData:.
In your case I would recommend separating the fetching of data from the construction of your singleton object.
Factory.m
+(Factory *)sharedFactory {
static Factory *sharedFactory = nil;
dispatch_once_t onceToken;
dispatch_once(&onceToken, {
sharedFactory = [[Factory alloc] init];
});
}
-(void)fetchDataInBackgroundWithCompletionHandler:(void(^)(NSURLResponse *response,
NSData *data,
NSError *error)
completion {
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:#"http://urltojson.com/file.json"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:10.0];
NSOperationQueue *downloadQueue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request
queue:downloadQueue
completionHandler:completion];
}
Now you should be able to create a reference to the data with a guarantee that the download request has finished and thus the data will exist.
ViewController.m
-(void)factoryLoad {
[[Factory sharedFactory] fetchDataInBackgroundWithCompletionHandler:^(void)(NSURLResponse *response, NSData *data, NSError *error){
if(!error) {
NSError *error2;
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error2];
if(error2){ /* handle error */ }
self.titles = [serializedData valueForKeyPath:#"data.title"];
[Factory sharedFactory].responseJSON = serializedData;
}
else {
// handle error
}
}];
}
This will guarantee that the download has completed before you try to access any of the downloaded information. However, I've left a few things out here, including any sort of activity indicator displaying to the user that the app is doing something important in the background. The rest is, uh, left as an exercise to the reader.
Ok I took a deeper investigation into Morgan Chen's answer and how to block.
The example code took some modification but I think It works as it should and is better code.
In Factory.m
+ (Factory *) sharedInstance
{
static Factory *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
-(void)fetchDataInBackgroundWithCompletionHandler: (void(^)(BOOL success, NSDictionary *data, NSError *error)) block
{
NSString * baseURL = #"http://jsonurl.com/file.json";
AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc] init];
__weak AFHTTPRequestOperationManager *weakManager = manager;
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(#"internet!");
[weakManager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[operationQueue setSuspended:NO];
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(#"no internet");
[weakManager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
[operationQueue setSuspended:YES];
break;
default:
break;
}
}];
[manager.reachabilityManager startMonitoring];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:baseURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) {
block(YES, responseObject, nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { // invalid request.
NSLog(#"%#", error.localizedDescription);
block(NO, nil, error);
}];
}
In ViewController.m I call this method on viewDidLoad
-(void)factoryLoad
{
[[Factory sharedInstance] fetchDataInBackgroundWithCompletionHandler:^(BOOL success, NSDictionary *data, NSError *error) {
if (success) {
NSLog(#"we have stuff");
self.responseData = data;
self.titles = [self.responseData valueForKeyPath:#"data.title"];
[self setupView];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
}];
}
Followed a recently made YouTube tutorial try connecting to web address asynchronously.
Retuning null for the response, but the data length is greater than 0. I've seen other code that does both null & length checking, which I didn't throw, just NSLog'd them.
#implementation ViewController
-(void)fetchAddress:(NSString *)address {
NSLog(#"Loading Address: %#", address);
[iOSRequest requestToPath:address onCompletion:^(NSString *result, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[self stopFetching:#"Failed to Fetch"];
NSLog(#"%#", error);
} else {
[self stopFetching:result];
NSLog(#"Good fetch: %#", result);
}
});
}];
}
- (IBAction)fetch:(id)sender {
[self startFetching];
[self fetchAddress:self.addressField.text];
NSLog(#"Address: %#", self.addressField.text);
}
-(void)startFetching {
NSLog(#"Fetching...");
[self.addressField resignFirstResponder];
[self.loading startAnimating];
self.fetchButton.enabled = NO;
}
-(void)stopFetching:(NSString *)result {
NSLog(#"Done Fetching %#", result);
self.outputLabel.text = result;
[self.loading stopAnimating];
self.fetchButton.enabled = YES;
}
#implementation iOSRequest
+(void)requestToPath:(NSString *)
path onCompletion:(RequestCompletionHandler)complete {
NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]
cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:10];
NSLog(#"path: %# %#", path, request);
[NSURLConnection sendAsynchronousRequest:request
queue:backgroundQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *result = [[NSString alloc] initWithData:
data encoding:NSUTF8StringEncoding];
if (complete) {
complete(result, error);
NSLog(#"result: %# %lu %#", result, (unsigned long)[data length], error);
}
}];
}
request is http://www.google.com>
response is null
data length is 13644
Not sure what is wrong... any suggestions?
Thanks to Josh Caswell for pointing in the right direction, but allowing me to figure it out myself.
Changed the initWithData encoding from NSUTF8StringEncoding to NSASCIIStringEncoding.
[NSURLConnection sendAsynchronousRequest:request
queue:backgroundQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *result = [[NSString alloc] initWithData:
data encoding:NSASCIIStringEncoding];
if (complete) {
complete(result, error);
NSLog(#"result: %# %lu %#", result, (unsigned long)[data length], error);
}
}];
How would I loop through the JSON returned by a TWRequest to get the geo information of a tweet? I am using the code below - I have marked up the bit I am unsure about. the text component works fine, I'm just not sure how to create the array of geo data and access this...
- (void)fetchTweets
{
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
//NSLog(#"phrase carried over is %#", delegate.a);
// Do a simple search, using the Twitter API
TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
[NSString stringWithFormat:#"http://search.twitter.com/search.json?q=%#", delegate.a]]
parameters:nil requestMethod:TWRequestMethodGET];
// Notice this is a block, it is the handler to process the response
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
// The response from Twitter is in JSON format
// Move the response into a dictionary and print
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
//NSLog(#"Twitter response: %#", dict);
NSArray *results = [dict objectForKey:#"results"];
//Loop through the results
for (NSDictionary *tweet in results) {
// Get the tweet
NSString *twittext = [tweet objectForKey:#"text"];
//added this one - need to check id NSString is ok??
NSString *twitlocation = [tweet objectForKey:#"geo"];
// Save the tweet to the twitterText array
[_twitterText addObject:twittext];
//this is the loop for the location
[twitterLocation addObject:twitlocation];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
else
NSLog(#"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];
}
"geo" is deprecated and probably not filled at all. I far as I remember it was deprecated in Twitter API v1.0 too. Try this code:
- (void)fetchTweets
{
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
//NSLog(#"phrase carried over is %#", delegate.a);
// Do a simple search, using the Twitter API
TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
[NSString stringWithFormat:#"http://search.twitter.com/search.json?q=%#", delegate.a]]
parameters:nil requestMethod:TWRequestMethodGET];
// Notice this is a block, it is the handler to process the response
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
// The response from Twitter is in JSON format
// Move the response into a dictionary and print
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
//NSLog(#"Twitter response: %#", dict);
NSArray *results = [dict objectForKey:#"results"];
//Loop through the results
for (NSDictionary *tweet in results) {
// Get the tweet
NSString *twittext = [tweet objectForKey:#"text"];
//added this one - need to check id NSString is ok??
id jsonResult = [tweet valueForKeyPath:#"coordinates.coordinates"];
if ([NSNull null] != jsonResult) {
if (2 == [jsonResult count]) {
NSDecimalNumber* longitude = [jsonResult objectAtIndex:0];
NSDecimalNumber* latitude = [jsonResult objectAtIndex:1];
if (longitude && latitude) {
// here you have your coordinates do whatever you like
[twitterLocation addObject:[NSString stringWithFormat:#"%#,%#", latitude, longitude]];
}
else {
NSLog(#"Warning: bad coordinates: %#", jsonResult);
}
}
else {
NSLog(#"Warning: bad coordinates: %#", jsonResult);
}
}
// Save the tweet to the twitterText array
[_twitterText addObject:twittext];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
else
NSLog(#"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];
}