photo credit: Kat Northern Lights Man via photopin cc
Good evening, this is Bono.
I was building a phone app in Xcode and needed to convert a phone number from 090XXXXXYYYYYY to 090-XXXXX-YYYYYY.
I googled and didn't find a very good page, so I'll leave it here.
コンテンツ
What we wanted to do
I would like to quote a phone number from the address book on the application and make a call.
There is one point, however. The data in the address book is in the format 090XXXXYYYYY.
On the other hand, to make a phone call in Xcode, you must include a hyphen, such as 090-XXXX-YYYYYY, or the number will not be recognized as a phone number.
Therefore, a conversion is needed to put hyphens between phone numbers.
Code to convert from 090XXXXXXYYYYY to 090-XXXX-YYYYY
assumption
- Call the phoneCall method at a certain timing (e.g., when a button is pressed)
- The delegate contains the phone number NUMBER.
- Separate by if since the number of digits is different for cell phones and regular phones.
- (void)phoneCall
{
AppDelegate delegate = (AppDelegate )[ [UIApplication sharedApplication] delegate];
NSString phone = delegate.number;
NSString phrase = @"-";
NSString separatedPhone;
if(phone.length == 11){
//for cell phone
NSString phone1 = [phone substringToIndex:3];
NSString phone2 = [phone substringWithRange:NSMakeRange(3, 4)];
NSString phone3 = [phone substringFromIndex:7];
separatedPhone = [[[ [phone1 stringByAppendingString:phrase]stringByAppendingString:phone2]stringByAppendingString:phrase]stringByAppendingString:phone3];
}else{
//other phone
NSString phone1 = [phone substringToIndex:3];
NSString phone2 = [phone substringWithRange:NSMakeRange(3, 3)];
NSString phone3 = [phone substringFromIndex:6];
separatedPhone = [[ [phone1 stringByAppendingString:phrase]stringByAppendingString:phone2]stringByAppendingString:phrase]stringByAppendingString:phone3];
}
NSURL url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",separatedPhone]];
[ [UIApplication sharedApplication] openURL:url];
}
[/crayon]
explanation
In this case, NSString methods are used extensively for string manipulation.
substringToIndex:N
This method cuts off the string from the beginning to the N+1st character.
This time, the third character from the beginning of the variable called PHONE, or 090, is extracted.
substringWithRange:NSMakeRange(N,M)
This method cuts off M characters from the N+1th of a variable.
In this case, four characters are cut from the fourth, i.e., the XXXX portion is obtained.
substringFromIndex:N
This is similar to substringToIndex, but slightly different.
This is a method to get the N+1th to the last character.
In this case, the YYYYY part is acquired.
after that
Simply concatenate strings extracted from the above methods with a hyphen in between.
Note that the codes listed here are not only for cell phones, but also for land lines with one less digit.
For your information.
Reference Site
This is the NSString page of the usual site, iPhone App Development Tora no Maki.
There are still many ways to use NSString.