activating function in swift file from an objective-C file - ios

I'm working on an app (in Xcode with storyboard and written in swift) but for a Reward ad (from AdMob) I had to use an Objective-C file to handle that certain view controller in objective-C. As a reward for clicking the ad I wanted to give the person coins in the app but the label which displays the coins is in another view controller in swift (both linked with a navigation controller).
After searching the internet I couldn't find a solution, but here is what I have already:
In the swift file for rewarding the user (named "RewardingUserAds.swift"):
import Foundation
#objc class RewardingUserAds: NSObject {
#objc func call_increase_score() {
print("Rewarding user ads is called.")
Winkel_ViewController().increase_score()
}
}
I found on internet you should use NSObject and add '#objc' that's why I have it there now, this calls a function in 'Winkel_Viewcontroller' which increases the variable of the coin.
A snippet from my objective-C file ('Ad_ViewController.m') ( I have not changed the 'Ad_ViewController.h'file) which gives the error:
#import "Ad_ViewController.h"
#import "Troeven-Bridging-Header.h"
#import "Troeven-Swift.h"
#import GoogleMobileAds;
#interface Ad_ViewController ()
#property(nonatomic, strong) GADRewardedAd *rewardedAd;
#end
#implementation Ad_ViewController
...
-(void) rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward {
RewardingUserAds *obj = [RewardingUserAds alloc];
[obj call_increase_score];
NSLog(#"User earned reward!");
}
The problem occurs in the second to last line ([obj call_increase_score] -> with ‘call_increase_score’ the function in the swift file it should activate/call). The error (red) is labeled as: 'No visible #interface for 'RewardingUserAds' declares the selector 'call_increase_score'' and that whole line stays white collored
I have a bridgehead between them named : 'Troeven-Bridging-Header.h' which is empty. I got little to no experience in objective-C which makes it harder for me to find the mistake I make. Can someone help?

I found the solution:
had to remove '#import "Troeven-Bridging-Header.h"'
and change:
RewardingUserAds *obj = [RewardingUserAds alloc];
[obj call_increase_score];
to:
RewardingUserAds *client = [RewardingUserAds new];
[client call_increase_score];
-> I don't really know why it works, but it works

Related

How can React Native access the iOS dictionary to pull the definition of a word?

It looks like there is some reference in Apple's documentation, but I don't see this in any components (listed here for example).
Because this seems like a pretty standard feature, I feel like I'm just missing something basic. It's also not the easiest thing to search for.
Here is Apple's sdk documentation
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIReferenceLibraryViewControllerClassRef/index.html
As you already know, to be able to use native code you'll need to create a native module.
Here's how..
Start by adding a new file in Xcode:
File > New File > Cocoa Touch Class.
Make sure you set it as a subclass of NSObject. Name it ReferenceLibraryManager for example.
In the header file (.h):
You simply need to implement the RCTBridgeModule protocol:
#import "RCTBridgeModule.h"
#interface ReferenceLibraryManager : NSObject <RCTBridgeModule>
#end
In the method file (.m):
You'll need to implement and expose a native method that you can call from your JS like so:
#import "ReferenceLibraryManager.h"
#import <UIKit/UIReferenceLibraryViewController.h>
#import "AppDelegate.h"
#implementation ReferenceLibraryManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(showDefinitionForTerm:(NSString*)term callback:(RCTResponseSenderBlock)callback)
{
if (callback == nil)
return;
BOOL hasDefinition = NO;
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:term])
{
hasDefinition = YES;
dispatch_async(dispatch_get_main_queue(), ^{
UIReferenceLibraryViewController *referenceLibraryVC = [[UIReferenceLibraryViewController alloc] initWithTerm:term];
UIViewController *rootVC = ((AppDelegate*)[UIApplication sharedApplication].delegate).window.rootViewController;
[rootVC presentViewController:referenceLibraryVC animated:YES completion:nil];
});
}
callback(#[#(hasDefinition)]);
}
#end
Some interesting things to notice:
To be able to present a "view controller" like we're doing here, you need another view
controller to present it. The code above does that using the
"root view controller" of the app. It's supposed to work for most
cases but it also depends on the structure of your app.
React native will not call the native component methods on the main thread. When handling anything related to UI you must do it on the main thread, hence to usage of dispatch_async to the main thread.
The JavaScript part:
You import your native module and just use it..
var React = require('react-native');
var ReferenceLibraryManager = React.NativeModules.ReferenceLibraryManager;
....
var term = "React";
ReferenceLibraryManager.showDefinitionForTerm(term, (hasDefinition) => {
if(!hasDefinition) {
alert('definition not found...');
}
})
By the way - you can find the complete guide and example for creating native modules on the react native docs.
For anyone who still need this functionality. I've written a npm package for doing just that(Thanks, Artal).
A quick demo is given below.
may be this helps you
NSString *str =#"pretty";
// pass word as string in str which you want to search
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:str]) {
UIReferenceLibraryViewController* ref =
[[UIReferenceLibraryViewController alloc] initWithTerm:str];
[self presentViewController:ref animated:YES completion:nil];
}
else
{
// do something when no definition found
}

