Shorten url using bit.ly - iphone

I have tried to Shorten urls using bit.ly. When i try to pass a static link it gives me a shorten url but when i try to pass a variable link it doesn't.
here is my code....
Bitlyzer *bitlyzer = [[Bitlyzer alloc] initWithDelegate:self];
[bitlyzer shortURL:string];
[bitlyzer shortURL:#"http://www.google.com"];
When i pass this url it gives me a Shorten url but when i pass a variable string as shown above it doesn't give me shorten url.
Please give me your suggetions...

Some time in our string some space is remain and so bitly not convert it and return null value so first remove the null or space from string and then try to convert it..
Add my these two methods in your .m file and then use with your variable.. see the example also how to use it...
-(NSString*) trimString:(NSString *)theString {
NSString *theStringTrimmed = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theStringTrimmed;
}
-(NSString *) removeNull:(NSString *) string {
NSRange range = [string rangeOfString:#"null"];
//NSLog(#"in removeNull : %d >>>> %#",range.length, string);
if (range.length > 0 || string == nil) {
string = #"";
}
string = [self trimString:string];
return string;
}
And use this like bellow...
string = [self removeNull:string];
[string retain];
Bitlyzer *bitlyzer = [[Bitlyzer alloc] initWithDelegate:self];
[bitlyzer shortURL:string];

Related

iOS rangeOfString can't locate the string that is definitely there

I am writing code in objective-c. I would like to extract a url from a string.
Here is my code:
NSMutableString *oneContent = [[latestPosts objectAtIndex:i] objectForKey:#"content"];
NSLog(#"%#", oneContent);//no problem
NSString *string = #"http";
if ([oneContent rangeOfString:string].location == NSNotFound) {
NSLog(#"string does not contain substring");
} else {
NSLog(#"string contains substring!");
}
As you can see, I want to extract a url from the oneContent string, and I have checked that oneContent definitely contains "http", but why does the result show nothing?
Is there some better way to extract the url?
Check oneContent or the actual code you are running.
This works:
NSMutableString *oneContent = [#"asdhttpqwe" mutableCopy];
NSLog(#"%#", oneContent);//no problem
NSString *string = #"http";
if ([oneContent rangeOfString:string].location == NSNotFound) {
NSLog(#"string does not contain substring");
} else {
NSLog(#"string contains substring!");
}
NSLog output:
Untitled[5911:707] asdhttpqwe
Untitled[5911:707] string contains substring!
It is probably best not to use a Mutable string unless there is some substantial reason to do so.
I would suggest using NSScanner.

Extract string upto certain character

I need to extract string upto certain word
I have time like this :"2012-12-29T00:00:00" how can I extract the part upto TO.That is I dont need time.This string is not static .I mean it changes like "2013-01-21T00:00:00"
use like below
NSString *stingTime = #"2012-12-29T00:00:00";
if([stingTime rangeOfString:#"T"].location != NSNotFound)
stingTime = [stingTime substringToIndex:[stingTime rangeOfString:#"T"].location];
//output
2012-12-29
This should like this...
NSString *string = #"2012-12-29T00:00:00";
NSRange range = [string rangeOfString:#"T0"];
if (range.location != NSNotFound){
NSString *newString = [string substringToIndex:range.location];
NSLog(#"NewString = %#",newString);
}
You can use functions of NSString For e.g.:
NSString *string = #"2012-12-29T00:00:00";
NSString *newString = [string substringToIndex:10];
Your newString will contain 2012-12-29.
Hope this helps.

How to replace two different strings in iPhone programming

I am reading CSV file in my application.
I want to replace two different strings and print as one line. for example:
string1#$string2#$string3####string4
I want to replace #$ with , and #### with \n
and want to show the result on a UILabel.
You can use stringByReplacingOccurrencesOfString:withString: method, like this:
NSString *orig = "string1#$string2#$string3####string4";
NSString *res = [orig stringByReplacingOccurrencesOfString:#"#$" withString:#" "];
res = [res stringByReplacingOccurrencesOfString:#"####" withString:#"\n"];
Note that the original string does not get changed: instead, a new instance is produced with the replacements that you requested.
use
String = [String stringByReplacingOccurrencesOfString:#"#$" withString:#","];
String = [String stringByReplacingOccurrencesOfString:#"####" withString:#"\n"];
And then
yourLabel.text=String;
Try this
NSString *string = #"####abc#$de";
string = [string stringByReplacingOccurrencesOfString:#"####" withString:#"\n"];
string = [string stringByReplacingOccurrencesOfString:#"#$" withString:#","];
Hope it helps you

Get filename from Content-Disposition header

I'm currently checking for this header and if it's available, I'll try to get the filename from it. Question is, what is the best method to retrieve it? I understand that Content-Disposition header may appear with different parameters. Examples below:
Content-Disposition = "inline; filename=sample-file-123.pdf"
Content-Disposition = "attachment; filename="123.zip""
I'm only interested to get the filename.
There is a dedicated API for this: URLResponse.suggestedFilename
So if you are getting your header from a URLResponse you just call
let filename: String = response.suggestedFilename ?? "default"
and you're done. Note that despite what the documentation says, the return value is optional so you have to provide a default or force unwrap if you dare (I wouldn't).
From the documentation:
The method first checks if the server has specified a filename using the
content disposition header. If no valid filename is specified using that mechanism,
this method checks the last path component of the URL. If no valid filename can be
obtained using the last path component, this method uses the URL's host as the filename.
If the URL's host can't be converted to a valid filename, the filename "unknown" is used.
In mose cases, this method appends the proper file extension based on the MIME type.
This method always returns a valid filename.
I would do something along the lines of this:
- (NSString *)getFilenameFrom:(NSString *)string {
NSRange startRange = [string rangeOfString:#"filename="];
if (startRange.location != NSNotFound && startRange.length != NSNotFound) {
int filenameStart = startRange.location + startRange.length;
NSRange endRange = [string rangeOfString:#" " options:NSLiteralSearch range:NSMakeRange(filenameStart, [string length] - filenameStart)];
int filenameLength = 0;
if (endRange.location != NSNotFound && endRange.length != NSNotFound) {
filenameLength = endRange.location - filenameStart;
} else {
filenameLength = [string length] - filenameStart;
}
return [string substringWithRange:NSMakeRange(filenameStart, filenameLength)];
}
return nil; //or return #"", whatever you like
}
You will have to check it as i made this in the browser (dont have access to xcode atm).
+ (NSString *)filenameFromContentDispositionHeader:(NSString *)contentDispositionHeader {
NSString *pattern = #"filename=\"(.*)\"";
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSTextCheckingResult *result =
[regex firstMatchInString:contentDispositionHeader
options:0
range:NSMakeRange(0, contentDispositionHeader.length)];
NSRange resultRange = [result rangeAtIndex:0];
if (resultRange.location == NSNotFound) {
return nil;
} else {
return [contentDispositionHeader substringWithRange:
NSMakeRange(resultRange.location + 10, resultRange.length - 11)];
}
}
Note that you'll need to modify the pattern if you can't be sure the filename is surrounded in double-quotes.

how to remove () charracter

when i convert my array by following method , it adds () charracter.
i want to remove the () how can i do it..
NSMutableArray *rowsToBeDeleted = [[NSMutableArray alloc] init];
NSString *postString =
[NSString stringWithFormat:#"%#",
rowsToBeDeleted];
int index = 0;
for (NSNumber *rowSelected in selectedArray)
{
if ([rowSelected boolValue])
{
profileName = [appDelegate.archivedItemsList objectAtIndex:index];
NSString *res = [NSString stringWithFormat:#"%d",profileName.userID];
[rowsToBeDeleted addObject:res];
}
index++;
}
UPDATE - 1
when i print my array it shows like this
(
70,
71,
72
)
Here's a brief example of deleting the given characters from a string.
NSString *someString = #"(whatever)";
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"()"];
NSMutableString *mutableCopy = [NSMutableString stringWithString:someString];
NSRange range;
for (range = [mutableCopy rangeOfCharacterFromSet:charSet];
range.location != NSNotFound;
[mutableCopy deleteCharactersInRange:range],
range = [mutableCopy rangeOfCharacterFromSet:charSet]);
All this does is get a mutable copy of the string, set up a character set with any and all characters to be stripped from the string, and find and remove each instance of those characters from the mutable copy. This might not be the cleanest way to do it (I don't know what the cleanest is) - obviously, you have the option of doing it Ziminji's way as well. Also, I abused a for loop for the hell of it. Anyway, that deletes some characters from a string and is pretty simple.
Try using NSArray’s componentsJoinedByString method to convert your array to a string:
[rowsToBeDeleted componentsJoinedByString:#", "];
The reason you are getting the parenthesis is because you are calling the toString method on the NSArray class. Therefore, it sounds like you just want to substring the resulting string. To do this, you can use a function like the following:
+ (NSString *) extractString: (NSString *)string prefix: (NSString *)prefix suffix: (NSString *)suffix {
int strLength = [string length];
int begIndex = [prefix length];
int endIndex = strLength - (begIndex + [suffix length]);
if (endIndex > 0) {
string = [string substringWithRange: NSMakeRange(begIndex, endIndex)];
}
return string;
}