I have my app setup to all Open-in with various document types. For example, user opens a PDF into my app and then rather than displaying it I have them select what folder they want to put it into and upload it to my server. I am taking the url, converting it to NSData and uploading that to my server (works great).
Then, when they want to view the file at a later time I am pulling the NSData from my server...this is where I am getting stuck. How do I take that NSData object and display the contents to the user from within my app? Thank you.
If the server doesn't require any user/password challenge you can just do
NSURL*url=[NSURL URLWithString:#"your url goes here"];
NSData*data=[NSData dataWithContentsOfURL:url];
Your can also have more control of your download using NSURLRequest and implementing the NSURLConnection class with it delegate methods
If you implement the NSURLRequest with a NSURLConnection you can challenge the user/password
For large content you can do the class method
+ sendAsynchronousRequest:queue:completionHandler:
For creating the pdf document from a NSData you can do:
CFDataRef myPDFData = (CFDataRef)data;
CGDataProviderRef provider= CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf= CGPDFDocumentCreateWithProvider(provider);
Where data is your received data
You either have to include view support for each type of document you are planning to support or hand over that task to another app which can do it for you using UIDocumentInteractionController.
See Document Interaction Programming Topics for iOS for more info.
Use a UIWebView to display NSData containing PDF
[self.webView loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
Web Views can display different Data.
Related
I am developing an iPhone application that is using an API to find recipes from http://www.recipepuppy.com/about/api/.
There are three parameters that must be put into return results. There are i,q, and p, Where i are the ingredients, q in a normal search query and p is the page #. I am able to specifically add these parameters and then load the results into a table view in Xcode.
I also want to implement a search that allows the users to search for recipes based on whatever they feel like and return the results. Could someone point me in the correct direction on how to do this. I know I will have to take what ever the user inputs and place it into a string but how do I implement that string into the parameters of the URL?
To answer your question:
I know I will have to take what ever the user inputs and place it into a string but how do I implement that string into the parameters of the URL?
You can use the stringWithFormat method of NSString. For example:
NSString *ingredients = #"ingredients";
NSString *query = #"soups";
NSString *page = #"1";
NSString *url = [NSString stringWithFormat:#"http://www.recipepuppy.com/api/?i=%#&q=%#&p=%#",ingredients,query,page];
Before using this URL, it's recommended that you URL encode it.
NSString *encodedURL = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Now that you have your URL, just start a connection to the web (NSURLConnection, AFNetworking, the choice is yours), parse the data returned, and load that into an array to display in the table view.
To build a solid app that involves JSON communication over the network, you can use the JSONModel library. It provides you with data models, and takes care of sending and receiving JSON forth and back to your API service.
You can have a look at the GitHub page:
https://github.com/icanzilb/JSONModel/
Or also to this example on how to use the YouTube JSON API with JSONModel:
http://www.touch-code-magazine.com/how-to-make-a-youtube-app-using-mgbox-and-jsonmodel/
On the GitHub page there are more code samples. Good luck :)
(Disclosure: I'm JSONModel's author)
I'd use AFNetworking for all your network requests:
https://github.com/AFNetworking/AFNetworking
To make the request just construct an NSString and send it off
[NSString stringWithFormat:#"http://www.recipepuppy.com/api/?i=%#&q=%#&p=%d", self.ingredients, self.query, self.page]
As per requirement I need to post user profile details like user name, first name, last name and profile picture from IPhone device. How can we send these details including “profile picture” together? Do I need to send these details as a JSON object? So I need to convert the image as byte array. Right?
If you can provide any IPhone code snippet then it would be better.
Also if possible please provide the code for WCF Rest service Part.
Thanks in advance.
To send data to the server in JSON format:
ADD:
JSON.h (Header)
JSON.m (Implementation)
Use
https://github.com/stig/json-framework/download
To convert your profile details, you can refer following sample
NSMutableDictionary *jsonEncode = [NSMutableDictionary dictionary];
[jsonEncode setValue:#"" forKey:#""];
NSString *jsonString = [jsonEncode JSONString];
To send Image to server you need to convert image into Base64 format.
Refer link below:
UIImage to base64 String Encoding
I have a core data application using rest kit to retrieve data from a web service. The response i get from the webservice is a json string like this -
"nodeRef": "workspace:\/\/SpacesStore\/b1d51831-990d-47a8-a018-f1c1bg33f594",
"type": "document",
"name": "x.jpg",
"displayName": "x.jpg",
"title": "myTestProject",
(This is some meta data around an image. If i want to retrieve this image then i should access it using this url - http://x.co.uk/share/proxy/alfresco/api/node/workspace/SpacesStore/b1d51831-990d-47a8-a018-f1c1bg33f594/content/thumbnails/ipad1024?c=force) - note ive changed the URL slightly so you wont be able to access it. Also you need to authentication to access the URL. So i was wondering how i would go about getting this image and storing it locally on my SQLite DB? I can obviously access the URL but how exactly do i prompt it to download the image
What i did in this situation was to do a lazy load on the image with something like:
image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.absoluteUri]]];
NSData *imageData=UIImageJPEGRepresentation(image, JPGCompression);
Then store it. Outside of Restkit. This was a while ago, but it worked for me.
Basically extract the URL from restkit, do a lazy load when the image is presented on the screen, and thne store it back to core data (or wherever) when you have loaded it once.
Hope this helps.
I need to access some plist files from a remote server, which are in a password protected directory. In case of not using a password to access the directory, i use the simple code and it works:
NSString *query = #"http://www.sante.com/FFFF/privat/updateDates.plist";
NSURL *urlDates = [NSURL URLWithString:[query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *datesUrl = [NSDictionary dictionaryWithContentsOfURL:urlDates];
But in case of password protected directory, does it exist a way to treat it in xcode (without using NSUrlCOnnection, xmlparser because i don't want to parse the file while i can access directly by using dictionaryWithContentsOfURL)?
Edit: The point is that i just want to write some code to access/download the plist files (which are in a password protected directory of server) from the webserver to ios device. But i don't know how to. In case of not using password for accessing server directory, my above code works very well, but i need to access them in secured mode, and i just need to validate the webserver directory password programatically (hard coded).
If you need to deal with customising the download process by supporting authentication, then you need to stop using dictionaryWithContentsOfURL: and start using NSConnection because NSConnection is the API for customising URL downloads. Particularly, look at the delegate method connection:didReceiveAuthenticationChallenge: which is where you hook in to the authentication procedure.
You don't need to use an XML parser, because you have no need to parse the downloaded contents. Hand the data off to NSPropertyListSerialization once you've downloaded them and that can return an NSDictionary instance to you.
I am a beginner in i phone development.I am,right now, using web service to fetch data on my page.now the thing is that i have 2 functions in the same webpage. now i want to fetch data from only 1 function at the time when my url is passed. so how can i pass the argument? note that,my main purpose is to convert data into json format.my URL that i run in web browser is like :
http://localhost/abc/webservices/mainPage.asmx?op=GetEmpDetail
how can i change its format,so that i can use it in objective c?my function name is getEmpDetail.
Thank you in advance.
Not Sure but try this out.
nsurl *url =http://localhost/abc/webservices/whatever/GetEmpDetail?Param1=value1&Param2=value2
url = [url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];