App delegate features in viewcontrller - Facebook - ios

I have a sample code with a header and an implementation file and the two appDelegate files. I would like to add what the project does to my app. The first two files are a ViewController file, so I just need to drag it in, but the other two are AppDelegate, and I obviously can't have two app delegate. But in the case of this sample app the app delegate is used as a proper viewcontroller, because in the .m file of the UIViewController file, there is this code:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
Since the AppDelegate doesn't implement methods such as applicationWillBecomeActive:, how can I transform the file into a UIViewController file? What do I need to change in the code above to call the controller, instead of the Delegate (my AppDelegate will so remain untouched).
The code is on GitHub
This is what I have done (the code needs the Facebook API to be included). Go on the download tab and download Archive.zip and AppDelegate.zip: https://github.com/Alexmitico45/FacebookRequests/downloads
Basically the controller ContactFBSViewController is linked the a viewcontroller in the storyboard.

AppDelegate is the singleton that implement the protocol UIApplicationDelegate, because so you can't duplicate it, it must be unique.
You can create your own singletons to store the info available all over your app.
You can google "objective-c singleton" and get some good link to do it

Related

Is it safe to remove SceneDelegate from Existing project?

I have my project which supports minimum ios version 12.4 and I'm using swift 5.2 in my project. To suuport Zoom SDK I need to remove sceneDelegate.swift file and SceneManifest from info.plist. Is it safe to remove sceneDelegate from existing project?
In my thought AppDelegate will be responsible for the application lifecycle and setup. The SceneDelegate will be responsible for what is shown on the screen (Windows or Scenes) handle and manage the way your app is shown.So will there be any issues in UI Flow of my APP if I remove Scene Delegate?
If I remove sceneDelegate how will handle app Foreground and Background Events?
Yes it 100% safe to remove SceneDelegate from your iOS app.
Please refer below steps to remove it from your app
Remove SceneDelegate.swift file
Remove Application Scene Manifest from Info.plist file
Add var window: UIWindow? to AppDelegate.swift
Replace #main with #UIApplicationMain
Remove UISceneSession Lifecycle ( functions ) in AppDelegate
After deleting scene delegate you would not able to do following
In iOS 13 and later, users can create multiple copies of your app’s UI
and toggle between them in the app switcher. On iPad, users can also
display one copy of your app’s UI side by side with another copy. For
each copy of your app’s UI, you use a scene object to manage the
window, views, and view controllers that present the UI onscreen
https://developer.apple.com/documentation/uikit/app_and_environment/scenes/specifying_the_scenes_your_app_supports
To answer you question to regarding view life cycle please refer this - https://www.appypie.com/scene-delegate-app-delegate-xcode-11-ios-13#:~:text=On%20iOS%2013,at%20a%20time.

`applicationDidFinishLaunching` not called in iOS project

applicationDidFinishLaunching is never called in the running of my app(tested on real device and simulator) despite the fact that applicationDidBecomeActive is called. I can only find macOS solutions to this problem(here, here, and here)
In my info.plist file, I have Main storyboard file base name set correctly. I have a Storyboard entry point hooked up to my initial view controller. The app launches and I see everything I should, but applicationDidFinishLaunching is never called and I use that method to handle on-startup configuration and construction.
Based on the macOS posts, I tried dragging an object(yellow cube) into the IB hierarchy of the initial view controller, set its class as my app delegate class, and set it as the initial view controller's delegate. But applicationDidFinishLaunching is still never called.
The method signature is correct (I know this is a common issue for Swift projects)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application{
//code
}
In the documentation for applicationDidFinishLaunching: it says:
Although not formally deprecated, do not use this method in your apps.
Use the application:willFinishLaunchingWithOptions: and
application:didFinishLaunchingWithOptions: methods instead.
If you use application:didFinishLaunchingWithOptions: then applicationDidFinishLaunching: will not be called.

Objective c access another ios app

