Add hyperlinks to NSString iOS - objective-c

I have a simple NSString, for example :
NSString *text = #"Stackoverflow is amazing!"
and I'd like to turn the word Stackoverflow into a hyperlink pointing to https://stackoverflow.com/ and still be able to output the string as a variable. Is this possible?

A string only contains letters - no formatting what so ever.
To make a part into a link, you have to Attributed Text and assign the first word a NSLink attribute with the url:
NSURL *url = [NSURL URLWithString:#"http://www.google.de"];
NSAttributedString *my = [NSAttributedString attributedStringWithString:#"my"
attributes:#{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont systemFontWithSize:16]}];
NSAttributedString *link = [NSAttributedString attributedStringWithString:#"Link"
attributes:#{NSForegroundColorAttributeName:[UIColor blue], NSFontAttributeName:[UIFont systemFontWithSize:16], NSLinkAttributeName:url}];
NSMutableAttributedString *attr = [my mutableCopy];
[attr appendAttributedString:link];
show it in a textview, a UILabel doesn't support clicking AFAIK

Related

xCode using variables

I'm pretty new to Objective C in general...
I'm wondering how I can use a *variable in the view controller, and add it onto the web view URL. Bellow is a UIWebViewer, It loads "site.com/something.php"... Along with that I would like for it to addon to the URL "?uuid=(UUID Variable here)".
Sorry... I'm more used to PHP/Perl coding where you can just throw in "$uuid"...
Thanks,
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *UUID = [[NSUUID UUID] UUIDString];
NSURL *myURL = [NSURL URLWithString:#"http://www.site.com/something.php?uuid="];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
}
It's very simple you just need to create a property to assign whatever value you want to it
ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *uuid = [[NSUUID UUID] UUIDString]; // Convention says that UUID should uuid
// All we need to do now is add the uuid variable to the end of the string and we can do
// that by using stringWithFormat:
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.site.com/something.php?uuid=%#", uuid]];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
}
Check the documentation of NSString and the class method stringWithFormat:
Here's what you're looking for:
NSString *UUID = [[NSUUID UUID] UUIDString];
NSString *urlstr = [NSString stringWithFormat:
#"http://www.site.com/something.php?=%#", UUID];
NSURL *myURL = [NSURL URLWithString:urlstr];
NSString's stringWithFormat: method allows you to build strings from literal strings and varaibles. Variables are added to the literal string using format specifiers. Most of the format specifiers are the same as all the other C-based languages, %d for integer types, %f for floating types, %c for char, etc.
In the case of Objective-C, the %# is used as the place holder for objects that respond to the description selector, which returns a string. (In the case of an NSString, it just returns the string itself, but you'll notice that you can put lots of other types of objects in here too... in fact, every object that inherits from NSObject has a default description method).

attaching an image to email from within the App

i'm trying to attach image an image to a mail that being sent from the App
i'm not sure how to do it, please advise here is my code:
NSString * subject = #"Hello";
NSString * address = #"myMail#gmail.com";
NSMutableString *strBody=[[NSMutableString alloc]init];
[strBody setString:_txtName.text];
[strBody appendString:_txtIngredients.text];
[strBody appendString:_txtPreper.text];
[strBody appendString:_txtServe.text];
[strBody appendString:_txtAboutdrink.text];
[strBody appendString:_txtSource.text];
//NSData *data = UIImagePNGRepresentation(_imgDrink);---- here is my problem
NSURL *url;
NSString * path = [NSString stringWithFormat:#"mailto:%#?subject=%#&body=%#", address, subject,strBody];
url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
If you don't mind displaying a form for the user to type a message into the email, I suggest using the MFMailComposeViewController. Through that, you can add attachments pretty easily. You can also pre-populate the recipients and the message body and pretty much everything else about the email. It's a handy class to have in your toolkit.

Write/Read NSMutableAttributedString to Plist

OK, here we are :
I've got an NSTextView
I'm getting it's NSMutableAttributedString content
I'm unable to read/write it to plist
By using Rob's code ( Saving custom attributes in NSAttributedString ), I've made some progress (I'm managing to write the data to disk), but I cannot recover it (= NSKeyedUnarchiver returns nil).
Encoding :
// where MAS --> NSMutableAttributedString
NSData* stringData = [NSKeyedArchiver archivedDataWithRootObject:MAS];
Decoding :
NSMutableAttributedString* mas = (NSMutableAttributedString*)[NSKeyedUnarchiver unarchiveObjectWithData:dat];
Any ideas? Any possible workaround (even if not with NSCoder, which I doubt it works with RTFs...) would be welcome!
And here's how it was solved.
NSAttributedString -> NSData :
NSData* j = [(NSMutableAttributedString*)MAS RTFDFromRange:NSMakeRange(0,[[MAS string] length])
documentAttributes:nil];
NSData -> NSAttributedString
NSMutableAttributedString* mas = [[NSMutableAttributedString alloc] initWithRTFD:dat
documentAttributes:nil];
Simple as that.

Too many arguments to method call ,expected 1 have 2

I'm calling a JSON URL from the NSURL class. My URL is:
like://Design_Time_Addresses/IServices/RoleService/json/Role/?name=%#",[textField text]];
When I write "doctor" in the textfield I have to get all the details of doctor through that URL. Similarly when I write "engineer" I have to get all the details of engineer. So it means what I type in the textfield, the name in URL should be replaced with the textfield value. But when I write the code like this:
NSURL *jsonUrl =[NSURL URLWithString:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSString *jsonStr =[[NSString alloc] initWithContentsOfURL:jsonUrl];
NSMutableDictionary *jsonDetails = [[NSMutableDictionary alloc]initWithDictionary:[jsonStr JSONValue]];
As shown here I am using the NSURL class to get this URL.
I am getting an error in the NSURL class. How do I pass the text fields value to a URL?
First line should be
NSString *urlString = [NSString stringWithFormat:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSURL *jsonUrl =[NSURL URLWithString:urlString];
NSURL URLWithString expects a single string argument, while you are providing two. Since you probably want to construct a single string from the #"http://..." string and [textField text], you should use an NSString method to concatenate them. You can't rely on NSURL to concatenate the string for you.

how to pass data from UIWebview to user defaults or another view

I have a embedded uiwebview in my app which will in turn call a webpage. This webpage contains some data which should be saved in the user defaults of the iphone. How to achieve this ?
It very much depends on what your page and data looks like... The basic approach is to use -stringByEvaluatingJavaScriptFromString: to retrieve content, e.g.:
NSString *script = #"document.getElementById('myTextInput').value";
NSString *result = [webView stringByEvaluatingJavaScriptFromString:script];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
result, #"MyTextInputValue",
// ... more?
nil];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:#"MyWebViewData"];
If the data is only used by scripts in the page, you could also simply use a script function that returns you one JSON string to store:
NSString *script = #"window.getJsonData()";
NSString *json = [webView stringByEvaluatingJavaScriptFromString:script];