Here is my code,where the crash occurs:-
let URL = NSURL(string: "http://files.parsetfss.com/fa80bc63-88d4-412d-a478-2451cffc92a9/tfss-1d2a321d-b02e-4745-a589-e31536f648df-XXXXX%20CAT15%2030.p0001.jpg")
NSLog("Loading page with URL: \(URL)")
The app crashes with EXC_BAD_ACCESS
The first argument of NSLog() is a format string, and contains
format specifiers (starting with %) which are expanded by the
following variable argument list. In your case %20C is a format specifier, but
no matching argument is supplied. That causes undefined behavior,
it can crash or produce incomplete or wrong output.
If you want to use NSLog() then a general safe method is
NSLog("%#", "Loading page with URL: \(URL)")
In this particular case,
NSLog("Loading page with URL: %#", URL)
works as well, since NSURL is a NSObject subclass and can be used
with the %# format.
You should use println instead of NSLog.
let URL = NSURL(string: "http://files.parsetfss.com/fa80bc63-88d4-412d-a478-2451cffc92a9/tfss-1d2a321d-b02e-4745-a589-e31536f648df-XXXXX%20CAT15%2030.p0001.jpg")!
println("Loading page with URL: \(URL)")
I have added the option sign ! at the end to unwrap.
Related
I am working on sharing a content on Facebook for an iOS app using Swift.
I have written a singleton class called FBManager and a function as below.
func shareContent(content:String, contentURL:String?, contentTitle:String? , fromController controller:UIViewController {
let shareDialog = FBSDKShareDialog()
let shareLinkContent = FBSDKShareLinkContent()
shareLinkContent.contentDescription = content
if let url = contentURL
{
shareLinkContent.contentURL = NSURL(string: url)
}
if let title = contentTitle
{
shareLinkContent.contentTitle = title
}
shareDialog.delegate = self
shareDialog.fromViewController = controller
shareDialog.shareContent = shareLinkContent
shareDialog.show()
}
But this does not even show a share dialog both on iOS 8 and iOS 9.
Instead the following delegate method gets called
func sharer(sharer: FBSDKSharing!, didFailWithError error: NSError!) {
}
with the error - "The operation couldn’t be completed. (com.facebook.sdk.share error 2.)"
Can someone please help ?
Facebook SDK's error codes are somewhat ambiguous because they cover rather large domains of errors. The code you provided does not really show the content of the variables and so I cannot pinpoint the problem. However, com.facebook.sdk.share error 2 is an Invalid Argument error, which usually arises from an invalid format of one or more members of FBSDKShareLinkContent.
Generally, you can use the FBSDKErrorCode enum to switch over the (error as NSError).code and find which domain it belongs to. (In this case, it'll point to Invalid Argument)
You can also print(error) directly in the didFailWithError delegate method, which will output a very descriptive log of the error and what caused it specifically.
Check your contentURL, make sure it starts with http:// or https:// or any other valid protocol. Same for the imageURL if you're using or planning to use one. This most likely caused your error!
The SDK's error codes reference may be helpful too.
I'm pulling a URL from a string and turning that into a button to a WebView of the link.
This is the error I'm getting...
2015-11-10 18:58:05.159 MPSTApp[520:169178] -canOpenURL: failed for URL: "https:/www.facebook.com/prontosantateresa -- file:///" - error: "This app is not allowed to query for scheme file"
For this instance the string is https://www.facebook.com/prontosantateresa but I believe it's using the double // as an escape character.
The code calling the url link is such -
var anchorLink: String?
func loadWebPage(){
let requestURL = NSURL(string: anchorLink!)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)
}
It's exactly what the error message says: It tries to open a file:// url. So, your algorithm for retrieving the https:// url seems to do something wrong and turn "//" into "/". It could also come handy to add the NSAllowArbitaryLoads key to your Info.plist.
I investigated issue. Since I was using an app browser, it didn't need to use UIApplication.canOpenUrl().
So, to resolve this I replaced the event method with below code in
#IBAction func website1ButtonPressed(sender: UIButton) {
if self.anchorLink != nil{
self.performSegueWithIdentifier("categoryDetailToWebSegue", sender: nil)
}
}
I use WKWebview to load a URL.
let webView = WKWebview()
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
webView.loadRequest(request)
How can I detect if the link the webView should load is broken?
You can use canOpenUrl method:
UIApplication.sharedApplication().canOpenURL(url)
It will do the url validation and if the link is ok it returns true.
It's mostly use before you call:
UIApplication.sharedApplication().openURL(url)
to make sure this link can be open in safari but it should help you here too.
Make sure the link starts with http:// or https://.
Edited:
It will just check is the link is a correct url.
If you want to see the page is offline, authorisation issues, etc. you can implement WKNavigationDelegate protocol and check out this method:
- webView:didFailNavigation:withError:
this should give you more info.
It's always good idea to use: str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
it make sure that you don't pass a character which are not allowed in URL.
Edited 2:
To detect the status code you can try to implement:
- webView:decidePolicyForNavigationResponse:decisionHandler:
the navigation response is an NSURLResponse instance but
whenever you make an HTTP request, the NSURLResponse object you get back is actually an instance of the NSHTTPURLResponse class so you should cast it to NSHTTPURLResponse. That should give you a statusCode.
In the last line in the method you should call handler, for example decisionHandler(WKNavigationResponsePolicyAllow).
Ref: Answer exists here
if let url = NSURL(string: yourUrlString) {
var canOpen = UIApplication.sharedApplication().canOpenURL(url)
}
If you want to check if url string is correct and valid - just create NSURL object, if path contains error it will cast to nil:
let string = "http://google.com"
let url = NSURL(string: string)
let brokenString = "http:\\brokenurl.12"
let brokenUrl = NSURL(string: brokenString) // nil - it's not valid!
If you have implemented NSURLConnectionDelegate then below solution can be used.
didFailWithError method of NSURLConnectionDelegate can be used for this.
This method get called if an error occurs during the loading of a resource.
Can refer to below link
https://developer.apple.com/documentation/foundation/nsurlconnectiondelegate/1418443-connection
If my url has a space at the end it crashes with EXC_BAD_INSTRUCTION.
I thought canOpenURL would trap for a bad URL but it doesn't.
if let strurl = cell.url{
if (UIApplication.sharedApplication().canOpenURL(NSURL(string:strurl)!)) {
UIApplication.sharedApplication().openURL(NSURL(string:strurl)!);
}
}
In my case a URL with an extra space at the end crashes it. I can trim white space, but what about anything else? Is there a proper way to trap for this error?
You are force unwrapping NSURL which causes the app to crash if it happens to be nil. To check the validity of the URL you should be using if let on the URL itself as well.
if let strurl = cell.url,
let url = NSURL(string: strurl) {
if (UIApplication.sharedApplication().canOpenURL(url) {
UIApplication.sharedApplication().openURL(url)
}
}
I'm making a very simple app that uses a UIWebView to display a pdf map that can be zoomed in on, panned, etc.
However, when creating a target url for the pdf, the pathForResource call isn't working right. This is my code:
var targetURL : NSURL = NSBundle.mainBundle().pathForResource(filename, ofType: type)
I get an error on the parentheses before "filename", that says Missing argument for parameter 'inDirectory' in call. I tried adding an argument for this:
var targetURL : NSURL = NSBundle.mainBundle().pathForResource(filename, ofType: type, inDirectory: "Map")
I don't know what to put for inDirectory, because I don't know what the directory is - I added the file to my project and it is in the same folder as my ViewController.swift file. Anyway, it doens't really matter, because I get the following error in the same place, Extra argument 'inDirectory in call.
What do I do?
pathForResource() does not return a NSURL object, but a String?. Declare your variable accordingly and it should work.
var targetURL : String? = NSBundle.mainBundle().pathForResource(filename, ofType: type)
Or, of course, if you would rather - just use URLForResource() instead.
var targetURL : NSURL? = NSBundle.mainBundle().URLForResource(filename, withExtension: type)
I was having this same problem. In my case it was because my variable (in your case type) needed to be unwrapped. Adding a bang ! made the error go away and made the code execute.