How to convert NSData with textEncoding utf-8 into NSURL - ios

I want to convert NSData (textEncoding utf-8) into NSURL.
I am writing below code for this, but conversion from NSData to NSString returning nil. (May be due to encoding type)
NSString *stringFromData = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // stringFromData is nil after execution of this line.
NSURL *url = [[NSURL alloc] initWithString:stringFromData];
So what should I do to convert NSData into NSURL in my case.
But when I am trying to load this data into webview, Its working good. Here is my code to load this data into webview.
[self.webView loadData:myData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil]; //PDF is showing in my webview.
but I am not able to convert this NSData into NSURL. What encoding should I use to convert NSData with textEncoding utf-8 into NSURL ?

You're trying to load data that represents a PDF into an NSString. A PDF file does not consist of UTF-8 encoded characters that represent text, it's a file that contains header information, fonts, vector graphics AND text.
The only solution to your problem in my mind is change the source of the NSData to something that will provide UTF-8 encoded characters that make up a URL.
If you can't get data any other way, why not check if you can extract the textual data from the PDF? https://github.com/zachron/pdfiphone

Have you tried writing the data to local file,
NSString *docPath = [NSHomeDirectory() stringByAppendingString:#"/Documents"];
NSString *pdfFilePath = [docPath stringByAppendingPathComponent:#"pdfFile.pdf"];
BOOL success = [myData writeToFile:pdfFilePath atomically:YES];
if (success) {
NSURL *url = [[NSURL alloc] initFileURLWithPath:pdfFilePath];
NSLog(#"url from data : %#",url);
}
Hope this helps.
Thanks

Ok, try this:
//Convert data to string
NSString *urlString = [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
//Convert the string to URL
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Related

play .h264 video format - iOS

I have video data,
I need to convert the data into '.h264' format and save into a Temporary file.
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
NSURL* url = [NSURL fileURLWithPath:path];
I have created the temporary file path but, how can I attach the data along with the file and save.
Editted:
Sorry.
I was my mistake. self answering.
NSData *data = [[NSData alloc]initWithBase64EncodedString:val options:0];
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* htmlFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[data writeToFile:htmlFilePath atomically:YES];

How to get url from video reference url

I have a url of video from photo library :-
/var/mobile/Applications/9BC2EBC4-7A71-4C8B-8BFB-D25D01E4CA83/Documents/IMG_5244.MOV
How can i get convert it to NSData I am tried following but nothing worked:
NSString* fileName = movieAsset.defaultRepresentation.filename;
NSURL* fileUrl = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject]URLByAppendingPathComponent:fileName];
NSString *str = [fileUrl path];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
[arrVideoUrls addObject:fileUrl];
Here in your code you have add NSString as an URL and try to convert that NSString URL to NSData, so here first convert that string url to NSURL and then convert it in to NSData like below...
NSString *strVideoURL = #"assets-library://asset/asset.MOV?id=2EBD925F-D275-403E-A6A4-3487134D9B9D&ext=MOV";
NSURL *urlVideo = [NSURL URLWithString:strVideoURL];
NSData *videoData = [NSData dataWithContentsOfURL:urlvideo]];

iOS NSURL String

I am trying to place strings inside my NSURL, I have tried most suggestions but all seem to fail, i'm trying to find a easy way to do it instead of having to use some long request with long code. I'm wondering is there anything small here i'm forgetting?
NSString *lat = #"53.350628";
NSString *lng = #"-6.254997";
NSURL *blogURL = [NSURL URLWithString:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
Thanks,
Curtis
+[NSURL URLWithStirng:] does not take a format string and a variable argument list. It just takes a string, so you should use something like +[NSString stringWithFormat:] first before passing it to URLWithString, like this:
NSURL *blogURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng]];
Format the string, than add that to the NSURL
NSString *lat = #"53.350628";
NSString *lng = #"-6.254997";
//Format String
NSString *urlString = [NSString stringWithFormat:#"http://url.com/jsontest.php?lat=%#&lng=%#&max=5", lat, lng];
NSURL *blogURL = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

Unable to create NSURL with special characters (éè) with Objective C

I am unable to load an image because of nil returned by NSURL. The url looks like this : http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg. When I enter my URL on firefox, I get my image but when I try in firefox the encoder http://www.example.fr/wp-content/uploads/2014/06/pr%3Flinaire.jpg I have no image...
Here is the code I use to load my image :
NSString *myString = #"http://www.example.fr/wp-content/uploads/2014/06/préliminaire.jpg";
NSURL *url = [NSURL URLWithString:myString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
That returns nil for url, so everything after is nil..
Thank you for your help guys
That's because those characters are not valid in a URL. Escape them first:
NSString *escaped = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escaped];
// etc.

convert AVAudioRecorder to NSString or bytes

I am trying to send AVAudioRecorder in Json
,
i would like to convert it to NSString or Byte..
any idea?
Never tried but maybe this ?
// Get the recording
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:filePath settings:recordSetting error:&error];
....
// Convert to NSData
NSData *theData = [[NSData alloc] initWithContentsOfFile:filePath];
// Convert to NSString
NSString* dataAsString = [NSString stringWithUTF8String:[theData bytes]];
Once you have completed a recording with AVAudioRecorder, you can retrieve the recorded file using your AVAudioRecorder's url property. Then you can initialize an NSData with the url:
NSData *data = [NSData dataWithContentsOfURL:recorder.url];
To send it as JSON, you need to base64 the data. I recommend this project which has a category for doing this: https://github.com/l4u/NSData-Base64
You'll need to #import "NSData+Base64.h" into your source and then you can get the base64 string from your NSData by calling base64EncodedString on it.

Resources