attaching an image to email from within the App - objective-c

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.

Related

Add hyperlinks to NSString iOS

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

Parsing NSURL in Objective-C

I have an NSTextView control that could potentially have links in it. How do I get the full url of the link?
Here is what I have so far
-(BOOL)textView:(NSTextView *)aTextView clickedOnLink:(id)aLink atIndex:(NSUInteger)charIndex
{
NSURL *htmlURL = [NSURL fileURLWithPathComponents:[aLink pathComponents]];
}
This gives me a URL that begins with file://localhost ... How do I get rid of that portion of the URL?
NSURL* url = [NSURL URLWithString:#"http://localhost/myweb/index.html"];
NSString* reducedUrl = [NSString stringWithFormat:
#"%#://%#",
url.scheme,
[url.pathComponents objectAtIndex:1]];

iOs Custom scheme in MFMailComposeViewController

I'm trying to send via MFMailComposeViewController a custom url scheme so that the receipient could tap on the embedded link and open my application on his iphone.
So far the receipient gets an email but nothing happens when he taps on the LINK.
here is the code:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the user did with your email sheet
[picker setSubject:#"Incoming task"];
// Fill out the email body text
NSString * types = #"";
for(NSString *type in task.location_types){
types = [types stringByAppendingFormat:#"%#|",type];
}
if(types.length > 1){
types = [types substringToIndex:types.length - 1];
}
NSString *desc = [task.description stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *link = [NSString stringWithFormat:#"helpy://?taskid=%d&desc=%#&types=%#\"",task.TaskId,desc,types];
NSString *emailBody = [NSString stringWithFormat:#"TAP HERE<script type=\"text/javascript\" charset=\"utf-8\">function doSomething() { window.location = \"%#\;return false;}</script>",link];
[picker setMessageBody:emailBody isHTML:YES];
picker.navigationBar.barStyle = UIBarStyleBlack;
[controller presentModalViewController:picker animated:YES];
[picker release];
helpy://?taskid=..... is my custom registered URL Scheme and if I type it from the address bar of the browser it opens up my application on the iPhone.
For some reason when this link is embedded into email, tapping it does not do anything.
Any help?
thanks
I highly doubt that Mail will execute JavaScript on iOS (even on the desktop I highly doubt it, but haven't tried it though). Simply set the URL as the href attribute of your hyperlink and everything should work fine. Any reason why you want to do it with JavaScript? I'm just curious ;-)
NSString *link = [NSString stringWithFormat:#"helpy://?taskid=%d&desc=%#&types=%#\"",task.TaskId,desc,types];
NSString *emailBody = [NSString stringWithFormat:#"TAP HERE", link];

Object-c/iOS :About use NSUserDefaults set serial numbers/password

I got many issues about setting sn/pwd
Because the first default data is from URL
got a plist data like this:
{
serial_number=1234;
password = 7777;
}
The application finish launched,It will download from URL via the code like this
NSString *urlString = [[NSString alloc]initWithString:#"http://getdefaults.php"];
NSArray *plistData;
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURLURLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
NSString *listFile = [[NSString alloc] initWithData:returnDataencoding:NSUTF8StringEncoding];
[listFile dataUsingEncoding:NSUTF8StringEncoding];
plistData = [listFile propertyList];
At first , I try to create a plist data in iphone:
NSArray *localPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *localDocPath = [localPaths objectAtIndex:0];
NSString *localFilePath = [localDocPath stringByAppendingPathComponent:#"InputPassword.plist"];
NSMutableDictionary *localDictread = [[NSMutableDictionary alloc] initWithContentsOfFile:localFilePath];
Than I compare with the information I get
if(localDictread==nil){
//pop a textfield to let user enter the serial numbers and password
//if the text in the textfield is equal the default SN/PWD from plistData,write the sn/pwd to plist in iphone
}
But I found NSUserDefaults , Can it work as the same ?
I mean work as the same is can I record a serial number and password in my iphone ???
What is best way to record a user serial number/password in iphone ?
If you are a storing password, not NSUserDefaults nor serializing the data in NSDocumentDirectory are great solutions because data is stored with little protection. iOS provides the KeyChain mechanism for this porupose. It provides C interfaces that are a bit obscure, but a Buzz Andersen got a nice Objective-C interface going called SFHFKeychainUtils. With SFHFKeychainUtils you can do something like:
[SFHFKeychainUtils storeUsername:user.text andPassword:pass.text forServiceName:#"my_service" updateExisting:YES error:nil];
And that gets stored securely into the keychain. To recover the password:
NSString *pw = [SFHFKeychainUtils getPasswordForUsername:user.text andServiceName:#"my_service"

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];