preference not working ios watch - ios

I have developing an ios watch app. I want to pass a value to ios watch. i have try several methods. When storing value in a element of ios app. I am using xcode 8 and the code is written in swift.
ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
print("webview")
let url = URL (string: "http://www.google.com/")
let request = URLRequest(url: url!)
webview.loadRequest(request)
let preferences = UserDefaults.standard
let currentLevel = "cl"
let currentLevelKey = "currentLevel"
preferences.set(currentLevel, forKey: currentLevelKey)
preferences.synchronize()
print("pref")
}
}
Then i have call the preference in ios watch app the code is
InterfaceController.swift
class InterfaceController: WKInterfaceController {
#IBAction func clickingTheButton() {
print("clicked")
let preferences = UserDefaults.standard
let currentLevelKey = "currentLevel"
if preferences.object(forKey: currentLevelKey) == nil {
// Doesn't exist
print("Doesn't exist")
} else {
let currentLevel = preferences.integer(forKey: currentLevelKey)
print("preference : ",currentLevel)
}
}
}
How to pass value from ios to watch app. Please help me.

Related

Load WebView before transitioning to it

I had to update an app that i didn't touch for quite a while and now am facing an 'Fatal error: Unexpectedly found nil while unwrapping an Optional value' in DetailViewController::refreshWebView() when executing webView.loadRequest(myRequest) because webView is nil at this point in time.
I didn't change anything relating to how the DetailViewController is loaded and assigned to MasterViewController, so I am very confused as why this does not work anymore.
Was there something changed that i am not aware of? Or did I implement this whole thing incorrectly in the first place and it was coincidence that it worked?
import UIKit
protocol EventSelectionDelegate: class {
func eventSelected(_ newEvent: String)
}
class MasterViewController: UIViewController, UIWebViewDelegate {
var activityView: UIActivityIndicatorView!
weak var delegate: EventSelectionDelegate?
func detailChosen(_ detailUrlString: String) {
delegate?.eventSelected(detailUrlString)
activityView.startAnimating()
}
func transitionToDetail() {
if let detailViewController = self.delegate as? DetailViewController {
DispatchQueue.main.async {
self.activityView.stopAnimating()
self.splitViewController?.showDetailViewController(detailViewController, sender: nil)
}
}
}
}
// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToUIApplicationOpenExternalURLOptionsKeyDictionary(_ input: [String: Any]) -> [UIApplication.OpenExternalURLOptionsKey: Any] {
return Dictionary(uniqueKeysWithValues: input.map { key, value in (UIApplication.OpenExternalURLOptionsKey(rawValue: key), value)})
}
import UIKit
class DetailViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
var animationWaitDelegate: MasterViewController!
var eventUrl: String! {
didSet {
self.refreshWebView()
}
}
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
}
func refreshWebView() {
let myURL = URL(string:eventUrl!)
let myRequest = URLRequest(url: myURL!)
webView.loadRequest(myRequest)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
animationWaitDelegate.transitionToDetail()
}
}
extension DetailViewController: EventSelectionDelegate {
func eventSelected(_ newEvent: String) {
eventUrl = newEvent
}
}
// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToUIApplicationOpenExternalURLOptionsKeyDictionary(_ input: [String: Any]) -> [UIApplication.OpenExternalURLOptionsKey: Any] {
return Dictionary(uniqueKeysWithValues: input.map { key, value in (UIApplication.OpenExternalURLOptionsKey(rawValue: key), value)})
}
PS: I found a workaround in the meantime where I added a flag in DetailViewController that allows me to call refreshWebView in viewDidLoad if webView was nil the first time it was called.
First you have to update following code for null check, which will prevent crashing your app.
func refreshWebView() {
if webView != nil {
let myURL = URL(string:eventUrl!)
let myRequest = URLRequest(url: myURL!)
webView.loadRequest(myRequest)
}
}
After at transition add following code will fix your issue.
func transitionToDetail() {
if let detailViewController = self.delegate as? DetailViewController {
DispatchQueue.main.async {
self.activityView.stopAnimating()
detailViewController.loadViewIfNeeded() // Add this line of code
self.splitViewController?.showDetailViewController(detailViewController, sender: nil)
}
}
}
loadViewIfNeeded() method will load your controls before open screen if required.

How can i make if in my webkit webview click on Amazon link or facebook link it will open app or if not found it will open in browser

here is my webview controller
import UIKit
import WebKit
class NewsWeb: UIViewController {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://google.com"
let request = URLRequest(url: URL(string: url)!)
self.webView.load(request)
webView.scrollView.addSubview(refreshControl)
webView.navigationDelegate = self as? WKNavigationDelegate
webView.uiDelegate = self as? WKUIDelegate
webView.scrollView.delegate = self as? UIScrollViewDelegate
webView.backgroundColor = UIColor.black
webView.isMultipleTouchEnabled = false
}
how can make if in the webview page clik on Amazon or Facbook link it will be open in app if in the device has installed the app if not found the app it will be open in browser
You could use my code and modify on you own bases:
func makeSocialLinkViewModel(for socialNetwork: SocialNetwork) -> TextCellViewModel {
TextCellViewModel(text: socialNetwork.rawValue) {
UIApplication.tryOpening(urls: [socialNetwork.appUrl, socialNetwork.websiteUrl])
}
}
I have a let:
let facebook = makeSocialLinkViewModel(for: .facebook)
case:
case facebook = "Facebook"
var appUrl: String {
case .facebook:
return Constants.Facebook.appUrl
}
var websiteUrl: String {
switch self {
case .facebook:
return Constants.Facebook.websiteUrl
}
}
And finally constants are just like:
struct Facebook {
static let appUrl = "fb://profile/(your_facebook_id)"
static let websiteUrl = "https://www.facebook.com/(your_url)"
}
Hope this helps