I have two apps written with Objective C. I have an IOS 8 jailbroken device. Can I get information from the view of another application?
Can I access self.view of the other app? I want to send a clicks command (touch up inside) to button in my other applications? Is it possible?
I have, ios 8, jailbreak device, xcode 6.3.
I'm sorry for my bad english.
no need for jailbrake.
here is something really quick you can try to open and click button in 2nd app.
Use URL schemes to open app from another app.
in the 2nd app in plist in URL types\item\URL Schemes\item0 add string say my2ndapp
to open 2nd app from your 1st app use:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"my2ndapp://somedata"]];
so now url: my2ndapp://somestring - will open your 2nd app and run method in its appdelegate:
(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
//do something with url if you need to, you can use it to tell yuor 2nd app do different things
openedFromURL=YES;//set bool flag, add this BOOL var in .h of appdelegate
return YES;
}
then in the same appdelegate in method
-(void)applicationDidBecomeActive:(UIApplication*)application{
if(!openedFromURL){return;}
//run method from viewcontroller you want, import it in .m and add it in .h of your appdelegate
MyViewController * myvc = [[MyViewController alloc]init];
[myvc myMethodHere];
openedFromURL=NO;//drop flag
}
you can go to your particular view or viewcontroller and run the method that is associated with your button touch up inside event
Similarly, you can make 2nd app interact with 1st one too.

IOS: Is it possible preserve the State of UIViewController in Background

Am developing an IOS app. Where i need to make some images upload to server. Am using NSURLSession and uploadTaskWithRequest to do this. Every thing is working fine in normal way. My requirement user wants to store some post with more than 10 images in app using database SQLLite. And later show all the stored posts in UITableView with button for each UITableViewCell. When user tap on each button it should start uploading each POST to server. So I thought i should persist my UIViewController in AppDelegate so the process of uploading should not be killed when user go to another view controllers.
My Problem: When user close the app the process is in which UIViewController POST uploading is Stoping. So i would like to know how to keep my UIViewController live even in app go close or go into background.
Is there any better way to fulfill my requirement.
Here are some better ways!
Use a singleton. In my apps, I'll usually create a class called "DataHandler" that handles all of the NSURLSession/CoreData/etc stuff so all I have to do is
[DataHandler uploadImages:images];
and when I need the information back I'll call
self.tableData = [DataHandler lastUploadedImages];
This way I don't have to worry if my view controller is still alive.
Have the AppDelegate do the NSURLSession stuff. You already know the AppDelegate is persisted so why not just have that guy do it! Here is an example:
UIApplication *sharedApplication = [UIApplication sharedApplication];
AppDelegate *appDelegate = [sharedApplication delegate];
// this is the method you would add to your AppDelegate. Make sure to
// import your AppDelegate's header file so your ViewController can
// call the method.
[appDelegate uploadImages:images];

iOS, use AppDelegate in InterfaceBuilder, two instances created

In an iOS application I implemented some application logic in the AppDelegate.
Several view controllers etc. need to call the AppDelegate, so i placed the AppDelegate in Interface Builder and gave the ViewControllers an IBOutlet AppDelegate* and drew a link to it.
In the app I see now that there seem to be two instances of the AppDelegate created, one that is called and that gets all the notifications and one that is linked to the ViewControllers.
So i changed my app to use in the ViewControllers:
app_del = (AppDelegate*)[[UIApplication sharedApplication] delegate].
Then I'm calling this appDelegate. But this also is a different instance than the one that gets the notifications. Can anybody explain why?
Can anybody tell me what is going wrong? My suspicion from googling is that the iOS creates the AppDelegate instance and Interface Builder does not know about that one and creates another one? Is that correct?
Can anybody give me some hint on how to best create/plan instances in Interface Builder and in what order they are created in application at startup and in what notification I can rely that they are all created?
Thanks for any hints,
Torsten.
Your suspicion is correct. iOS creates an instance of your application delegate when launching your app, this instance is the one that is registered to receive all of the delegate events and so forth. See here.
Anything you add in to a xib or storyboard will be a new instance, and will not work.
To obtain a pointer to the application delegate, use this method:
[[UIApplication sharedApplication] delegate];
There is a school of thought that says you shouldn't overuse the application delegate as a conveniently available global data or method store, but that is outside the scope of this answer.

Resources