I'm new to iPhone development and Objective-C. Using the ZBar SDK I've developed a basic app which scans a QR image from the photo album and outputs what it translates to.
我是iPhone开发和Objective-C的新手。使用ZBar SDK我开发了一个基本应用程序,它可以扫描相册中的QR图像并输出它所转换的内容。
I want to know if there is a way to take this output, determine whether it is a URL and if so open it in a web browser.
我想知道是否有办法获取此输出,确定它是否是URL,如果是这样,则在Web浏览器中打开它。
3 个解决方案
#1
24
NSURL's URLWithString returns nil if the URL passed is not valid. So, you can just check the return value to determine if the URL is valid.
如果传递的URL无效,NSURL的URLWithString将返回nil。因此,您只需检查返回值即可确定URL是否有效。
UPDATE
UPDATE
Just using URLWithString:
will usually not be enough, you probably also want to check if the url has a scheme and a host, otherwise, urls such as al:/dsfhkgdsk
will pass the test.
仅使用URLWithString:通常是不够的,您可能还想检查url是否有方案和主机,否则,诸如al:/ dsfhkgdsk之类的URL将通过测试。
So you probably want to do something like this:
所以你可能想做这样的事情:
NSURL *url = [NSURL URLWithString:yourUrlString];
if (url && url.scheme && url.host)
{
//the url looks ok, do something with it
NSLog(@"%@ is a valid URL", yourUrlString);
}
If you only want to accept http URLs you may want to add [url.scheme isEqualToString:@"http"]
.
如果您只想接受http URL,可能需要添加[url.scheme isEqualToString:@“http”]。
#2
2
Here is an alternative solution which I came across. You can do like this.
这是我遇到的另一种解决方案。你可以这样做。
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourstring"]];
bool valid = [NSURLConnection canHandleRequest:req];
Source : https://*.com/a/13650542/2513947
来源:https://*.com/a/13650542/2513947
#3
0
You can look at below code to check whether a string contains website url or simple text.
您可以查看下面的代码来检查字符串是否包含网站URL或简单文本。
NSString *getString =[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"];
if([getString rangeOfString:@"Www"].location == NSNotFound
&& [getString rangeOfString:@"www"].location == NSNotFound && [getString rangeOfString:@"com"].location == NSNotFound)
{
[companyNameTF setText:[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"]];
}
else
{
[websiteTF setText:[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"]];
}
#1
24
NSURL's URLWithString returns nil if the URL passed is not valid. So, you can just check the return value to determine if the URL is valid.
如果传递的URL无效,NSURL的URLWithString将返回nil。因此,您只需检查返回值即可确定URL是否有效。
UPDATE
UPDATE
Just using URLWithString:
will usually not be enough, you probably also want to check if the url has a scheme and a host, otherwise, urls such as al:/dsfhkgdsk
will pass the test.
仅使用URLWithString:通常是不够的,您可能还想检查url是否有方案和主机,否则,诸如al:/ dsfhkgdsk之类的URL将通过测试。
So you probably want to do something like this:
所以你可能想做这样的事情:
NSURL *url = [NSURL URLWithString:yourUrlString];
if (url && url.scheme && url.host)
{
//the url looks ok, do something with it
NSLog(@"%@ is a valid URL", yourUrlString);
}
If you only want to accept http URLs you may want to add [url.scheme isEqualToString:@"http"]
.
如果您只想接受http URL,可能需要添加[url.scheme isEqualToString:@“http”]。
#2
2
Here is an alternative solution which I came across. You can do like this.
这是我遇到的另一种解决方案。你可以这样做。
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourstring"]];
bool valid = [NSURLConnection canHandleRequest:req];
Source : https://*.com/a/13650542/2513947
来源:https://*.com/a/13650542/2513947
#3
0
You can look at below code to check whether a string contains website url or simple text.
您可以查看下面的代码来检查字符串是否包含网站URL或简单文本。
NSString *getString =[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"];
if([getString rangeOfString:@"Www"].location == NSNotFound
&& [getString rangeOfString:@"www"].location == NSNotFound && [getString rangeOfString:@"com"].location == NSNotFound)
{
[companyNameTF setText:[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"]];
}
else
{
[websiteTF setText:[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"]];
}