Swift support for NetworkPlugin (#201)
Summary: Solves #173 - [x] Add plugins in the sonarkit xcodeproj so that its easy to debug - [x] Get rid off the c++ in the headers - [x] Add example to hit network requests and validate Have a look at the screen shot below ✨⭐️⚡️ <img width="1677" alt="screen shot 2018-08-02 at 12 46 07 pm" src="https://user-images.githubusercontent.com/3865908/43581809-22efe4fe-9652-11e8-9424-f279d07c5c81.png"> Pull Request resolved: https://github.com/facebook/flipper/pull/201 Reviewed By: danielbuechele Differential Revision: D9132157 Pulled By: priteshrnandgaonkar fbshipit-source-id: 2b425506961f02eb2bf629c2bcab0da6e7ce5bb0
This commit is contained in:
committed by
Facebook Github Bot
parent
134a0d96c5
commit
0c60347593
@@ -17,7 +17,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
// let layoutDescriptorMapper = SKDescriptorMapper(defaults: ())
|
||||
// SonarKitLayoutComponentKitSupport.setUp(descriptorMapper: layoutDescriptorMapper)
|
||||
// client.addPlugin(SonarKitLayoutPlugin(rootNode: application, descriptorMapper: layoutDescriptorMapper))
|
||||
// client.addPlugin(SonarKitNetworkPlugin(networkAdapter: SKIOSNetworkAdapter()))
|
||||
|
||||
client?.add(SonarKitNetworkPlugin(networkAdapter: SKIOSNetworkAdapter()))
|
||||
client?.start()
|
||||
|
||||
let storyboard = UIStoryboard(name: "MainStoryBoard", bundle: nil)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
|
||||
<device id="retina4_7" orientation="portrait">
|
||||
<adaptation id="fullscreen"/>
|
||||
</device>
|
||||
<dependencies>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
@@ -31,7 +31,7 @@
|
||||
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="tappedNetworkInspector:" destination="2r2-64-LPh" eventType="touchUpInside" id="6Ta-mA-Ybz"/>
|
||||
<segue destination="qMe-JX-czP" kind="push" id="Poz-iX-aCZ"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="xh0-Q4-60g">
|
||||
@@ -44,9 +44,6 @@
|
||||
<state key="normal" title="ComponentKit Layout">
|
||||
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="tappedComponentKitLayout:" destination="2r2-64-LPh" eventType="touchUpInside" id="amo-e0-rJv"/>
|
||||
</connections>
|
||||
</button>
|
||||
</subviews>
|
||||
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
@@ -72,7 +69,7 @@
|
||||
<!--Network View Controller-->
|
||||
<scene sceneID="Zgm-Mh-TPB">
|
||||
<objects>
|
||||
<viewController storyboardIdentifier="NetworkViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="qMe-JX-czP" customClass="NetworkViewController" sceneMemberID="viewController">
|
||||
<viewController storyboardIdentifier="NetworkViewController" useStoryboardIdentifierAsRestorationIdentifier="YES" id="qMe-JX-czP" customClass="NetworkViewController" customModule="SampleSwift" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="1kD-re-kO3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
@@ -93,7 +90,7 @@
|
||||
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="tappedGetAPI:" destination="qMe-JX-czP" eventType="touchUpInside" id="Xb9-5E-G2l"/>
|
||||
<action selector="tappedGetAPI:" destination="qMe-JX-czP" eventType="touchUpInside" id="jq4-HI-nDK"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Fel-3h-2Ts">
|
||||
@@ -107,7 +104,7 @@
|
||||
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="tappedPOSTAPI:" destination="qMe-JX-czP" eventType="touchUpInside" id="kv5-Bd-q86"/>
|
||||
<action selector="tappedPOSTAPI:" destination="qMe-JX-czP" eventType="touchUpInside" id="gqv-I8-C9f"/>
|
||||
</connections>
|
||||
</button>
|
||||
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cYg-iA-yAD">
|
||||
@@ -122,7 +119,7 @@
|
||||
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
|
||||
</state>
|
||||
<connections>
|
||||
<action selector="tappedGithubLitho:" destination="qMe-JX-czP" eventType="touchUpInside" id="SPR-ez-foo"/>
|
||||
<action selector="tappedFetchFBLitho:" destination="qMe-JX-czP" eventType="touchUpInside" id="bLR-k5-ebI"/>
|
||||
</connections>
|
||||
</button>
|
||||
</subviews>
|
||||
@@ -153,6 +150,7 @@
|
||||
</constraints>
|
||||
<viewLayoutGuide key="safeArea" id="dLV-zh-N2b"/>
|
||||
</view>
|
||||
<navigationItem key="navigationItem" id="foE-rU-54N"/>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="jLg-IP-htV" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
|
||||
88
iOS/SampleSwift/SampleSwift/NetworkViewController.swift
Normal file
88
iOS/SampleSwift/SampleSwift/NetworkViewController.swift
Normal file
@@ -0,0 +1,88 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
import UIKit
|
||||
|
||||
class NetworkViewController: UIViewController {
|
||||
|
||||
@IBAction func tappedGetAPI(_ sender: UIButton) {
|
||||
let getURL = URL(string: "https://demo9512366.mockable.io/FlipperGet")!
|
||||
let dataTask = URLSession.shared.dataTask(with: getURL){ [weak self] (data, response, error) in
|
||||
guard let strongSelf = self else { return }
|
||||
guard let dataUnwrapped = data else {
|
||||
strongSelf.showAlert(message: "Received no data in GET API")
|
||||
return
|
||||
}
|
||||
|
||||
if let errorUnwrapped = error {
|
||||
strongSelf.showAlert(message: "Received error in GET API Error:\(errorUnwrapped.localizedDescription)")
|
||||
return
|
||||
}
|
||||
|
||||
let dict = try! JSONSerialization.jsonObject(with: dataUnwrapped, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as! [String: String]
|
||||
// As sonar cannot detect print() in Logs
|
||||
NSLog("MSG-GET: \(dict["msg"] ?? "Did not find msg key in the received response")")
|
||||
strongSelf.showAlert(message: "Received response from GET API, please check the sonar network plugin for detailed response")
|
||||
}
|
||||
dataTask.resume()
|
||||
}
|
||||
|
||||
@IBAction func tappedPOSTAPI(_ sender: UIButton) {
|
||||
guard let postURL = URL(string: "https://demo9512366.mockable.io/FlipperPost") else {
|
||||
showAlert(message: "Check the POST URL")
|
||||
return
|
||||
}
|
||||
var postRequest = URLRequest(url: postURL)
|
||||
postRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
postRequest.addValue("application/json", forHTTPHeaderField: "Accept")
|
||||
|
||||
let dict = ["app" : "Flipper", "remarks": "Its Awesome"]
|
||||
postRequest.httpBody = try! JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.init(rawValue: 0))
|
||||
postRequest.httpMethod = "POST"
|
||||
let dataTask = URLSession.shared.dataTask(with: postRequest){ [weak self] (data, response, error) in
|
||||
guard let strongSelf = self else { return }
|
||||
guard let dataUnwrapped = data else {
|
||||
strongSelf.showAlert(message: "Received no data in POST API")
|
||||
return
|
||||
}
|
||||
|
||||
if let errorUnwrapped = error {
|
||||
strongSelf.showAlert(message: "Received error in POST API Error:\(errorUnwrapped.localizedDescription)")
|
||||
return
|
||||
}
|
||||
|
||||
let dict = try! JSONSerialization.jsonObject(with: dataUnwrapped, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as! [String: String]
|
||||
// As sonar cannot detect print() in Logs
|
||||
NSLog("MSG-POST: \(dict["msg"] ?? "Did not find msg key in the received response")")
|
||||
strongSelf.showAlert(message: "Received response from POST API, please check the sonar network plugin for detailed response")
|
||||
}
|
||||
dataTask.resume()
|
||||
}
|
||||
|
||||
@IBAction func tappedFetchFBLitho(_ sender: UIButton) {
|
||||
let imageURL = URL(string: "https://raw.githubusercontent.com/facebook/litho/master/docs/static/logo.png")!
|
||||
let dataTask = URLSession.shared.dataTask(with: imageURL){ [weak self] (data, response, error) in
|
||||
guard let strongSelf = self else { return }
|
||||
guard let _ = data else {
|
||||
strongSelf.showAlert(message: "Received no data in Images API")
|
||||
return
|
||||
}
|
||||
|
||||
if let errorUnwrapped = error {
|
||||
strongSelf.showAlert(message: "Received error in Images API Error:\(errorUnwrapped.localizedDescription)")
|
||||
return
|
||||
}
|
||||
|
||||
// As sonar cannot detect print() in Logs
|
||||
NSLog("Got Image")
|
||||
strongSelf.showAlert(message: "Received Litho Image")
|
||||
}
|
||||
dataTask.resume()
|
||||
}
|
||||
|
||||
func showAlert(message: String) {
|
||||
let alertController = UIAlertController.init(title: "Flipper", message: message, preferredStyle: .alert);
|
||||
let alertAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)
|
||||
alertController.addAction(alertAction)
|
||||
present(alertController, animated: true, completion: nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user