Create request from URL with queryparams like NSDictionary - objective-c

http://mobile.gunsel.ua/index.php?r=Rupages&jsonRequest={"id_page":"2"}
So,how can I create a NSURL(and after request) from this URL.(I use NSJSONSerilization).
I'm new to iOS development please help me. Thank You!
Don't offer links on apple documentation.

Not sure I get what you are asking, but you can use a string, then format it with your JSON (I.e concatenate the params), then create an NSUrl from that string.
stringWithFormat
Should do the trick.
I'm not sure if that answered your question, but you need to elaborate if that's not the case.

i'm found out!! This is was associated with Encoding)))
NSString sampleUrl = #"mobile.gunsel.ua/index.php?r=Rupages&jsonRequest={\"id_page\":\"2\"}";
NSString encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];

Related

stringByAddingPercentEscapesUsingEncoding was deprecated in 9.0 .How to do this?

I'm a junior developer, and I got a code for this:
(NSString *)encodedStringFromObject:(id)object {
return [[object description] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
But in 9.0 have to use stringByAddingPercentEncodingWithAllowedCharacters.
How Could I transfer this code ?
I need help, Thanks!
If you want just fast example look at this code:
NSString * encodedString = [#"string to encode" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
Also check List of predefined characters sets
If you want explanation read the documents or at least this topic: How to encode a URL in Swift
URL = [[NSString stringWithFormat:#"%#XYZ",API_PATH]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
You'd read the spec for both functions. Then you would find out for what purpose characters are replaced with escape sequences. From there you would find out the set of allowed characters, which must be documented somewhere.
That's an essential part of changing from junior to normal to senior developer: Find out exactly what your code is supposed to do, which should be defined somewhere, and then make it do what it should do.
stringByAddingPercentEscapesUsingEncoding is probably deprecated because it doesn't know which characters are allowed and sometimes gives the wrong results.
You can use stringByRemovingPercentEncoding instead of stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding
[#"string" stringByRemovingPercentEncoding];
Here is solution to do in Swift
var encodedString = "Hello, playground".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)

How to decode URL in objective c iphone?

I was wondering if there is a method to decode a string that is in URL
encoded format?
this is my URL i want to decode it but i dont understand how to do this i tried multiple
ways
http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7
but didn't get the right response
Please anyone can help me out of this,
how can i decode this url i dont need % sign.
Thanks
By using the NSString method -(NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Ex:
NSString *unencodedUrlString = [#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Here's the not deprecated method : string​By​Removing​Percent​Encoding.
NSString *unencodedUrlString = [yourString string​By​Removing​Percent​Encoding];

initWithContentsOfURL what is the encoding?

Hi I saved a URL that has a path to a directory where I will be storing information, Now I want to retrieve the information but I don't know how to receive the items, I am trying to use initWithContentsOfURL but I dont know what the encoding would be?
This is how I saved the URL
//DirectoryPaths has the NSCAcheDirectory
dirPath = [[directoryPaths objectAtIndex:0] URLByAppendingPathComponent:[NSString stringWithFormat:#"photos.jpg"]];
How do I get a path to the URL now? I tried
NSString *pathToFile = [[NSString alloc] initWithContentsOfURL:dirPath
usedEncoding:nil
error:&error];
I have no clue what the encoding would be since I didnt use one to store the file?
I think you've misunderstood; initWithContentsOfURL:usedEncoding:error: loads a string from a URL. So you'd specify the encoding used for the string — UTF-8, ASCII or something like that.
To get a disk path from a URL you probably want [dirPath path]. In practice you can probably just load from the URL rather than hardcoding local disk behaviour.
Use NSUTF8StringEncoding or ASCII. It works for me. :)

Retrieve news feed using facebook API

Hi I have been looking for hours but cannot find the API call that retrieves a user's news feed (i.e the thing that you see at http://www.facebook.com/home.php). I think this should be something extremely easy. What did I miss?
I would prefer an answer with FQL, but other API methods will be just fine, as I think I can translate between them.
https://graph.facebook.com/me/home
Note: /me/home retrieves an outdated view of the News Feed. This is currently a known issue and we (Facebook) don't have any near term plans to bring them back up into parity.
I found it! The way is to look in the stream for post from users that are your friends. This is kind of annoying because it takes double the effort just to do some very basic task. In Objective-C using FBConnect the code look like this:
NSString *connections = [NSString stringWithFormat:#"select target_id from connection where source_id == %lld", _uid];
NSString *fql = [NSString stringWithFormat:#"select message from stream where source_id in (%#)", connections];
NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:#"query"];
[[FBRequest requestWithDelegate:self] call:#"facebook.fql.query" params:params];
http://developers.facebook.com/docs/reference/fql/stream
the first example is how to properly get the news feed.

Objective-C: Extract filename from path string

When I have NSString with /Users/user/Projects/thefile.ext I want to extract thefile with Objective-C methods.
What is the easiest way to do that?
Taken from the NSString reference, you can use :
NSString *theFileName = [[string lastPathComponent] stringByDeletingPathExtension];
The lastPathComponent call will return thefile.ext, and the stringByDeletingPathExtension will remove the extension suffix from the end.
If you're displaying a user-readable file name, you do not want to use lastPathComponent. Instead, pass the full path to NSFileManager's displayNameAtPath: method. This basically does does the same thing, only it correctly localizes the file name and removes the extension based on the user's preferences.
At the risk of being years late and off topic - and notwithstanding #Marc's excellent insight, in Swift it looks like:
let basename = NSURL(string: "path/to/file.ext")?.URLByDeletingPathExtension?.lastPathComponent