UitextField and UIWebView interaction? - iphone

I put some text into the textField like: "Peter.at", and the UIwebview shows all correct (Mag. pharm. Peter MÜLLER)!
The Problem is when I type in the UItextField: "Püter.at", the UIwebView does nothing!
What is wrong? I think it is the "ü" but I don't know how to fix it!

Yep. I see what the problem is.
Your code should look like this:
NSString *preURL = #"";
NSString *middleURL = #".at";
NSString *fullURLString =
[[NSString stringWithFormat:#"%#%#%#",preURL,1.text,middleURL] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL * preparedURL = [[NSURL URLWithString:fullURLString];
[uiwebnamesearch loadRequest: [NSURLRequest requestWithURL: preparedURL]];
[uiwebnamesearch reloadInputViews];
The important change here is that you are "percent escaping" the string, to make it possible to use in a URL.
Here is the documentation for that [NSString stringByAddingPercentEscapesUsingEncoding:] method.

Related

UIlabel into a url?

i created a uilabel that contains my location data using this code,
longitudedata.text = [NSString stringWithFormat:#"longitude=%f", location.coordinate.longitude];
speeddata.text = [NSString stringWithFormat:#"speed=%f", [location speed] * 2.2369];
altitudedata.text = [NSString stringWithFormat:#"altitude=%f", [location altitude]];
but now i need to add these in a url so it will send that data to my server but everything iv tried dosnt work :(
NSURL *myURL = [NSURL URLWithString:[#"http://servername/ldtracker.aspx?id=3&" stringByAppendingString:longitudedata.text]];
and so on but this dosnt work, seems like iv tried everything
im a beginner to xcode, please help :)
UILabels dont handle urls, u need to give them a NSString.
label.text = [NSString stringWithFormat:#"http://servername/ldtracker.aspx?id=3&%#", longitudedata.text];
this should work if i understood ur problem correctly
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://servername/ldtracker.aspx?id=3&%#&%#%#",longitudedata.text,speeddata.text,altitudedata.text]];

Crop NSURL / NSString

I got some URL's heading to certain mp3's like:
(1) localhost://blablabla/song1.mp3
(2) localhost://blablabla/songwithmorechars.mp3
and so on.
How can I crop the URL's to:
(1) song1.mp3
(2) songwithmorechars.mp3
Need to display the current song my AVAudioPlayer is playing in a UILabel.
Thanks
SOLUTION:
Here's the deal:
titleLabel.text = [[[self.audioPlayer.url absoluteString] lastPathComponent] stringByReplacingOccurrencesOfString:#".mp3" withString:#""];
Take the substring with the last / (there's a method in ObjC for that!)
NSString *sub = [url lastPathComponent];
Here's the info for that method:
NSString lastPathComponent Apple Doc
Just use [url lastPathComponent], you don't need to convert it to a string first.
use
NSString *lastString = [yourStringName lastPathComponent];
NSURL *firstURL = [NSURL URLWithString:#"localhost://blablabla/song1.mp3"];
NSString *firstString = [firstURL absoluteString];
NSLog(#"Name:%#",[firstString lastPathComponent]);
From docs:
NSURL Class Reference
NSString Class Reference

NSURL with string

I have problem with NSURL. I am trying to create NSURL with string
code
NSString *prefix = (#"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [[NSURL alloc] initWithString:dialThis];
NSLog(#"%#",url);
also tried
NSURL *url = [NSURL URLWithString:dialThis];
but it gives null . what is wrong ?
Thanks..
Your problem is the unescaped spaces in the URL. This, for instance, works:
NSURL *url = [NSURL URLWithString:#"tel://1234567890x101"];
Edit: As does this..
NSURL *url2 = [NSURL URLWithString:[#"tel://1234567890 ext. 101"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Before passing any string as URL you don't control, you have to encode the whitespace:
NSString *dialThis = [prefix stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// tel://1234567890%20ext.%20101
As a side note, iOS is not going to dial any extension. The user will have to do that manually.
From Apple URL Scheme Reference: Phone Links:
To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number.
Im not sure the "ext." in phone number can be replce by what value? but you can try like this,
NSString *prefix = [NSString stringWithString: #"tel://1234567890 ext. 101"];
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[dialThis stringByReplacingOccurrencesOfString:#" ext. " withString:#"#"]];
// it might also represent by the pause symbol ','.
you can go to find the ext. is equivalent to what symbol in the phone, then replace it.
but dunno it can be work in actual situation or not....
As with iOS 9.0,
stringByAddingPercentEscapesUsingEncoding:
has been deprecated.
Use the following method for converting String to NSURL.
let URL = "URL GOES HERE"
let urlString = URL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())
If you've got something you think should be a URL string but know nothing about how URL strings are supposed to be constructed, you can use NSURL's URLWithDataRepresentation:relativeToURL: method. It parses the URL string (as bytes in an NSData) and percent-encodes characters as needed. Use the NSUTF8StringEncoding for best results when converting your NSString to NSData.
NSURL *url = [NSURL URLWithDataRepresentation:[#"tel:1234567890 ext. 101" dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil];
NSLog(#"%#",url);
creates a URL with the string 1234567890%20ext.%20101
It attempts to do the right thing. However, for best results you should find the specification for the URL scheme you using and follow it's syntax to create your URL string. For the tel scheme, that is https://www.rfc-editor.org/rfc/rfc3966.
P.S. You had "tel://" instead of "tel:" which is incorrect for a tel URL.
Try this one, It works for me....
NSString *prefix = (#"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[queryString stringByReplacingOccurrencesOfString:#" " withString:#"%20"]];
NSLog(#"%#",url);
Make an extension for use in any part of the project as well:
extension String {
var asNSURL: NSURL! {
return NSURL(string: self)
}
}
From now you can use
let myString = "http://www.example.com".asNSURL
or
myString.asNSURL

I have a link to my terms and conditions data and I want to show this data by reading it

How do I read data from an USL link, and display it on my textview? The URL link content contains only normal text
I am using:
NSString *userAgreementStringURL = [NSString stringWithFormat:#"%#/requestConfigurationData.php?ownerID=%#&view=userAgreement", serverURL, ownerID];
NSURL *userAgreementURL = [NSURL URLWithString:userAgreementStringURL];
NSURLRequest *userAgreementRequest = [NSURLRequest requestWithURL:userAgreementURL];
userAgreementData = [[NSMutableData alloc] init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
userAgreementConnection = [[NSURLConnection connectionWithRequest:userAgreementRequest delegate:self] retain];
[userAgreementTextView setText:[NSString stringWithContentsOfURL:#"http://medicinephone.com/dev2/visit/requestConfigurationData.php?ownerID=13&view=userAgreement" encoding:NSUTF8StringEncoding error:NULL]];
Show us your code. If you are trying what I think you are trying passing an array to text view's setText method won't work. You need to get the string from the array prior to that. If you array contains one word per array element you might want to try using componentsJoinedByString.
[myTextView setText:[NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://apple.com" encoding:NSUTF8StringEncoding error:NULL]]];
one line of code, thank you apple.
Edit: forgot to make a NSURL from the string.

NSURL declaration /allocation showing error

I Have tried
NSString *alertTxt =textfieldName.text;
NSLog(#"Name: %#",alertTxt);
NSURL *urlAddress = [[NSURL alloc ] initWithString: #"%#/login/index.php",alertTxt];
error : too many argument to function initstring ..
the user will giv the url address to the alertText field and i hav to embed in to the webkit may i know the process how to make it !!!
Where im making mistake kindly help me out in this
Thanks
-initWithString: can only take a complete string, not a format string. To use -initWithString: you need to complete the string first.
NSString* urlString = [NSString stringWithFormat:#"%#/login/index.php", alertTxt];
NSURL* urlAddress = [[NSURL alloc] initWithString:urlString];
BTW, it may be more efficient to use -stringByAppendingString:.
NSString* urlString = [alertTxt stringByAppendingString:#"/login/index.php"];