Mac OS X Simple Voice Recorder - xcode

Does anyone have some sample code for a SIMPLE voice recorder for Mac OS X? I would just like to record my voice coming from the internal microphone on my MacBook Pro and save it to a file. That is all.
I have been searching for hours and yes, there are some examples that will record voice and save it to a file such as http://developer.apple.com/library/mac/#samplecode/MYRecorder/Introduction/Intro.html . The sample code for Mac OS X seems to be about 10 times more complicated than similar sample code for the iPhone.
For iOS the commands are as simple as:
soundFile =[NSURL FileURLWithPath:[tempDir stringByAppendingString:#"mysound.cap"]];
soundSetting = [NSDictionary dictionaryWithObjectsAndKeys: // dictionary setting code left out goes here
soundRecorder = [[AVAudioRecorder alloc] initWithURL:soundFile settings:soundSetting error:nil];
[soundRecorder record];
[soundRecorder stop];
I think there is code to do this for the Mac OS X that would be as simple as the iPhone version. Thank you for your help.
Here is the code (currently the player will not work)
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#interface MyAVFoundationClass : NSObject <AVAudioPlayerDelegate>
{
AVAudioRecorder *soundRecorder;
}
#property (retain) AVAudioRecorder *soundRecorder;
-(IBAction)stopAudio:(id)sender;
-(IBAction)recordAudio:(id)sender;
-(IBAction)playAudio:(id)sender;
#end
#import "MyAVFoundationClass.h"
#implementation MyAVFoundationClass
#synthesize soundRecorder;
-(void)awakeFromNib
{
NSLog(#"awakeFromNib visited");
NSString *tempDir;
NSURL *soundFile;
NSDictionary *soundSetting;
tempDir = #"/Users/broncotrojan/Documents/testvoices/";
soundFile = [NSURL fileURLWithPath: [tempDir stringByAppendingString:#"test1.caf"]];
NSLog(#"soundFile: %#",soundFile);
soundSetting = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatMPEG4AAC],AVFormatIDKey,
[NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityHigh],AVEncoderAudioQualityKey, nil];
soundRecorder = [[AVAudioRecorder alloc] initWithURL: soundFile settings: soundSetting error: nil];
}
-(IBAction)stopAudio:(id)sender
{
NSLog(#"stopAudioVisited");
[soundRecorder stop];
}
-(IBAction)recordAudio:(id)sender
{
NSLog(#"recordAudio Visited");
[soundRecorder record];
}
-(IBAction)playAudio:(id)sender
{
NSLog(#"playAudio Visited");
NSURL *soundFile;
NSString *tempDir;
AVAudioPlayer *audioPlayer;
tempDir = #"/Users/broncotrojan/Documents/testvoices/";
soundFile = [NSURL fileURLWithPath: [tempDir stringByAppendingString:#"test1.caf"]];
NSLog(#"soundFile: %#", soundFile);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
[audioPlayer setDelegate:self];
[audioPlayer play];
}
#end

Here is the code that is working for me on macOS 10.14 with Xcode 10.2.1, Swift 5.0.1.
First of all you have to set up NSMicrophoneUsageDescription aka Privacy - Microphone Usage Description in your Info.plist file as described in the Apple docs: Requesting Authorization for Media Capture on macOS.
Then you have to request a permission from a user to use a microphone:
switch AVCaptureDevice.authorizationStatus(for: .audio) {
case .authorized: // The user has previously granted access to the camera.
// proceed with recording
case .notDetermined: // The user has not yet been asked for camera access.
AVCaptureDevice.requestAccess(for: .audio) { granted in
if granted {
// proceed with recording
}
}
case .denied: // The user has previously denied access.
()
case .restricted: // The user can't grant access due to restrictions.
()
#unknown default:
fatalError()
}
Then you can use the following methods to start and stop audio recording:
import AVFoundation
open class SpeechRecorder: NSObject {
private var destinationUrl: URL!
var recorder: AVAudioRecorder?
let player = AVQueuePlayer()
open func start() {
destinationUrl = createUniqueOutputURL()
do {
let format = AVAudioFormat(settings: [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey: AVAudioQuality.high,
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 1,
AVLinearPCMBitDepthKey: 16,
])!
let recorder = try AVAudioRecorder(url: destinationUrl, format: format)
// workaround against Swift, AVAudioRecorder: Error 317: ca_debug_string: inPropertyData == NULL issue
// https://stackoverflow.com/a/57670740/598057
let firstSuccess = recorder.record()
if firstSuccess == false || recorder.isRecording == false {
recorder.record()
}
assert(recorder.isRecording)
self.recorder = recorder
} catch let error {
let code = (error as NSError).code
NSLog("SpeechRecorder: \(error)")
NSLog("SpeechRecorder: \(code)")
let osCode = OSStatus(code)
NSLog("SpeechRecorder: \(String(describing: osCode.detailedErrorMessage()))")
}
}
open func stop() {
NSLog("SpeechRecorder: stop()")
if let recorder = recorder {
recorder.stop()
NSLog("SpeechRecorder: final file \(destinationUrl.absoluteString)")
player.removeAllItems()
player.insert(AVPlayerItem(url: destinationUrl), after: nil)
player.play()
}
}
func createUniqueOutputURL() -> URL {
let paths = FileManager.default.urls(for: .musicDirectory,
in: .userDomainMask)
let documentsDirectory = URL(fileURLWithPath: NSTemporaryDirectory())
let currentTime = Int(Date().timeIntervalSince1970 * 1000)
let outputURL = URL(fileURLWithPath: "SpeechRecorder-\(currentTime).m4a",
relativeTo: documentsDirectory)
destinationUrl = outputURL
return outputURL
}
}
extension OSStatus {
//**************************
func asString() -> String? {
let n = UInt32(bitPattern: self.littleEndian)
guard let n1 = UnicodeScalar((n >> 24) & 255), n1.isASCII else { return nil }
guard let n2 = UnicodeScalar((n >> 16) & 255), n2.isASCII else { return nil }
guard let n3 = UnicodeScalar((n >> 8) & 255), n3.isASCII else { return nil }
guard let n4 = UnicodeScalar( n & 255), n4.isASCII else { return nil }
return String(n1) + String(n2) + String(n3) + String(n4)
} // asString
//**************************
func detailedErrorMessage() -> String {
switch(self) {
case 0:
return "Success"
// AVAudioRecorder errors
case kAudioFileUnspecifiedError:
return "kAudioFileUnspecifiedError"
case kAudioFileUnsupportedFileTypeError:
return "kAudioFileUnsupportedFileTypeError"
case kAudioFileUnsupportedDataFormatError:
return "kAudioFileUnsupportedDataFormatError"
case kAudioFileUnsupportedPropertyError:
return "kAudioFileUnsupportedPropertyError"
case kAudioFileBadPropertySizeError:
return "kAudioFileBadPropertySizeError"
case kAudioFilePermissionsError:
return "kAudioFilePermissionsError"
case kAudioFileNotOptimizedError:
return "kAudioFileNotOptimizedError"
case kAudioFileInvalidChunkError:
return "kAudioFileInvalidChunkError"
case kAudioFileDoesNotAllow64BitDataSizeError:
return "kAudioFileDoesNotAllow64BitDataSizeError"
case kAudioFileInvalidPacketOffsetError:
return "kAudioFileInvalidPacketOffsetError"
case kAudioFileInvalidFileError:
return "kAudioFileInvalidFileError"
case kAudioFileOperationNotSupportedError:
return "kAudioFileOperationNotSupportedError"
case kAudioFileNotOpenError:
return "kAudioFileNotOpenError"
case kAudioFileEndOfFileError:
return "kAudioFileEndOfFileError"
case kAudioFilePositionError:
return "kAudioFilePositionError"
case kAudioFileFileNotFoundError:
return "kAudioFileFileNotFoundError"
//***** AUGraph errors
case kAUGraphErr_NodeNotFound: return "AUGraph Node Not Found"
case kAUGraphErr_InvalidConnection: return "AUGraph Invalid Connection"
case kAUGraphErr_OutputNodeErr: return "AUGraph Output Node Error"
case kAUGraphErr_CannotDoInCurrentContext: return "AUGraph Cannot Do In Current Context"
case kAUGraphErr_InvalidAudioUnit: return "AUGraph Invalid Audio Unit"
//***** MIDI errors
case kMIDIInvalidClient: return "MIDI Invalid Client"
case kMIDIInvalidPort: return "MIDI Invalid Port"
case kMIDIWrongEndpointType: return "MIDI Wrong Endpoint Type"
case kMIDINoConnection: return "MIDI No Connection"
case kMIDIUnknownEndpoint: return "MIDI Unknown Endpoint"
case kMIDIUnknownProperty: return "MIDI Unknown Property"
case kMIDIWrongPropertyType: return "MIDI Wrong Property Type"
case kMIDINoCurrentSetup: return "MIDI No Current Setup"
case kMIDIMessageSendErr: return "MIDI Message Send Error"
case kMIDIServerStartErr: return "MIDI Server Start Error"
case kMIDISetupFormatErr: return "MIDI Setup Format Error"
case kMIDIWrongThread: return "MIDI Wrong Thread"
case kMIDIObjectNotFound: return "MIDI Object Not Found"
case kMIDIIDNotUnique: return "MIDI ID Not Unique"
case kMIDINotPermitted: return "MIDI Not Permitted"
//***** AudioToolbox errors
case kAudioToolboxErr_CannotDoInCurrentContext: return "AudioToolbox Cannot Do In Current Context"
case kAudioToolboxErr_EndOfTrack: return "AudioToolbox End Of Track"
case kAudioToolboxErr_IllegalTrackDestination: return "AudioToolbox Illegal Track Destination"
case kAudioToolboxErr_InvalidEventType: return "AudioToolbox Invalid Event Type"
case kAudioToolboxErr_InvalidPlayerState: return "AudioToolbox Invalid Player State"
case kAudioToolboxErr_InvalidSequenceType: return "AudioToolbox Invalid Sequence Type"
case kAudioToolboxErr_NoSequence: return "AudioToolbox No Sequence"
case kAudioToolboxErr_StartOfTrack: return "AudioToolbox Start Of Track"
case kAudioToolboxErr_TrackIndexError: return "AudioToolbox Track Index Error"
case kAudioToolboxErr_TrackNotFound: return "AudioToolbox Track Not Found"
case kAudioToolboxError_NoTrackDestination: return "AudioToolbox No Track Destination"
//***** AudioUnit errors
case kAudioUnitErr_CannotDoInCurrentContext: return "AudioUnit Cannot Do In Current Context"
case kAudioUnitErr_FailedInitialization: return "AudioUnit Failed Initialization"
case kAudioUnitErr_FileNotSpecified: return "AudioUnit File Not Specified"
case kAudioUnitErr_FormatNotSupported: return "AudioUnit Format Not Supported"
case kAudioUnitErr_IllegalInstrument: return "AudioUnit Illegal Instrument"
case kAudioUnitErr_Initialized: return "AudioUnit Initialized"
case kAudioUnitErr_InvalidElement: return "AudioUnit Invalid Element"
case kAudioUnitErr_InvalidFile: return "AudioUnit Invalid File"
case kAudioUnitErr_InvalidOfflineRender: return "AudioUnit Invalid Offline Render"
case kAudioUnitErr_InvalidParameter: return "AudioUnit Invalid Parameter"
case kAudioUnitErr_InvalidProperty: return "AudioUnit Invalid Property"
case kAudioUnitErr_InvalidPropertyValue: return "AudioUnit Invalid Property Value"
case kAudioUnitErr_InvalidScope: return "AudioUnit InvalidScope"
case kAudioUnitErr_InstrumentTypeNotFound: return "AudioUnit Instrument Type Not Found"
case kAudioUnitErr_NoConnection: return "AudioUnit No Connection"
case kAudioUnitErr_PropertyNotInUse: return "AudioUnit Property Not In Use"
case kAudioUnitErr_PropertyNotWritable: return "AudioUnit Property Not Writable"
case kAudioUnitErr_TooManyFramesToProcess: return "AudioUnit Too Many Frames To Process"
case kAudioUnitErr_Unauthorized: return "AudioUnit Unauthorized"
case kAudioUnitErr_Uninitialized: return "AudioUnit Uninitialized"
case kAudioUnitErr_UnknownFileType: return "AudioUnit Unknown File Type"
case kAudioUnitErr_RenderTimeout: return "AudioUnit Rendre Timeout"
//***** Audio errors
case kAudio_BadFilePathError: return "Audio Bad File Path Error"
case kAudio_FileNotFoundError: return "Audio File Not Found Error"
case kAudio_FilePermissionError: return "Audio File Permission Error"
case kAudio_MemFullError: return "Audio Mem Full Error"
case kAudio_ParamError: return "Audio Param Error"
case kAudio_TooManyFilesOpenError: return "Audio Too Many Files Open Error"
case kAudio_UnimplementedError: return "Audio Unimplemented Error"
default: return "Unknown error (no description)"
}
}
}
The workaround for the inPropertyData == NULL issue is adapted from Swift, AVAudioRecorder: Error 317: ca_debug_string: inPropertyData == NULL.
The code that provides string messages for the OSStatus codes is adapted from here: How do you convert an iPhone OSStatus code to something useful?.

The AVFoundation framework is new in Lion and is very similar to the iOS version. That includes AVAudioRecorder. You can use the code from iOS with little or no modification.
Docs are here.

The reason that your code does not play the audio is audioPlayer variable is immediately released as soon as it reaches the end of the method block.
So move the following variable to the outside of the method block, then it will play the audio well.
AVAudioPlayer *audioPlayer;
By the way, your code snippet was very helpful for me! :D

Here is the snippet for Mac:
NSDictionary *soundSetting = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatMPEG4AAC],AVFormatIDKey,
[NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityHigh],AVEncoderAudioQualityKey, nil];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSURL* audioFileURL = [NSURL fileURLWithPath: [documentsDirectory stringByAppendingString:#"/test.wav"]];
NSError* error;
AVAudioRecorder* soundRecorder = soundRecorder = [[AVAudioRecorder alloc] initWithURL: audioFileURL settings: soundSetting error: &error];
if (error)
{
NSLog(#"Error! soundRecorder initialization failed...");
}
// start recording
[soundRecorder record];

Related

Parse app crashes after pressing "OK" on Push Notification Alert

I don't have any code handling or creating a new UIAlertView in my didReceiveRemoteNotification method. Simply..
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Active {
print("active")
}
When I press "OK" on the alert, the app crashes with exec_bad_access and the stack is stuck on the completion(NSNotFound) line below.
if ([UIAlertController class] != nil) {
__block UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
void (^alertActionHandler)(UIAlertAction *) = [^(UIAlertAction *action){
// This block intentionally retains alertController, and releases it afterwards.
if (action.style == UIAlertActionStyleCancel) {
completion(NSNotFound);
} else {
NSUInteger index = [alertController.actions indexOfObject:action];
completion(index - 1);
}
alertController = nil;
} copy];

OSX Dropbox Sync API Error - unable to verify link request

Coded in Swift I implemented after the Tutorial.
DBAccountManager is setup in AppDelegate on applicationDidFinishLaunching.
Later, when the user activates dropbox support in my application I'm trying to link the account.
The Window Panel is displayed and my application is waiting for the callback.
Sometimes I do not get a linked account, even the user logs in and accepts.
The Log says
"[ERROR] unable to verify link request"
When this occurs on a machine it wont't work, you can retry and retry...
if it worked, it works like a charm and in future I always get the linked account directly from the library without the login window.
What does this error mean and what can I do?
AppDelegate:
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Dropbox API Key & Secret
let _appKey = "------"
let _appSecret = "------"
// Accountmanager
if (DBAccountManager.sharedManager() == nil)
{
let accountManager = DBAccountManager(appKey: _appKey, secret: _appSecret)
DBAccountManager.setSharedManager(accountManager)
}
....
}
The linking in my class, when user clicked to activate dropbox:
internal func __start(parentWindow:NSWindow?, callback:((Bool) -> Void))
{
let am = DBAccountManager.sharedManager()
if am == nil
{
NSLog("Dropbox not available!")
callback!(false)
return
}
// link account
let linkedAccount = am!.linkedAccount
if (linkedAccount != nil)
{
// Already linked
DLog("Dropbox link found.")
let fileSystem = DBFilesystem(account: linkedAccount!)
DBFilesystem.setSharedFilesystem(fileSystem)
callback(true)
}
else
{
// link with window must be in mainthread
dispatch_async(dispatch_get_main_queue())
{
am!.linkFromWindow(parentWindow) {
account in
if (account != nil)
{
DLog("Dropbox linked")
let fileSystem = DBFilesystem(account: account!)
DBFilesystem.setSharedFilesystem(fileSystem)
callback(true)
}
else
{
DLog("NOT LINKED (Dropbox)")
callback(false)
} // if - else account
} // accountmanager block
} // dispatchblock main
} // if - else linkedaccount
}
Here the full log, the app is not doing anything else:
2015-02-23 10:25:39.443 TestApp[39226:30958267] Dropbox init
<<<< MediaValidator >>>> mv_ValidateRFC4281CodecId: Unrecognized codec 1.(null). Failed codec specific check.
<<<< MediaValidator >>>> mv_LookupCodecSupport: Unrecognized codec 1
[10:25:40.979] mv_LowLevelCheckIfVideoPlayableUsingDecoder signalled err=-12956 (kFigMediaValidatorError_VideoCodecNotSupported) (video codec 1) at line 1851
<<<< MediaValidator >>>> mv_TestCodecSupportUsingDecoders: Unrecognized codec 1
<<<< MediaValidator >>>> mv_ValidateRFC4281CodecId: Unrecognized codec 1.(null). Failed codec specific check.
<<<< MediaValidator >>>> mv_LookupCodecSupport: Unrecognized codec 1
[10:25:40.979] mv_LowLevelCheckIfVideoPlayableUsingDecoder signalled err=-12956 (kFigMediaValidatorError_VideoCodecNotSupported) (video codec 1) at line 1851
<<<< MediaValidator >>>> mv_TestCodecSupportUsingDecoders: Unrecognized codec 1
2015-02-23 10:25:43.873 TestApp[39226:30958267] [ERROR] unable to verify link request
2015-02-23 10:25:43.879 TestApp[39226:30958267] NOT LINKED (Dropbox)
I have same issue [ERROR] unable to verify link request after long research and studying the DropBoxSDK I come to the point that this error occurs when state ID is different from value saved at key KDBKLinkNonce. Every time at new Session it generates new state ID. See below code of [[DBSession sharedSession] handleOpenURL:url] method.
- (BOOL)handleOpenURL:(NSURL *)url {
NSString *expected = [NSString stringWithFormat:#"%#://%#/", [self appScheme], kDBDropboxAPIVersion];
if (![[url absoluteString] hasPrefix:expected]) {
return NO;
}
NSArray *components = [[url path] pathComponents];
NSString *methodName = [components count] > 1 ? [components objectAtIndex:1] : nil;
if ([methodName isEqual:#"connect"]) {
NSDictionary *params = [DBSession parseURLParams:[url query]];
NSString *token = [params objectForKey:#"oauth_token"];
NSString *secret = [params objectForKey:#"oauth_token_secret"];
NSString *userId = [params objectForKey:#"uid"];
NSString *state = [params objectForKey:#"state"];
NSString *nonce = [[NSUserDefaults standardUserDefaults] objectForKey:kDBLinkNonce];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kDBLinkNonce];
[[NSUserDefaults standardUserDefaults] synchronize];
if (![nonce isEqual:state]) {
DBLogError(#"unable to verify link request");
return NO;
}
[self updateAccessToken:token accessTokenSecret:secret forUserId:userId];
} else if ([methodName isEqual:#"cancel"]) {
DBLogInfo(#"DropboxSDK: user cancelled Dropbox link");
}
return YES; }
For further reference please check this link dropbox-sdk-ios
I found something and since I found a fix for that, even the dropbox error is gone.
The problem seems to be, that NSUserDefaults did not store any data (not in memory and not on disk!). Since Dropbox uses NSUserDefaults to check the state before and after, this killed the whole process.
With getting the NSUserDefaults back working it seems the whole dropbox problem was gone.
OSX NSUserDefaults not Working

Twilio iOS unrecognized selector sent to Class

I'm new to using Twilio and I'm hoping someone can help me debug my app.
I am making a call to get a capability token and it is returned just fine. I print it in the console to check then it appears when I make the call to initWithCapabilityToken that's where something is breaking and I cant figure it out.
Here is my code...
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error){
// Log Any Reply
if ([data length] >0 && error == nil) {
NSData *jsonData = data;
// Deserialize JSON into Dictionary
error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:&error ];
if (jsonObject != nil && error == nil) {
NSLog(#"Successfully deserialized JSON response...");
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(#"Deserialized JSON Dictionary = %#", deserializedDictionary);
NSString *token = [deserializedDictionary objectForKey:#"token"];
if (token == nil) {
NSLog(#"Error retrieving token");
} else {
NSLog(#"Token: %#", token);
// Setup TCDevice
_phone = [[TCDevice alloc] initWithCapabilityToken:token delegate:self];
}
}
} else if (error != nil){
NSLog(#"An error happened while de-serializing the JSON data.");
}
}else if ([data length] == 0 && error == nil){
NSLog(#"Nothing was downloaded.");
}else if (error != nil){
NSLog(#"Error happened = %#", error);
}
}];
Here is what I get right after my token is logged...
2015-01-29 16:32:14.637 AppName[4649:701822] +[NSString stringWithPJStr:]: unrecognized selector sent to class 0x3377da98
2015-01-29 16:32:14.639 AppName[4649:701822] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSString stringWithPJStr:]: unrecognized selector sent to class 0x3377da98'
*** First throw call stack:
(0x254da49f 0x32c90c8b 0x254df7d5 0x254dd7d7 0x2540f058 0x10ee7d 0x10e32b 0x10e2b7 0x105959 0x10568d 0x7b3ab 0x24fa228d 0x261d52b1 0x2614034d 0x26132b07 0x261d7c1b 0x487e29 0x4822c9 0x489567 0x48a891 0x33351e31 0x33351b84)
libc++abi.dylib: terminating with uncaught exception of type NSException
Thanks
After reading all of Twilios documentation I discovered I was missing one thing. I had to add -ObjC to Other Linker Flags and that solved my problem.
add this lines to the other linker flags:
-ObjC
-lTwilioClient
-lcrypto
-lssl
it worked for me only with a upper C add the end (-ObjC)
Somewhere you are calling a stringWithPJStr function(selector) that doesn't exist.

Error with 'getProfilePicture'

I have a profile view controller where the user can set or change his or her profile picture but am getting constant errors that do not make sense to me.
-(void)getProfilePicture
{
PFUser *user = [PFUser currentUser];
NSLog(#"file--%#",[user objectForKey:PF_USER_PICTURE]);
userName = user[PF_USER_USERNAME];
status = user[PF_USER_STATUS];
if ([user objectForKey:PF_USER_PICTURE]) {
[imageUser setFile:[user objectForKey:PF_USER_PICTURE]];
[imageUser loadInBackground];
}
else{
imageUser.image = [UIImage imageNamed:#"blank_profile#2x.png"];
}
// fieldName.text = user[PF_USER_FULLNAME];
}
#pragma mark - UIActionSheetDelegate
I receive the following errors portrayed in my ProfileViewController.m (I can provide/add .h if needed):
"No visible #interface for 'UIImageView' declares the selector 'setFile:'
"No visible #interface for 'UIImageView' declares the selector 'loadInBackground'
Any help would be much appreciated or any supporting code thats needed, thanks.
Try this and see what it does for you:
-(void)getProfilePicture
{
PFUser *user = [PFUser currentUser];
NSLog(#"file--%#",[user objectForKey:PF_USER_PICTURE]);
userName = user[PF_USER_USERNAME];
status = user[PF_USER_STATUS];
if ([user objectForKey:PF_USER_PICTURE]) {
userImage.file = [user objectForKey:PF_USER_PICTURE];
[userImage loadInBackground:^(UIImage *image, NSError *error) {
if (!error) {
imageUser.image = image;
[imageUser setNeedsDisplay];
} else {
NSLog(#"%#", error);
}
}];
}
else{
imageUser.image = [UIImage imageNamed:#"blank_profile#2x.png"];
}
// fieldName.text = user[PF_USER_FULLNAME];
}
#pragma mark - UIActionSheetDelegate
This is assuming your userImage is a PFImageView and [user objectForKey:PF_USER_PICTURE] is a PFFile.
EDIT
Actually, looking at your error it seems that userImage is just a UIImageView not a PFImageView, which might be the source of all your problems. Try making the userImage a PFImageView

Clang Error on "Potential null dereference."

I keep getting Clang errors on the following type of code and I can't figure out why they're erroneous or how to resolve them to Clang's satisfaction:
+ (NSString *)checkForLength: (NSString *)theString error: (NSError **)error {
BOOL hasLength = ([theString length] > 0);
if (hasLength) return theString;
else {
*error = [NSError errorWithDomain:#"ErrorDomain" code:hasLength userInfo:nil];
return nil;
}
}
Leaving aside the utterly-contrived nature of the example (which Clang did object to so it's illustrative enough), Clang balks at the error assignment line with the following objection:
Potential null dereference. According to coding standards in 'Creating and Returning NSError Objects' the parameter 'error' may be null.
I like having a pristine Clang report. I've read the cited document and I can't see a way to do what's expected; I checked some open-source Cocoa libraries and this seems to be a common idiom. Any ideas?
The way to do what's expected is shown in listing 3-5 in that document. With your example code:
+ (NSString *)checkForLength: (NSString *)theString error: (NSError **)error {
BOOL hasLength = ([theString length] > 0);
if (hasLength) return theString;
else {
if (error != NULL) *error = [NSError errorWithDomain:#"ErrorDomain" code:hasLength userInfo:nil];
return nil;
}
}
The Cocoa convention is that the return value should indicate success or failure (in this case, you return nil for failure) and the error is filled in with additional information, but only when the caller requests it.
In other words
NSError *error = nil;
NSString *result = [self checkForLength: aString error: &error];
and
NSString *result = [self checkForLength: aString error: NULL];
are both valid ways to invoke the method. So the method body should always check for a NULL error param:
if (error != NULL)
*error = ...;

Resources