How to check if NSString has a URL with a specific address - ios

I've been searching and trying different things, but come up short handed.
I am trying to take a URL that someone has copied i.e.: https://website/u/drksndrs and compare it with an if statement basically saying if copied string matches https://website/u/ prefix then go ahead with the program. Searching for different users based on their ID after u/. Here is the code I have right now.
NSString *latest = [UIPasteboard generalPasteboard].string;
NSString *prefix = #"https://www.website.com/u/";
NSRange textRange = [latest rangeOfString:prefix];
if (textRange.location != NSNotFound) {
NSLog(#"the prefix matches!");
[self performOperation];
} else {
NSLog(#"This doesn't match the https://www.website.com/u/ prefix.");
}

You should be checking if textRange.location == 0 to see if it is a prefix. Better yet, use the hasPrefix: method.
NSString *latest = [UIPasteboard generalPasteboard].string;
NSString *prefix = #"https://www.website.com/u/";
if ([latest hasPrefix:prefix]) {
NSLog(#"the prefix matches!");
[self performOperation];
} else {
NSLog(#"This doesn't match the https://www.website.com/u/ prefix.");
}

Related

Parsing id to NSString

When parsing API responses, sometimes I can not rely on strings being embedded in quotation marks. ID's are a good example of this, where some API's will send the numerical ID as a string while some will send it as a number.
What is a good practice when parsing such a value? If I simply parse it to an NSString like so:
NSString *myID = (NSString *)message["myID"];
I can end up with an NSString object that somehow contains (long)123.
And using stringValue would cause issues when the value is actually already sent as a string (since NSString does not have a stringValue function).
A way that works, but is somewhat ugly, is this:
id myID = (NSString *)message["myID"];
if ([myID respondsToSelector:#selector(stringValue)])
{
myID = [myID stringValue];
}
You could do something like:
id myID = message["myID"];
if ([myID isKindOfClass:[NSString class]]) { ... }
else { ... }
As long as this logic is encapsulated inside data parser and is opaque for your api users (i.e. they will always get a string) any approach is fine, e.g.:
- (NSString*)parseID:(NSDictionary*)message {
id rawID = message["myID"];
if ([rawID isKindOfClass:[NSString class]]){
return rawID;
} else if ([rawID isKindOfClass:[NSNumber class]]) {
return [(NSNumber*)rawID stringValue];
} else {
// We might still want to handle this case.
NSAssert(false, #"Unexpected id type");
return nil;
}
}
Alternative is to define stringValue in extension, so any possible objet will respond to selector:
#implementation NSString(JSONStringParsing)
- (NSString *)stringValue {
return [self copy];
}
#end
Why not just use description?
NSArray *objects = #[
#NSIntegerMin,
#NSIntegerMax,
#"123456789"
];
for (id object in objects) {
NSString *stringObject = [object description];
NSLog(#"%# -> %# | %#", [object className], [stringObject className], stringObject);
}

How to filter search within a set of letters in search bar so that each letter typed will reduce the results in objective -c

i have implemented a search bar that searching trough an array of countries(presented in a picker view), the problem is that the user need to type the full country name that it will find it and i want him to be able to type even one letter and it will show the first country that starts with that letter and if types another than it sorts even further etc etc.
Anyone have any ideas??
for(int x = 0; x < countryTable.count; x++){
NSString *countryName = [[countryTable objectAtIndex:x]objectForKey:#"name"];
if([searchedStr isEqualToString:countryName.lowercaseString]){
[self.picker selectRow:i inComponent:0 animated:YES];
flag.image = [UIImage imageNamed:[[countryTable objectAtIndex:i]objectForKey:#"flag"]];
}
}
There's a method on NSArray called filteredArrayUsingPredicate: and a method on NSString called hasPrefix:. Together they do what you need...
NSString *userInput = //... user input as lowercase string. don't call this countryName, its confusing
NSPredicate *p = [NSPredicate predicateWithBlock:^BOOL(id element, NSDictionary *bind) {
NSString countryName = [[element objectForKey:#"name"] lowercaseString];
return [countryName hasPrefix:userInput];
}];
NSArray *filteredCountries = [countryTable filteredArrayUsingPredicate:p];
If you're on iOS 8 or OS X Yosemite, you can do:
NSString *country = countryName.lowercaseString; //"england"
NSString *needle = #"engl";
if (![country containsString:needle]) {
NSLog(#"Country string does not contain part (or whole) of searched country");
} else {
NSLog(#"Found the country!");
}
Else, if on versions below iOS 8:
NSString *country = countryName.lowercaseString; //"england"
NSString *needle = #"engl";
if ([country rangeOfString:needle].location == NSNotFound) {
NSLog(#"Country string does not contain part (or whole) of searched country");
} else {
NSLog(#"Found the country!");
}
Lastly, just iterate through all possible countries and apply this to them all. There might exist more robust solutions out there (like danh's solution with some smaller modifications), but this is by far the easiest to start with.

Newbie trying to construct an URL for GET request - my test code and screenshot included

Being an iOS and Objective-C newbie I'm trying to construct an app, where the user can authenticate through Facebook, Google+ or 3 further (Russian) social networks.
For Facebook I know, that I could use the Facebook SDK or Social.framework, but for the others - I have to use OAuth and UIWebView, because there are no good SDKs for them yet.
(I have already succeeded in that once - but that was for an Adobe AIR app and now I'm trying to learn native...)
In Xcode 5.2 I have prepared a very simple Master-Detail app for iPhone and checked it into GitHub:
My question is about constructing a NSString for a GET (or body in POST) request -
Currently I have the following awkward source code in the DetailViewController.m:
- (NSString*)buildUrl
{
NSString *key = _dict[kKey];
NSString *str = _dict[kAuthUrl];
if ([key isEqual: kFB]) {
str = [str stringByAppendingString:#"display=touch"];
str = [str stringByAppendingString:#"&response_time=token"];
str = [str stringByAppendingString:#"&client_id="];
str = [str stringByAppendingString:_dict[kAppId]];
str = [str stringByAppendingString:#"&redirect_uri="];
str = [str stringByAppendingString:_dict[kAppUrl]];
//str = [str stringByAppendingString:#"&state="];
//str = [str stringByAppendingString:rand(1000)];
} else if ([key isEqual: kGG]) {
} else if ([key isEqual: kMR]) {
} else if ([key isEqual: kOK]) {
} else if ([key isEqual: kVK]) {
}
return str;
}
My questions:
Instead of using stringByAppendingString could I use something nicer? Like maybe an NSArray (or even better NSDictionary) and then somehow joining it with ampersands inbetween?
How to escape the HTML entities in the value of redirect_uri= ?
I need to append a random number as the value of state=, but I am not sure what function to use there best...
Here is what my app prints at the moment, for the above code:
MyAuth[9626:70b] request: { URL: https://graph.facebook.com/oauth/authorize?display=touch&response_time=token&client_id=432298283565593&redirect_uri=https://www.facebook.com/connect/login_success.html }
(which is no good: the URL at the end is not escaped and there is no random state number).
stringWithFormat: is your friend.
- (NSString*)buildUrl
{
NSString *key = _dict[kKey];
NSString *str = _dict[kAuthUrl];
if ([key isEqual: kFB]) {
NSString *escapedURI = [_dict[kAppUrl] stringByAddingPercentEscapesUsingEncoding:NSUTF8Encoding];
int state = arc4random_uniform(1000);
str = [NSString stringWithFormat:#"%#display=touch&response_time=token&client_id=%#&redirect_uri=%#&state=%d", _dict[kAuthUrl], _dict[kAppId], escapedURI, state];
} else if ([key isEqual: kGG]) {
} else if ([key isEqual: kMR]) {
} else if ([key isEqual: kOK]) {
} else if ([key isEqual: kVK]) {
}
return str;
}

Split NSString and Limit the response

I have a string Hello-World-Test, I want to split this string by the first dash only.
String 1:Hello
String 2:World-Test
What is the best way to do this? What I am doing right now is use componentsSeparatedByString, get the first object in the array and set it as String 1 then perform substring using the length of String 1 as the start index.
Thanks!
I added a category on NSString to split on the first occurrence of a given string. It may not be ideal to return the results in an array, but otherwise it seems fine. It just uses the NSString method rangeOfString:, which takes an NSString(B) and returns an NSRange showing where that string(B) is located.
#interface NSString (Split)
- (NSArray *)stringsBySplittingOnString:(NSString *)splitString;
#end
#implementation NSString (Split)
- (NSArray *)stringsBySplittingOnString:(NSString *)splitString
{
NSRange range = [self rangeOfString:splitString];
if (range.location == NSNotFound) {
return nil;
} else {
NSLog(#"%li",range.location);
NSLog(#"%li",range.length);
NSString *string1 = [self substringToIndex:range.location];
NSString *string2 = [self substringFromIndex:range.location+range.length];
NSLog(#"String1 = %#",string1);
NSLog(#"String2 = %#",string2);
return #[string1, string2];
}
}
#end
Use rangeOfString to find if split string exits and then use substringWithRange to create new string on bases of NSRange.
For Example :
NSString *strMain = #"Hello-World-Test";
NSRange match = [strMain rangeOfString:#"-"];
if(match.location != NSNotFound)
{
NSString *str1 = [strMain substringWithRange: NSMakeRange (0, match.location)];
NSLog(#"%#",str1);
NSString *str2 = [strMain substringWithRange: NSMakeRange (match.location+match.length,(strMain.length-match.location)-match.length)];
NSLog(#"%#",str2);
}

Decoding the scanned barcode value to int value

When I scan the barcode and I get some value if it is Equal=2 then I need to display with == and if it is Equal=3 then I need to display with = and if the value is 4 then invalid.
But Scanned Barcode are of integer value -- when decode using NSASCII it is displaying only till value 127 after that it is showing invalid results. Eg: if my Barcode value = 9699 the result value=jem then my added result value=jem= actualstring value=%åasc value id only showing 37
Here is my code:
- (void) readerView:(ZBarReaderView *)view didReadSymbols:(ZBarSymbolSet *)syms fromImage:(UIImage *)img
{
// do something useful with results -- cool thing is that you get access to the image too
for (ZBarSymbol *symbol in syms) {
[resultsBox setText:symbol.data];
if ([resultsBox.text length] == 2) {
addedresult.text = [resultsBox.text stringByAppendingString:#"=="];
} else if ([resultsBox.text length] == 3) {
addedresult.text = [resultsBox.text stringByAppendingString:#"="];
} if ([resultsBox.text length] >= 4) {
addedresult.text = #"Invalid";
}
[Base64 initialize];
NSString *myString = [[NSString alloc]initWithString:addedresult.text];
NSData * data = [Base64 decode:myString];
NSString * actualString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"%#",actualString);
labeltext.text= actualString;
int asc = [actualString characterAtIndex:0];
label.text = [NSString stringWithFormat:#"%d", asc];
[actualString release];
break;
}
}
Since someone revived this question's comments, i'll revive this entire post.
You shouldn't go through NSData to create an NSString from something you already have, and you're probably losing something along the way. Go directly to NSString using stringWithFormat. Also, ASCII will come back and byte you later, if you have a choice, use UTF8.
NSString *actualStringUTF8 = [NSString stringWithFormat:#"%#",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSString *actualStringASCII = [NSString stringWithFormat:#"%#",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",actualStringUTF8);
NSLog(#"%c",[actualStringUTF8 UTF8String]); //This is a const char*
Secondly, I looked into the SDK and it says symbol.data is already an NSString*. Depending on what you want, you may not need to do anything. If you do end up needing to change encoding, make sure you understand why you need to (one good reason is "the rest of the application uses NS****StringEncoding").
Also make sure you compare strings the correct "Objective-C" way:
[actualString isEqualToString: testString];
NOT actualString == testString;

Resources