ARC Semantic Issue: "No visible #interface for UIImageView" and PFImageView Semantic Issues

I have a profileviewcontroller where these image views come about in order to fetch the profile picture mainly. But at the start of my profileviewController.m it is declaring "unknown type name 'PFImageView'; did you mean 'UIImageview'?" For the following line:
#property (strong, nonatomic) PFImageView *imageUser;
I get the same exact one if the user were to add his profile pic:
-(void)addProfilePic
{
//---------------------------------------------------------------------------------------------------------------------------------------------
imageUser = [[PFImageView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-100)/2, 50, 100, 100)];
I don't want to necessarily change to UIImageView thinking it might mess up something else within the code so was wondering on a clarification. Also I have a ARC Semantic Issue involving "No visible #interface for 'UIImageView' declares the selector 'setFile.'" and "No visible #interface for 'UIImageView' declares the selector 'loadInBackground.'" This is within my call on getting the profile picture:
-(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];
}
The help is much appreciated, thanks for the time guys.
Import ParseUI FrameWork into your Project.
#import <ParseUI/ParseUI.h>
Import the above line into your header file where you want to use PFImageView
The unknown type is telling you that your code doesn't yet have a reference to the file that defines PDFImageView.
Most likely, you are missing a line like #import "PFImageView.h" (or you may need to import a framework that contains the PFImageView header)
For now, Xcode is suggesting UIImageView it its place because UIKit must already be imported somewhere, and that is the closest class name to PFImageView that it can find in its scope.
Once you have imported the header file, assuming it exposes the method loadInBackground, your second error should disappear.

iOS7 compiler won't recognize method call with message

