nsurl

How to convert NSURL to String in Swift

萝らか妹 提交于 2019-12-20 10:19:02
问题 How do you convert an NSURL to a String in Swift? The following: var directoryURL: NSURL var urlString: String = nsurlObject as String Throws the error: Cannot convert value of type 'NSURL' to type 'String' in coercion 回答1: It turns out there are properties of NSURL you can access (see Swift Reference): var directoryURL: NSURL var urlString: String = directoryURL.absoluteString // OR var urlString: String = directoryURL.relativeString // OR var urlString: String = directoryURL.relativePath //

Random websites button

醉酒当歌 提交于 2019-12-20 07:56:34
问题 this one is pretty simple I guess but I just can't find the answer that would suit perfectly for my issue. I want to make a button that opens a random URL from a list I'll give him, let's say - google, youtube and facebook just for the example. This is my line of code that connects now only to google...: - (IBAction)site:(id)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"]]; } Can someone please advise me what to add to the code so it will

NSURL fail able initialiser initWithString: does not return nil on empty String in Swift

。_饼干妹妹 提交于 2019-12-20 04:53:43
问题 It is easy to reproduce in a Playground import Foundation let urlString = "" let url = NSURL(string: urlString) print(url) The result of url is somehow (no URL) instead of the expected nil and the printout is Optional() with neither .Some or .None . I would really like some insight on this one, because it caught me by surprise and causes a crash in production code. 回答1: After i filled a Radar for this i got the following answer: Apple Developer Relations An empty string is a valid URL string.

NSURL is returning nil for a valid URL

我的未来我决定 提交于 2019-12-20 03:11:29
问题 I have a valid URL for google maps , which if you run in your browser would show an image of a map. However, when I put it inside my Swift code and try to create an NSURL from String it returns nil let urlString = "https://maps.googleapis.com/maps/api/staticmap?maptype=hybrid&center=37.33233141,-122.0312186&path=color:0xff0000|weight:10|37.33233141,-122.0312186|21.422570,%2039.826190&markers=color:blue%7Clabel:S%7C37.33233141,-122.0312186&markers=color:blue%7Clabel:S%7C21.422570,39.826190

UIWebView: Error while loading URL

梦想的初衷 提交于 2019-12-19 19:52:11
问题 I am trying to build an RSS reader for the Tom's Hardware website. I have an error when I try to load an URL into an UIWebview from an RSS link. Here'is my code: - (void)viewDidLoad { [super viewDidLoad]; if (self.url != nil) { NSURL *requestUrl = [NSURL URLWithString:self.url]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl]; [webView loadRequest:requestObj]; } } The url is setted by the parent controller. When the URL comes directly from the RSS, I have an error in log:

Add http:// to NSURL if it's not there

狂风中的少年 提交于 2019-12-19 06:28:18
问题 I am using web view in my app, getting a URL from a text field. It works if the string starts with "http://". I am trying to modify the code so that it can also handle the situations where users don't enter "http://" or "https://" How to check if the URL doesn't have "http://" in it ? How to modify the URL to add "http://" in it ? NSString *URLString = textField.text; NSURL *URL = [NSURL URLWithString:URLString]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; [self.webView

Url minus query string in Objective-C

ε祈祈猫儿з 提交于 2019-12-19 05:03:16
问题 What's the best way to get an url minus its query string in Objective-C? An example: Input: http://www.example.com/folder/page.htm?param1=value1&param2=value2 Output: http://www.example.com/folder/page.htm Is there a NSURL method to do this that I'm missing? 回答1: There's no NSURL method I can see. You might try something like: NSURL *newURL = [[NSURL alloc] initWithScheme:[url scheme] host:[url host] path:[url path]]; Testing looks good: #import <Foundation/Foundation.h> int main(int argc,

How to make an NSURL that contains a | (pipe character)?

风流意气都作罢 提交于 2019-12-18 14:10:04
问题 I am trying to access google maps' forward geocoding service from my iphone app. When i try to make an NSURL from a string with a pipe in it I just get a nil pointer. NSURL *searchURL = [NSURL URLWithString:@"http://maps.google.com/maps/api/geocode/json?address=6th+and+pine&bounds=37.331689,-122.030731|37.331689,-122.030731&sensor=false"]; I dont see any other way in the google api to send bounds coordinates with out a pipe. Any ideas about how I can do this? 回答1: Have you tried replacing the

how to get width and height of image from url without downloading?

别等时光非礼了梦想. 提交于 2019-12-18 12:58:26
问题 I'm using SDWebImage for showing images inside cells. But it is perfect mached to frame of UImageView that I'm doing in code below: NSString * s =[NSString stringWithFormat:@"url of image to show"]; NSURL *url = [NSURL URLWithString:s]; [cell.shopImageView sd_setImageWithURL:url]; My UIImageView is size 50x50. For example image from url is size 990x2100 and my image is not displaying well in the frame given. In this case when hight is bigger, I want to resize my image by proper height ratio

NSUrl from NSString returns null

时光怂恿深爱的人放手 提交于 2019-12-18 09:44:28
问题 I want to parse the location of my Documents Directory to NSUrl from NSString I have. when my console NSLog(stringURL) i get: "/var/mobile/Applications/2B35D06D-6E4A-40F2-833E-28463A946834/Library/Application Support/MyAppDirectory" as an output. but when I do NSURL* url = [NSURL URLWithString:stringUrl]; and NSLog(url) i get (null) as an output. WHat am I doing wrong here? 回答1: From the apple documentation "An NSURL object initialized with URLString. If the URL string was malformed or nil,