fprintf causes device to crash but not simulator? - ios

I have an iPod touch running iOS 4.2.1, and a simulator running iOS 4.2. In the simulator, my code works just fine, but on the device, it crashes.
The code in question is:
FILE *file = fopen([[#"niplot/" stringByAppendingString:text_field.text] fileSystemRepresentation], "w");
...
fprintf(file, "%s", [print_str UTF8String]); //crash occurs here, EXC BAD ACCESS
...
fclose(file);
Could it have something to do with the file or folder not existing? The simulator created them for me in this case. I'd try to create the files and folders manually on the device, but I'm not sure how. Also, I've tried the file names with and without a .txt extension.

Here's some examples of file save/load/delete.

Related

htmlDocPtr/gethtml issue

I have this kind of code in an iOS app of mine:
NSString *docNameString;
docNameString=#"https://www.mysite.php";
documentHTML=gethtml((char*)[docNameString UTF8String],
(char*)[#"UTF-8" UTF8String]);
It was working up to now (meaning last time I touched the app in Xcode).
But now it is no longer working. I get this message in the Xcode console:
I/O warning : failed to load external entity "https://www.mysite.php"
Document not parsed successfully.
I am currently using Xcode Version 10.0.
Anyone has an idea of how to deal with this issue?

AVPlayer unable to play video from NSDocuments Directory on iOS8

I'm trying to get my app to play a video that has been downloaded to the Documents Directory.
My code works when tested on the iOS 7.1 simulator but not on iOS 8.0 devices or the iOS 8.0 simulator. There isn't any error, AVPlayer does not play the video and shows a Black View.
I also checked the Document's folder of my device with XCode's Organizer and the file appears to be successfully downloaded.
Here is a simplified version of my code.
// file_path is a string that stores the path to the video file a println of it shows
// "/var/mobile/Containers/Data/Application/83C7837C-59B5-4767-A579-7CE758A93C6F/Documents/og4gr.mp4"
var path:NSURL = NSURL.fileURLWithPath(file_path, isDirectory: false);
_player = AVPlayer(URL: path);
Has anyone else encountered this? Thank you for your time!
So the problem was that the Application Documents Folder Path changes every time you launch the app and I was storing the absolute path of the video file.
According to this article here:
http://pinkstone.co.uk/where-is-the-documents-directory-for-the-ios-8-simulator/, this is how it is now in iOS 8, which is why while my method worked in iOS7 it was failing on iOS8 devices and Simulators. Thank you #RoboticCat for pointing me in the right direction!

CGPDFDocumentCreateWithURL(filePath) returns null on device, but not on simulator

I'm using following line to create a PDF reference:
CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateWithURL(filePath);
On my simulator, this is working.
But when I deploy this to my device, the call returns "null"
The filePath is exactly the same.
As a test, I'm also printing if the filePath (PDF) exists and it does.
I also checked for case sensitive things.
My device is running iOS 6.1.2 and I'm testing with Simulator 6.0.
Any ideas?

Fresh SpeakHere example app has error when recording audio in Simulator/Xcode

I can't record audio using the SpeakHere example app from apple. When I run the app in Simulator from within Xcode, it starts up normally, but when I press the record button, the error "Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)" occurs:
The log message about the missing root view controller at app startup is already there BEFORE the above error occurs and it is probably not connected to my problem.
I have downloaded the SpeakHere example project from the linked website (see top of this question), opened the fresh download in Xcode and directly started the app. I did not modify any setting and not any line of code. I've also searched on google and stackoverflow for this problem and didn't find a solution, although this problem must be very general.
I use Xcode Version 4.5.2 (4G2008a) and a MacBook Pro from late 2009 with Mac OS X 10.8.
I've also had a friend try this on his computer and he has the very same problem. He has the same OS and his XCode version is also 4.5.2.
I would now try older Xcode versions, but right now I don't like to download a few gigabytes for a trial'n'error approach on my connection.
Any help appreciated, including reports like "works for me with Xcode version ...". Thanks!
The problem occurs because in the method AQRecorder::StartRecord(CFStringRef inRecordFile), the function CFURLCreateWithString() fails and returns a pointer to nil. This is not detected and later on the code calls CFRelease() on this nil pointer, which causes the EXC_BREAKPOINT.
The purpose of the method CFURLCreateWithString() basically is to take a url string as input and return a pointer to a CFURL object as output. The problem here is that the input is not a url string. Instead, it's simply a path on the local file system without file:/ or the like as prefix. For this reason, this method fails.
The solution is to remove the not-working call to the method CFURLCreateWithString() and instead call a related method, namely CFURLCreateWithFileSystemPath(), which is prepared to take a local file system path and convert it to a CFURL:
In the method AQRecorder::StartRecord(CFStringRef inRecordFile), replace or comment out the line
url = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)recordFile, NULL);
and insert
url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)recordFile, kCFURLPOSIXPathStyle, false);
at its place.
url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)recordFile, kCFURLPOSIXPathStyle, false);
The code above made my xcoce 4.6 compile and run in simulator, but it doesnot record my voice from my usb microphone, I test my microphone in the garash band application and sucessfully record and play my voice, and the dbmeter does not move at all, any way when I port it to the real device it work, it just can't record and play voice in my simulator.

