nsurl

NSString encoding returns nil on url content

 ̄綄美尐妖づ 提交于 2019-12-18 09:35:04
问题 I'm following an iOS Swift guide on Udemy and this is the first issue I cannot work around: I am supposed to see html etc printed to the console but instead I get null. This is the section: let url = NSURL(string: "https://google.com") let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in if error == nil { var urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding) print(urlContent) } } task.resume() If I print just the data then it gives me

Parse NSURL path and query on iOS

你说的曾经没有我的故事 提交于 2019-12-18 04:02:14
问题 Are there any standard objects or functions to parse an NSURL's components? Clearly I could write one, but why re-invent the wheel? [NSURL path] will return an NSString like "argX=x&argY=y&argZ=z" What I would rather get back is a dictionary populated with {@"argX" => @"x", @"argY" => @"y", @"argZ" = @"z"} For the path, which returns a string like "/partA/partB/partC", I would rather get an array with the structure {[0] => @"partA", [1] => @"partB", [2] => @"partC"} I realize this is a pretty

Parse NSURL path and query on iOS

Deadly 提交于 2019-12-18 04:02:07
问题 Are there any standard objects or functions to parse an NSURL's components? Clearly I could write one, but why re-invent the wheel? [NSURL path] will return an NSString like "argX=x&argY=y&argZ=z" What I would rather get back is a dictionary populated with {@"argX" => @"x", @"argY" => @"y", @"argZ" = @"z"} For the path, which returns a string like "/partA/partB/partC", I would rather get an array with the structure {[0] => @"partA", [1] => @"partB", [2] => @"partC"} I realize this is a pretty

How to find out if the NSURL is a directory or not

怎甘沉沦 提交于 2019-12-17 23:25:16
问题 I have a NSURL object. It has address of a filesystem element, it is either a file or a directory. I want to be able to tell if the NSURL is a directory or a file. I have already tried this, which doesn"t seem to work! NSURL * temp ....... ;// it is initialized and has a valid value CFURLRef xx = (CFURLRef)CFBridgingRetain(temp); if(CFURLHasDirectoryPath(xx)) NSLog(@"was a file"); else NSLog(@"was a folder"); 回答1: NSNumber *isDirectory; // this method allows us to get more information about

NSURL found nil while unwraping an Optional value

孤街醉人 提交于 2019-12-17 20:45:15
问题 Anyone knows why am I getting fatal error: unexpectedly found nil while unwrapping an Optional value When i use let URL = NSURL(string: "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907")! 回答1: The | character its not a valid URL character so you must replace it with percent escape character. Encoding whole string will do that automatically for you var stringUrl = "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907"

How To Get HTML source from URL with Swift

依然范特西╮ 提交于 2019-12-17 15:36:30
问题 I need to look at the HTML of a page given by a certain URL. If I have this, what is the most efficient and synchronous way to get the HTML source for that URL using Swift? I haven't been able to find a concise way online that returns it into a variable as opposed to printing it in a completionHandler. I need to manipulate the source outside of whatever call uses the URL. How is this done in Swift? 回答1: Disclaimer : Since this is getting quite a lot of views, I just want to remind everyone

App Updates, NSURL, and Documents Directory

霸气de小男生 提交于 2019-12-17 11:57:07
问题 I have an app in the app store that uses Core Data to persist much of the data. The exception is storing images. I store images in subdirectories to the Documents directory and store an NSURL reference to that image in the appropriate object attribute in the core data store. We have noticed that, when an update makes it to the app store, those images are not found, and thus don't display, using the references stored with the previous version of the app. Now, I have a suspicion that the

Leak from NSURL and AVAudioPlayer using ARC

坚强是说给别人听的谎言 提交于 2019-12-17 10:45:24
问题 I am runung Instruments on an iPhone 4S. I am using AVAudioPlayer inside this method: -(void)playSound{ NSURL *url = [self.word soundURL]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; if (!error) { [audioPlayer prepareToPlay]; [audioPlayer play]; }else{ NSLog(@"Problem With audioPlayer on general card. error : %@ | url %@",[error description],[url absoluteString]); } I am getting leaks when playing the sound files: Leaked objects: 1. Object :

How to use openURL for making a phone call in Swift?

家住魔仙堡 提交于 2019-12-17 07:23:59
问题 I have converted the code for making a phone call from Objective-C to Swift, but in Objective-C, we can set the type of the URL that we like to open (e.g. telephone, SMS, web) like this: @"tel:xx" @"mailto:info@example.es" @"http://stackoverflow.com" @"sms:768number" The code in Swift is: UIApplication.sharedApplication().openURL(NSURL(string : "9809088798") I read that have not released any scheme parameter for tel: , but I don't know if Swift can detect if the string is for making a phone

Saving an array of NSURL to NSUserDefaults

女生的网名这么多〃 提交于 2019-12-14 03:57:21
问题 I have an array of NSURL items like this: var array = [NSURL]() I'm trying to save this to NSUserDefaults: let defaults = NSUserDefaults(suiteName: "group.a.project") defaults?.setObject(array, forKey: "NSURLarray") Will result in it crashing as NSURL can't be stored in NSUserDefaults. How can I save this array of NSURL's to NSUserDefaults, so that I can then access it and use the NSURL's in the array as required (saving an array of strings and then trying to use them as NSURL's will result