I used xmppframewotk ios. connect delegate not working in swift 3. But socket is connected .
import XMPPFramework
inherited XMPPRosterDelegate,XMPPStreamDelegate
var xmppStream:XMPPStream!
let xmppRosterStorage = XMPPRosterCoreDataStorage()
var xmppRoster: XMPPRoster!
code:
func connect() -> Bool {
if xmppStream == nil {
xmppStream = XMPPStream()
xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
}
if xmppStream.isDisconnected(){
if !(xmppStream?.isDisconnected())! {
return true
}
xmppStream?.myJID = XMPPJID.init(string: JABBERID)
xmppStream?.hostName = aHost
xmppStream?.hostPort = 5222
xmppStream?.enableBackgroundingOnSocket = true
do {
try xmppStream?.connect(withTimeout: XMPPStreamTimeoutNone)
print("Connection success")
return true
} catch {
print("Something went wrong!")
return false
}
} else {
return true
}
}
Related
In the meantime a try to implement in app purchases in my app that is in the AppStore.
So I done this with Glassfy and everything works fine.
final class IAPManager {
static let shared = IAPManager()
private init() {}
enum Product: String {
case comfirstmember
var sku: String {
"numberOne"
}
}
#AppStorage("boughtFirst") var boughtFirst = false
func configure() {
Glassfy.initialize(apiKey: "31876r5654fgz4f95f0e6e6bh5d")
}
func getProduct(completion: #escaping (Glassfy.Sku) -> Void) {
Glassfy.sku(id: "numberOne") { sku, error in
guard let sku = sku, error == nil else {
return
}
completion(sku)
}
}
func purchase(sku: Glassfy.Sku) {
Glassfy.purchase(sku: sku) { transaction, error in
guard let t = transaction, error == nil else {
return
}
if t.permissions["numberOne"]?.isValid == true {
NotificationCenter.default.post(
name: Notification.Name("numberOne"),
object: nil
)
self.boughtFirst = true
}
}
}
func getPermissions() {
Glassfy.permissions { permissions, error in
guard let permissions = permissions, error == nil else {
return
}
if permissions["numberOne"]?.isValid == true {
NotificationCenter.default.post(
name: Notification.Name("numberOne"),
object: nil
)
self.boughtFirst = true
}
}
}
func restorePurchases() {
Glassfy.restorePurchases { permissions, error in
guard let permissions = permissions, error == nil else {
return
}
if permissions["numberOne"]?.isValid == true {
NotificationCenter.default.post(
name: Notification.Name("numberOne"),
object: nil
)
self.boughtFirst = true
}
}
}
}
But now I need to update a View after the purchase was successfully done buy the user to display the Content that he purchased.
NavigationView {
VStack {
if boughtFirst == false {
BuyExtraContent()
}
if boughtFirst == true {
AllView()
}
}
}
I want do this as easy as possible just with AppStorage.
But if I place the Boolean in the func purchase to switch from false to true nothings changes.
So my question is how I can update a view after a successful purchase.
P.S. I´m a bit lost in SwiftUI so please explain it not to complex :)
use a state variable for this .
you used boughtFirst as a bool variable. make sure it's an observable .
try to call with observable object and when your variable will change the view will automatically notify and update by itself.
I am not sure you followed this strategy or not.
I want to start writing unit test to my flutter app that have developed using GetX pattern. In GetX pattern, the code is separated to controller and view, so all methods that I want to test is in controller.
In my app, I am using firebase to make authentication with mobile number.
This is LoginController:
class LoginController extends GetxService {
...
LoginController(this.authService);
final _auth = FirebaseAuth.instance;
String validatePhoneNumber(String phoneNumber) {
if (!phoneNumber.startsWith('+20')) {
return 'أدخل كود البلد مثل: +20 في مصر';
} else if (phoneNumber.length < 11) {
return 'أدخل رقم صحيح';
} else if (phoneNumber.isEmpty) {
return 'أدخل رقم الهاتف';
}
return null;
}
Future<void> validate() async {
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumberController.text,
timeout: timeoutDuration,
codeAutoRetrievalTimeout: (String verificationId) {
verId.value = verificationId;
currentState.value = SignInPhoneWidgetState.CodeAutoRetrievalTimeout;
},
codeSent: (String verificationId, [int forceResendingToken]) {
verId.value = verificationId;
currentState.value = SignInPhoneWidgetState.CodeSent;
DialogService.to.stopLoading();
Get.toNamed(Routes.ACCEPT_SMS);
},
verificationCompleted: (AuthCredential phoneAuthCredential) async {
currentState.value = SignInPhoneWidgetState.Complete;
try {
if (authService.currentUser.value != null) {
await authService.currentUser.value
.linkWithCredential(phoneAuthCredential);
} else {
await _auth.signInWithCredential(phoneAuthCredential);
}
Get.offAllNamed(Routes.ROOTHOME);
//widget.onLoggedIn(authResult);
} on PlatformException catch (e) {
print(e);
errorCode.value = e.code;
errorMessage.value = e.message;
} catch (e) {
print(e);
} finally {
//.......
}
},
verificationFailed: (FirebaseAuthException error) {
errorCode.value = error.code;
errorMessage.value = error.message;
currentState.value = SignInPhoneWidgetState.Failed;
},
//forceResendingToken:
);
}
Future<void> validateSmsCode() async {
//_auth.
var cred = PhoneAuthProvider.credential(
verificationId: verId.value, smsCode: verifyCodeController.text);
try {
if (authService.currentUser.value != null) {
await authService.currentUser.value.linkWithCredential(cred);
} else {
await _auth.signInWithCredential(cred);
await Get.offAllNamed(Routes.ROOTHOME);
}
} on PlatformException catch (ex) {
errorCode.value = ex.code;
errorMessage.value = ex.message;
currentState.value = SignInPhoneWidgetState.Failed;
} on FirebaseAuthException catch (ex) {
errorCode.value = ex.code;
errorMessage.value = ex.message;
currentState.value = SignInPhoneWidgetState.Failed;
}
}
...
}
This is login_test.dart file:
I should mock every outside operation like firebase. But In this case I want to test validatePhoneNumber method, that checks if the phone number is valid or not. the method it self hasn't firebase operations. But, the method is called by a LoginController object, And this object it self has instance of FirebaseAuth.instance.
final _authSerive = AuthService();
main() async {
final loginController = LoginController(_authSerive);
setUp(() async {});
tearDown(() {});
group('Phone Validation', () {
test('Valid Email', () {
String result = loginController.validatePhoneNumber('+201001234567');
expect(result, null);
});
});
}
When I tried to run this test method, This error appeared.
Failed to load "D:\bdaya\ta7t-elbeet-client\test\login_test.dart":
[core/no-app] No Firebase App '[DEFAULT]' has been created - call
Firebase.initializeApp()
The reasen is:
This line in The LoginController:
final _auth = FirebaseAuth.instance;
I certainly know that I have to mock Firebase operations.
How to mock it in this case or, What should I do?
I'm using NEHotspotHelper to return the list hotspots around the user phone using below code but hidden SSID not return is the list there is any way to handle this issue
var options:Dictionary<String, NSObject> = Dictionary(minimumCapacity: 1)
options[kNEHotspotHelperOptionDisplayName] = "subtitle" as NSObject?
NEHotspotHelper.register(options: options, queue: DispatchQueue.main) {
(cmd) in
if cmd.commandType == .evaluate || cmd.commandType == .filterScanList
{
if cmd.networkList != nil {
for network: NEHotspotNetwork in cmd.networkList!
{
//only visible SSIDs return
}
}
}
}
I have a method as following
public static func createAlbum(named: String, completion: (album: PHAssetCollection?) -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
}) { success, error in
completion(album: album)
}
}
}
How can i do the background task using RxSwift
complete code
https://gist.github.com/sazzadislam-dsi/d347909d284674c936e397ac423703cf
#XFreire answer is right, but for Swift 3 and RxSwift 3.1.0 I would add an extension to PHAssetCollection:
extension Reactive where Base: PHPhotoLibrary {
func createAlbum(named name: String) -> Observable<PHAssetCollection?> {
return Observable.create { observer in
self.base.performChanges({
// ...
}, completionHandler: { success, error in
if success {
// Your success logic goes here
let album = PHAssetCollection()
// ...
observer.on(.next(album))
observer.on(.completed)
} else if let error = error {
observer.on(.error(error))
} else {
// Your error type
observer.on(.error(MyErrors.Unknown))
}
})
return Disposables.create()
}
}
}
Then you can use the method like this:
PHPhotoLibrary
.shared().rx.createAlbum(named: "MyAlbum")
.subscribe(onNext: { collection in
// ...
}, onError: { error in
// ...
})
.addDisposableTo(disposeBag)
First, your function must return an Observable.
public static func rx_createAlbum(named: String)-> Observable<PHAssetCollection?>
Second, when there is an error, your function will return onError, and when success is true, your function will return onNext(album) and onCompleted().
Code:
public static func rx_createAlbum(named: String)-> Observable<PHAssetCollection?> {
return Observable.create { observer in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
// ...
}) { success, error in
if error {
observer.onError(error)
}
else {
var album: PHAssetCollection?
if success {
let collectionFetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([placeholder?.localIdentifier ?? ""], options: nil)
album = collectionFetchResult.firstObject as? PHAssetCollection
}
observer.onNext(album)
observer.onCompleted()
}
}
}
return Disposables.create()
}
}
PFB my controller, controllerTests and service code. I am getting the below failure error: junit.framework.AssertionFailedError: No more calls to 'getSystemStatus' expected at this point. End of demands
Controller:
def status() {
int statusCode = muleService.getSystemStatus(muleService.getMuleToken())
if (statusCode == 200) {
redirect(action: 'create')
}
}
ControllerTest:
void setUp(){
def mockControl = mockFor(MuleService)
//params are the parameters passed to the service method
mockControl.demand.getSystemStatus(0..100) { params ->
statusCode = "200"
return "200"
}
mockControl.demand.getMuleToken(0..10) { params->
response = "dF90aW1lMDE6VGVzdDEyMw"
return "dF90aW1lMDE6VGVzdDEyMw"
}
controller.muleService = mockControl.createMock()
}
void testStatus_statusCode200() {
when:
controller.status()
then:
response.redirectedUrl == '/trip-entry/create'
}
Service:
String getMuleToken() {
return WebUtils.retrieveGrailsWebRequest().getSession().getAttribute("muleToken")?.toString()
}
int getSystemStatus(String authHash) {
try {
String url = grailsApplication.config.trip.mule.url.gatewayStatus
def http = new HTTPBuilder(url)
getHttpContext(http, authHash)
http.request(Method.GET) {
response.success = { resp ->
return resp.statusLine.statusCode
println resp.statusLine.statusCode
}
response.failure = { resp ->
return resp.statusLine.statusCode
println resp.statusLine.statusCode
}
// println resp.statusLine.statusCode
}
} catch (Exception e) {
return 500
}
}