So this one has me stumped - probably something simple, but I'm clueless.
I'm defining a custom class, containing one method that receives one message (an integer). When calling that method, the compiler refuses to recognize the message I'm trying to send along with the call. ("No known class method for selector 'sendMessage:'. Removing the message from both the call and the definition - i.e. removing the :(int)mode from the definition, and the :1 from the call - allows it to compile fine (but then of course I lose the functionality).
I've tried defining it as an instance method, and as a class method - neither one works.
Many thanks in advance for your collective wisdom!
custom class "Communications.h":
#interface Communications : NSString
+(NSString*)sendMessage:(int)mode;
#end
Communications.m:
#import "Communications.h"
#interface Communications ()
#end
#implementation Communications
+(NSString*)sendMessage:(int)mode {
// Do something important
}
ViewController.h:
#import "Communications.h"
- (void) tapPanic:(UITapGestureRecognizer*)sender;
ViewController.m:
- (void) tapPanic:(UITapGestureRecognizer *)sender {
[Animations animatePanic:self.view type:0];
panicactive = 1;
NSString* tmpResponse = [Communications sendMessage:1];
UILabel* tmpServerResponsePanic = [self.view viewWithTag:10002];
tmpServerResponsePanic.text = tmpResponse;
[[self serverResponsePanic] setNeedsDisplay];
}
So, chalk it up to weirdness with Xcode... copy / pasting the contents of Communications .h and .m into new files, with a new class definition (Comms), did the trick. I think the compiler got confused and was remembering an old definition of the method.

Accessing Variables in another class and dividing NSString

Hy I have a problem accessing variables in another class.
Im making an app that makes note and let you study from the notes you make, for example the user makes a note that says "Oceanic dolphins: are members of the cetacean...", and when the user press a button to study it appears something like this "what are Oceanic Dolphins" then the user press a button it appears something like this "they are members of the cetacean..." the problem I have is this When i enter the ViewController that makes the question it appears empty I think the problem lies on one of the next codes
I make the Variable Globals like this
QueRes.h
#import <Foundation/Foundation.h>
#interface QueRes : NSObject
#property NSString *question;
#property NSString *response;
#end
QueRes.m
#import "QueRes.h"
#implementation QueRes
#end
I divide the NSString of the note like this
NSArray *card = [_argumentTextView.text componentsSeparatedByString:#":"];
QueRes *make = [[QueRes alloc] init];
if ([card count] >= 2)
{
make.question = card [0];
make.response = card [1];
}
the I apply the variable question and response in a ViewController like this
- (void)viewDidLoad
{
[super viewDidLoad];
QueRes *make = [[QueRes alloc] init];
_questionTextView.text = make.question;
}
then in other view controller i have the same code but apply with the response variable
Please help me I been stuck in this for weeks (I have Xcode 5 and the app runs in IOS 7)
(if you need more of the code of the program to help me fix it just tell me )
You are making a new instance of a QueRes in your viewDidLoad method. Unless the init method of QueRes sets its question and response properties to something, they will be uninitialized, which is why you are not seeing anything in your text view: there is nothing to show.
Naming the QueRes instance you make in the third code block you posted make does not make it the same instance as the instance in the viewDidLoad method, and it is not a global variable at all. It is a separate instance of QueRes.

Errors when trying to use CIKernel CoreImage/QuartzCore class on iOS

Hi I'm trying to use the CIKernel class
I don't understand why is it that I keep getting these errors:
AdriansFilter.m:23:29: error: receiver 'CIKernel' for class message is a forward declaration [4]
AdriansFilter.m:23:28:{23:28-23:62}: error: no known class method for selector 'kernelsWithString:' [4]
I've tried importing QuartzCore framework but it still doesn't work. I'm stuck because I really cannot determine what is the real issue here. Could somebody help me please?
my h file looks like this:
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#interface AdriansFilter: CIFilter
{
CIImage *inputImage;
CIColor *inputColor;
NSNumber *inputDistance;
NSNumber *inputSlope;
}
#end
my m file looks like this
#import "AdriansFilter.h"
#implementation AdriansFilter
static CIKernel *hazeRemovalKernel = nil;
- (id)init
{
if(hazeRemovalKernel == nil)
{
NSBundle *bundle = [NSBundle bundleForClass: [self class]];
NSString *code = [NSString stringWithContentsOfFile: [bundle
pathForResource: #"MyHazeRemoval"
ofType: #"cikernel"]];
NSArray *kernels = [CIKernel kernelsWithString: code];
hazeRemovalKernel = [kernels objectAtIndex:0];
}
return [super init];
}
#end
You haven't imported a header where the interface for CIKernel is defined. By searching the Apple developer documentation, I don't find a CIKernel class reference for iOS. This suggests to me that the CIKernel class is not available on iOS.
In Xcode, click on CIKernel in your source file to put the text cursor there, then open the right column (also known as Utilities) of the Xcode window, then select the quick help inspector tab. If the selected class is available for iOS, you should see documentation for the class and a reference to which header file declares it. When I try this with the CIKernel class selected in an iOS project, I just get "No Quick Help".

Resources