Logging data on device and retrieving the log

On a debug build in Xcode, regardless of whether I am using the simulator or an actual device, NSLog, printf, fprintf assert and NSAssert statements come out on the console
If I now run a release build on the device (say I send a test flight build and big it up on my iPhone; this will be a release build), which of these (if any) are getting recorded?
And how do I retrieve the log?
Does NSLog actually output something on release build? What is the determining factor? Whether it is writing to stdout or stderr? is only stderr written to device log? Does this mean I have to use fprintf? Is ANYTHING written to device log? is there even such a thing? If so, how to pick it up?
Could someone clarify the situation?
In Xcode 6.1.1, you can view the NSLog output by doing the following. However, I'm not sure if it lets you see logs from too far back in time. I've only seen it go back up to a couple hours.
In any case, here are the steps:
In Xcode, go to Window -> Devices.
Select your device in the left panel.
Click the little arrow as shown in the screenshot below.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:#"%#.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
Just add this block of code in application:didFinishLaunchingWithOptions method in the app delegate file and it will create a log file in app document directory on iPhone which logs all console log events. You need to import this file from iTunes to see all console events.
Note: In the .plist file make sure that Application supports iTunes file sharing is exists and is set to YES so that you can access through iTunes.
To get Logfiles :
Launch itunes, after your device has connected select Apps - select your App - in Augument Document you will get your file. You can then save it to your disk
In swift 4.0+, the code of Shyl will changes to,
var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(Date()).log"
let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName)
freopen(logFilePath.cString(using: String.Encoding.ascii)!, "a+", stderr)
all other process are same that explained by Shyl
Just add this block of code in application:didFinishLaunchingWithOptions method in the app delegate file and it will create a log file in app document directory on iPhone which logs all console log events. You need to import this file from iTunes to see all console events.
Note: In the .plist file make sure that Application supports iTunes file sharing exists and is set to YES so that you can access
through iTunes.
To get Logfiles : Launch iTunes, after your device has connected
select Apps - select your App - in Augument Document you will get your
file. You can then save it to your disk
NSLog is written to device log in production release and you can check this by connecting your iPhone to your system and using Organizer. Select your iPhone in the organizer, click Device Logs. You would see all NSLog outputs in the log.
I found this link from APPLE very informative and complete.
It pretty much gives you all the options to see or access logs of the device whether or not they are connected to your dev machine.
https://developer.apple.com/library/ios/qa/qa1747/_index.html
Yes, NSLog outputs on the device. You can see it's outputs with your device connected to your Mac and using Xcode Organizer tool.
If you use Testflight SDK, you can capture all logs with their Remote Logging feature.
I know this is an old thread but you can also have access to the device logs going to:
Settings -> Privacy -> Analytics -> Data
Hope this help
Regards
I think in Xcode 9.3 the device log screen has been moved to a new location.Kindly refer the following link.
Get device logs at runtime in Xcode

Resources