Today Extension App Group not work swift ios 11

i've a problem with sharing data from my app to Today extension.
I've enable the app group in capabilities.
In my app controller I've this
var defaults = UserDefaults(suiteName: "group.com.mygroup")!
defaults.setValue("today extension text", forKey: "customKey")
defaults.synchronize()
And this is my Today Extension code:
class TodayViewController: UIViewController, NCWidgetProviding {
#IBOutlet var TimerText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func widgetPerformUpdate(completionHandler: (#escaping (NCUpdateResult) -> Void)) {
completionHandler(NCUpdateResult.newData)
let defaults = UserDefaults(suiteName: "group.com.mygroup")!
let myString = defaults.value(forKey: "customKey")
if(String(describing: myString) == "" || myString == nil) {
TimerText.text = "no data"
} else {
TimerText.text = String(describing: myString!)
}
}
}
The widget after lunch application show always no data although in the m ain app the variable is correctly set.
Thanks!

Saving data with User defaults

I am making a feature in my app that allows you to mute the music, i put a switch there so you can change it between on and off. However, the switch works fine in the app, when i close the vie controller and i pull it back up it will still show it how it was left, but when you close the app and open it back up the switch looks as if it off but it still plays the music.This is my code and i will attach a picture.
import Foundation
import UIKit
import SpriteKit
import AVFoundation
var bombSoundEffect: AVAudioPlayer!
var Ghost = SKSpriteNode()
class SecondViewController: UIViewController {
var sw = false
#IBOutlet var mySwitch: UISwitch!
#IBAction func switchpressed(_ sender: AnyObject) {
let defaults = UserDefaults.standard
if mySwitch.isOn{
defaults.set(true, forKey: "SwitchState")
if bombSoundEffect != nil {
bombSoundEffect.stop()
bombSoundEffect = nil
}
}
else{
defaults.set(false, forKey: "SwitchState")
let path = Bundle.main.path(forResource: "newmusic.wav", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
bombSoundEffect = sound
sound.numberOfLoops = -1
sound.play()
} catch {
// couldn't load file :(
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
let defaults = UserDefaults.standard
if (defaults.object(forKey: "SwitchState") != nil) {
mySwitch.isOn = defaults.bool(forKey: "SwitchState")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
}
and here is the picture
If you didn't understand what i said above please ask questions.
As i understand from your code, you are not checking the User Defaults properly. In your viewDidLoad you have to check the bool value of your user defaults is set to true or not.
override func viewDidLoad() {
super.viewDidLoad()
let defaults = UserDefaults.standard
//check bool Value is set to true or not
if defaults.bool(forKey: "SwitchState") == true {
mySwitch.isOn = defaults.bool(forKey: "SwitchState")
} else {
print("false")
}
}
Please check this code. Hopefully it will helps you.
In viewDidLoad you need to check for when you need to pause the music:
// Keep this part the same
let defaults = UserDefaults.standard
if (defaults.object(forKey: "SwitchState") != nil) {
mySwitch.isOn = defaults.bool(forKey: "SwitchState")
}
// Check to see if the switch is off, if so, stop the music
if !mySwitch.isOn {
bombSoundEffect.stop()
bombSoundEffect = nil
}
You can also take out the duplicate call to super.viewDidLoad.
Note: It really looks like there is more code in your view controller, because there isn't anything currently there that would be playing the music, so this solution may not work given that you haven't provided enough information.

Retrieve Facebook user ID in Swift

I have a helper function that is grabbing the picture from a specific URL:
func getProfilePicture(fid: String) -> UIImage? {
if (fid != "") {
var imageURLString = "http://graph.facebook.com/" + fid + "/picture?type=large"
var imageURL = NSURL(string: imageURLString)
var imageData = NSData(contentsOfURL: imageURL!)
var image = UIImage(data: imageData!)
return image
}
return nil
}
I proceed to call getProfilePicture() with the user's Facebook ID, and store the output into a UIImageView. My question is, how can I find a Facebook user's ID? Do I request a connection through Facebook? It would be helpful if some code is provided here.
Thank you for your help.
If they are already successfully logged into your app:
FBSDKAccessToken.current().userID
What worked for me:
Use FBSDKLoginButtonDelegate
Implement func loginButtonWillLogin
Add notification to onProfileUpdated
In onProfileUpdated func proceed to get your Profile info
After onProfileUpdated is called, you can use your FBSDKProfile in any Controller
And now, some Swift 3 code...
import UIKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Facebook login integration
let logBtn = FBSDKLoginButton()
logBtn.center = self.view.center
self.view .addSubview(logBtn)
logBtn.delegate = self
}
func loginButtonWillLogin(_ loginButton: FBSDKLoginButton!) -> Bool {
print("will Log In")
FBSDKProfile.enableUpdates(onAccessTokenChange: true)
NotificationCenter.default.addObserver(self, selector: #selector(onProfileUpdated(notification:)), name:NSNotification.Name.FBSDKProfileDidChange, object: nil)
return true
}
func onProfileUpdated(notification: NSNotification)
{
print("profile updated")
print("\(FBSDKProfile.current().userID!)")
print("\(FBSDKProfile.current().firstName!)")
}
}

Resources