CAShapeLayer Class Creation - objective-c

I found a fantastic tutorial written in Swift for creating an animation when my app opens. It's written in Swift, so I'm Objective-C-ifying it for a project I'm working on, but I'm running into a little trouble adding my animation. Swift works, but I can't figure out what's wrong w/ my Objective-C version.
Creating a Complex Loading Animation in Swift
Swift Version of CAShapeLayer Class:
func expand() {
var expandAnimation: CABasicAnimation = CABasicAnimation(keyPath: "path")
expandAnimation.fromValue = ovalPathSmall.CGPath
expandAnimation.toValue = ovalPathLarge.CGPath
expandAnimation.duration = animationDuration
expandAnimation.fillMode = kCAFillModeForwards
expandAnimation.removedOnCompletion = false
// the following line I'm having trouble converting to Objective-C
addAnimation(expandAnimation, forKey: nil)
}
Objective-C Version:
- (void)expand {
CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:#"path"];
expandAnimation.fromValue = (__bridge id)(_ovalPathSmall.CGPath);
expandAnimation.toValue = (__bridge id)(_ovalPathSquishHorizontal.CGPath);
expandAnimation.duration = self.animationDuration;
expandAnimation.fillMode = kCAFillModeForwards;
expandAnimation.removedOnCompletion = NO;
// I can't figure out how to "convert" this to Objective-C
// addAnimation(expandAnimation, forKey: nil)
}
Initializers:
I suspect I'm mucking something up with the initializer, so here's the code for the working Swift one and my Objective-C-ified version below.
Swift Version:
let animationDuration: CFTimeInterval = 0.3
override init!() {
super.init()
fillColor = Colors.red.CGColor
path = ovalPathSmall.CGPath
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Objective-C Version:
This is my attempt at mimicking the initializer in my .m file:
- (instancetype)init
{
self = [super init];
if (self) {
self.animationDuration = 0.3;
self.fillColor = [UIColor redColor].CGColor;
self.path = self.ovalPathSmall.CGPath;
}
return self;
}
Thank you for reading and I welcome your input.

To answer your immediate question, I think you just need [self addAnimation...], but I don't know what you have tried.
To answer your comment though, at least at this point, it is pretty easy. See the mixing and matching section of: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_78.
The short version is that the Swift compiler runs first, and generates Obj-C stubs that the Obj-C compiler can understand in a single header file based on your project name. This file is generated in the build output (Derived Data etc). You need to import that file into your Ojb-C code, per the example in the above link.
#import "ProductModuleName-Swift.h"

Related

app broke after Xcode update

I have a Swift app, with some Objective-C code mixed in. It was working yesterday, but this morning I updated XCode and now everything had gone to hell!
Firstly, after updating, I clicked the XCode popup box to allow it to upgrade my app to Swift4. This is where the problems started.
I have a Swift class called RestClient with the following 4 functions (among others):
class func getInstance() -> RestClient {
if (gRestClient == nil) {
let prefs:UserDefaults = UserDefaults.standard
return RestClient(username: prefs.string(forKey: "username")!, password: prefs.string(forKey: "password")!)
}
return gRestClient!
}
class func getUsername() -> String {
if (gUsername == nil) {
let prefs:UserDefaults = UserDefaults.standard
gUsername = prefs.string(forKey: "username")!
}
return gUsername!
}
class func getPassword() -> String {
if (gPassword == nil) {
let prefs:UserDefaults = UserDefaults.standard
gPassword = prefs.string(forKey: "password")!
}
return gPassword!
}
public func getServer() -> String {
return MAIN_SERVER;
}
Then in my /Objective-C/ folder, I have some more files, once of which is called RestClientObj.m. In here, I have this lines of code:
NSString* url = [NSString stringWithFormat:#"%#/receipt/email/%#/%#/", [[RestClient getInstance] getServer], rrn, emailAddress];
NSString *authStr = [NSString stringWithFormat:#"%#:%#", [RestClient getUsername], [RestClient getPassword]];
So as you can see, I'm calling the RestClient.swift from here. The RestClientObj.h is as follows:
#ifndef RestClientObj_h
#define RestClientObj_h
#endif /* ResClientObj_h */
#interface RestClientObj : NSObject {
}
+(BOOL) sendSMS:(NSString *)rrn mn:(NSString *)mobileNumber;
+(BOOL) sendEmail:(NSString *)rrn mn:(NSString *)emailAddress;
#end
This whole upgrade is causing other problems too. I have another class with the following error:
No visible #interface for 'AppDelegate' declares the selector 'managedObjectContext'
on the line:
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext]; <-ERROR
Can anyone shed any light on this?
EDIT: Here's some code from the AppDelegate class:
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
Just to close this off.
The issue was missing the #obj identifier before the variable declaration to make it visible to my objective-c code, in combination with the XCode Swift Upgrade wizard renaming some functions.

Objective C static const in swift

How write this Objective C code in Swift?
#import "FFTViewController.h"
//only this part
static vDSP_Length const FFTViewControllerFFTWindowSize = 4096;
#implementation FFTViewController
My idea is
let vDSP: vDSP_Length = 4096
i'm sure that is not correct, but i have no idea.
thanks for help
Update:
I want port this Objective C code to swift
#import "FFTViewController.h"
static vDSP_Length const FFTViewControllerFFTWindowSize = 4096;
#implementation FFTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//
// Setup the AVAudioSession. EZMicrophone will not work properly on iOS
// if you don't do this!
//
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
if (error)
{
NSLog(#"Error setting up audio session category: %#", error.localizedDescription);
}
[session setActive:YES error:&error];
if (error)
{
NSLog(#"Error setting up audio session active: %#", error.localizedDescription);
}
//
// Setup time domain audio plot
//
self.audioPlotTime.plotType = EZPlotTypeBuffer;
self.maxFrequencyLabel.numberOfLines = 0;
//
// Setup frequency domain audio plot
//
self.audioPlotFreq.shouldFill = YES;
self.audioPlotFreq.plotType = EZPlotTypeBuffer;
self.audioPlotFreq.shouldCenterYAxis = NO;
//
// Create an instance of the microphone and tell it to use this view controller instance as the delegate
//
self.microphone = [EZMicrophone microphoneWithDelegate:self];
//
// Create an instance of the EZAudioFFTRolling to keep a history of the incoming audio data and calculate the FFT.
//
self.fft = [EZAudioFFTRolling fftWithWindowSize:FFTViewControllerFFTWindowSize
sampleRate:self.microphone.audioStreamBasicDescription.mSampleRate
delegate:self];
//
// Start the mic
//
[self.microphone startFetchingAudio];
}
This is my beginning
import UIKit
import Accelerate
class ViewController: UIViewController, EZMicrophoneDelegate{
var mic: EZMicrophone!
var fft: EZAudioFFTRolling!
private let FFTViewControllerFFTWindowSize: vDSP_Length = 4096
override func loadView() {
super.loadView()
mic = EZMicrophone(delegate: self, startsImmediately: true)
fft = EZAudioFFTRolling(windowSize: FFTViewControllerFFTWindowSize, sampleRate: mic.audioStreamBasicDescription().mSampleRate, delegate: self)
}
}
I becoming the following error:
compile fails with the following error:
"Cannot invoke initializer for type 'EZAudioFFTRolling' with an argument list of type '(windowSize: vDSP_Length, sampleRate: Float64, delegate: ViewController)'"
How is my problem?
Sorry for this question i have no ObjectiveC background, is little challenge this port for me.
Thanks for Help
The problem you're getting an error is in types. When you start typing EZAudioFFTRolling initializer in XCode, autocompletion helps you by displaying specification. It looks like this:
EZAudioFFTRolling(windowSize: vDSP_Length, sampleRate: Float, delegate: EZAudioFFTDelegate!)
If you compare parameters types from specification with actual values you're passing, you will figure out that mic.audioStreamBasicDescription().mSampleRate has Float64 type but should be Float (they're actually not the same) and self is not conforming to EZAudioFFTDelegate protocol. So after changing your line to this
fft = EZAudioFFTRolling(windowSize: FFTViewControllerFFTWindowSize, sampleRate: Float(mic.audioStreamBasicDescription().mSampleRate), delegate: self)
and adding protocol conformance you will be able to build your code.
Swift is type safety in contradistinction to Objective-C, so it's common issues that all Objective-C developers have when start working with Swift. But errors are actually helping you, so read them attentively.

WKWebKit Mac OS NSPrintOperation

WebKit 1 exposed the WebFrameView where I could call a print operation.
- (void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView {
NSPrintOperation *printOperation = [frameView printOperationWithPrintInfo:[NSPrintInfo sharedPrintInfo]];
[printOperation runOperation];
}
With WKWebKit API I can't seem to figure out how to perform a similar action or which view to grab for printing. All my efforts have come up with blank pages.
macOS 11 update: the printOperation(with:) method is no longer private and is now the solution that I use in my apps.
Amazingly, WKWebView still doesn't support printing on macOS, despite the legacy WebView being deprecated.
Looking at https://github.com/WebKit/webkit/commit/0dfc67a174b79a8a401cf6f60c02150ba27334e5 , printing was implemented years ago as a private API, but for some reason, has not been exposed. If you don't mind using a private API, you can print a WKWebView with:
public extension WKWebView {
// standard printing doesn't work for WKWebView; see http://www.openradar.me/23649229 and https://bugs.webkit.org/show_bug.cgi?id=151276
#available(OSX, deprecated: 10.16, message: "WKWebView printing will hopefully get fixed someday – maybe in 10.16?")
private static let webViewPrintSelector = Selector(("_printOperationWithPrintInfo:")) // https://github.com/WebKit/webkit/commit/0dfc67a174b79a8a401cf6f60c02150ba27334e5
func webViewPrintOperation(withSettings printSettings: [NSPrintInfo.AttributeKey : Any]) -> NSPrintOperation? {
guard self.responds(to: Self.webViewPrintSelector) else {
return nil
}
guard let po: NSPrintOperation = self.perform(Self.webViewPrintSelector, with: NSPrintInfo(dictionary: printSettings))?.takeUnretainedValue() as? NSPrintOperation else {
return nil
}
// without initializing the print view's frame we get the breakpoint at AppKitBreakInDebugger:
// ERROR: The NSPrintOperation view's frame was not initialized properly before knowsPageRange: returned. This will fail in a future release! (WKPrintingView)
po.view?.frame = self.bounds
return po
}
}
You can have this be the default print operation for your NSDocument subclass by adding:
override func printOperation(withSettings printSettings: [NSPrintInfo.AttributeKey : Any]) throws -> NSPrintOperation {
return myWebView.webViewPrintOperation(withSettings: printSettings) ?? try super.printOperation(withSettings: printSettings)
}
Here is marcprux swift solution in Objective C:
SEL printSelector = NSSelectorFromString(#"_printOperationWithPrintInfo:");
if ([self.webView respondsToSelector:printSelector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
NSPrintOperation *printOperation = (NSPrintOperation*) [self.webView performSelector:printSelector withObject:[NSPrintInfo sharedPrintInfo]];
#pragma clang diagnostic pop
return printOperation;
}
Starting on macOS 11 this is not private anymore: printOperation(with:)
Usage:
let info = NSPrintInfo.shared
// configure info...
let operation = webView.printOperation(with: info)

glCreateProgram causes segfault?

I'm trying to write a Wavefront OBJ file viewer in Objective-C, that is capable of loading meshes/materials/shaders from files. I've created classes for shaders and shader programs, and I'm trying to create an OpenGL shader program object as part of my shader program class's init method:
- (id)initWithVertexShader:(NSString *)vshader FragmentShader:(NSString *)fshader {
self = [super init];
if (self) {
SRShader* shaders[2] = {
[[SRShader alloc] initWithFilename:vshader Type:GL_VERTEX_SHADER Source:nil],
[[SRShader alloc] initWithFilename:fshader Type:GL_FRAGMENT_SHADER Source:nil]
};
program = glCreateProgram();
for (int i = 0; i < 2; i++) {
SRShader* s = shaders[i];
NSError* e = nil;
s.source = [NSString stringWithContentsOfFile:s.filename encoding:NSUTF8StringEncoding error:&e];
if (!e) {
NSLog(#"Failed to read shader file: %#\n", s.filename);
exit(-1);
}
GLuint shader = [s compile];
... and so on.
However, calling glCreateProgram results in EXC_BAD_ACCESS, as does the call to [SRShader compile], which in turn calls glCreateShader. Does anyone know of any issues with these function calls in Objective-C? Maybe something to do with ARC or calling them in an initialization function?
If you're using an NSOpenGLView, you will need to add the following lines of code before you ever call glCreateProgram(), so probably in -(id)initWithCoder if you load the view from Interface Builder:
NSOpenGLContext* context = [self openGLContext];
[context makeCurrentContext];
Very basic, and I know this thread is dated, but maybe it will save someone a minute or two when they Google and find this thread (as I did just now).
As Trevor Answered in another post you should initialize the glew.
glewInit();
source of the answer:
https://gamedev.stackexchange.com/a/22788

How BarAlarm iOS app is made? [duplicate]

I am developing a non-appstore app for iOS. I want to read the cellular signal strength in my code.
I know Apple doesn't provide any API by which we can achieve this.
Is there any private API that can be used to achieve this? I have gone through the various threads regarding this issue but could not find any relevant info.
It is completely possible because there is an app in the app-store for detecting the carrier's signal strength.
Get signalStreght IOS9:
UIApplication *app = [UIApplication sharedApplication];
NSArray *subviews = [[[app valueForKey:#"statusBar"] valueForKey:#"foregroundView"] subviews];
NSString *dataNetworkItemView = nil;
for (id subview in subviews) {
if([subview isKindOfClass:[NSClassFromString(#"UIStatusBarSignalStrengthItemView") class]])
{
dataNetworkItemView = subview;
break;
}
}
int signalStrength = [[dataNetworkItemView valueForKey:#"signalStrengthRaw"] intValue];
NSLog(#"signal %d", signalStrength);
It is not very hard.
Link CoreTelephony.framework in your Xcode project
Add the following lines where you need it
Code:
int CTGetSignalStrength(); // private method (not in the header) of Core Telephony
- (void)aScanMethod
{
NSLog(#"%d", CTGetSignalStrength()); // or do what you want
}
And you are done.
Update May 2016
Apple removed this opportunity.
I briefly looked at the VAFieldTest project located at Github.
There seems to be getSignalStrength() and register_notification() functions in Classes/VAFieldTestViewController.m that might be interesting to you as they call into CoreTelephony.framework.
I am pretty confident that some of the used calls are undocumented in the CoreTelephony framework documentation from Apple and therefore private - any app in the AppStore must have slipped passed inspection.
To get signal streght in iOS 9 or above in Swift 3, without using the private API from CoreTelephony - CTGetSignalStrength(). Just scouring the statusBar view.
func getSignalStrength() -> Int {
let application = UIApplication.shared
let statusBarView = application.value(forKey: "statusBar") as! UIView
let foregroundView = statusBarView.value(forKey: "foregroundView") as! UIView
let foregroundViewSubviews = foregroundView.subviews
var dataNetworkItemView:UIView!
for subview in foregroundViewSubviews {
if subview.isKind(of: NSClassFromString("UIStatusBarSignalStrengthItemView")!) {
dataNetworkItemView = subview
break
} else {
return 0 //NO SERVICE
}
}
return dataNetworkItemView.value(forKey: "signalStrengthBars") as! Int
}
Attention: If the status bar is hidden, the key "statusBar" will return nil.
I haven't tested it, but apparently this is now a method of CTTelephonyNetworkInfo instead of a global/static function.
The return type is id, so I think you get either a NSDictionary (as the _cachedSignalStrength ivar implies) or an NSNumber (as the old function implies).
id signalStrength = [[CTTelephonyNetworkInfo new] signalStrength];
This changed in iOS 8.3, as you can see from the commit.
Note that this is still not documented! So if your app will go in the App Store, take your precautions.
Here's Lucas' answer converted to Xamarin, and tested on iOS 10.2.1:
var application = UIApplication.SharedApplication;
var statusBarView = application.ValueForKey(new NSString("statusBar")) as UIView;
var foregroundView = statusBarView.ValueForKey(new NSString("foregroundView")) as UIView;
UIView dataNetworkItemView = null;
foreach (UIView subview in foregroundView.Subviews)
{
if ("UIStatusBarSignalStrengthItemView" == subview.Class.Name)
{
dataNetworkItemView = subview;
break;
}
}
if (null == dataNetworkItemView)
return false; //NO SERVICE
int bars = ((NSNumber)dataNetworkItemView.ValueForKey(new NSString("signalStrengthBars"))).Int32Value;